[StaticRemoval] Various fixes (#4245)

This commit is contained in:
Tomas Votruba 2020-09-17 09:34:27 +02:00 committed by GitHub
parent 9431c8107b
commit 2630e1b900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 5 deletions

View File

@ -134,6 +134,7 @@ abstract class AbstractFluentChainMethodCallRector extends AbstractRector
$classOfClassMethod[] = null; $classOfClassMethod[] = null;
} }
} }
return count(array_unique($classOfClassMethod)) <= 1; return count(array_unique($classOfClassMethod)) <= 1;
} }

View File

@ -195,13 +195,18 @@ CODE_SAMPLE
// is the same class or external call? // is the same class or external call?
$className = $this->getName($staticCall->class); $className = $this->getName($staticCall->class);
if ($className === 'self') { if ($className === 'self') {
return new MethodCall(new Variable(self::THIS), $staticCall->name, $staticCall->args); return new MethodCall(new Variable(self::THIS), $staticCall->name, $staticCall->args);
} }
$propertyName = $this->propertyNaming->fqnToVariableName($classType); $propertyName = $this->propertyNaming->fqnToVariableName($classType);
$propertyFetch = new PropertyFetch(new Variable(self::THIS), $propertyName);
$currentMethodName = $staticCall->getAttribute(AttributeKey::METHOD_NAME);
if ($currentMethodName === MethodName::CONSTRUCT) {
$propertyFetch = new Variable($propertyName);
} else {
$propertyFetch = new PropertyFetch(new Variable(self::THIS), $propertyName);
}
return new MethodCall($propertyFetch, $staticCall->name, $staticCall->args); return new MethodCall($propertyFetch, $staticCall->name, $staticCall->args);
} }
@ -296,10 +301,30 @@ CODE_SAMPLE
} }
$propertyExpectedName = $this->propertyNaming->fqnToVariableName(new ObjectType($classType)); $propertyExpectedName = $this->propertyNaming->fqnToVariableName(new ObjectType($classType));
if ($this->isTypeAlreadyInParamMethod($constructClassMethod, $classType)) {
return;
}
$constructClassMethod->params[] = new Param( $constructClassMethod->params[] = new Param(
new Variable($propertyExpectedName), new Variable($propertyExpectedName),
null, null,
new FullyQualified($this->classTypes) new FullyQualified($classType)
); );
} }
private function isTypeAlreadyInParamMethod(ClassMethod $classMethod, string $classType): bool
{
foreach ($classMethod->getParams() as $param) {
if ($param->type === null) {
continue;
}
if ($this->isName($param->type, $classType)) {
return true;
}
}
return false;
}
} }

View File

@ -0,0 +1,31 @@
<?php
namespace Rector\RemovingStatic\Tests\Rector\Class_\SingleStaticServiceToDynamicRector\Fixture;
use Rector\RemovingStatic\Tests\Rector\Class_\SingleStaticServiceToDynamicRector\Source\FirstStaticClass;
class ConstructAlreadyThereDependency
{
public function __construct(FirstStaticClass $firstStaticClass)
{
FirstStaticClass::someStaticFunction();
}
}
?>
-----
<?php
namespace Rector\RemovingStatic\Tests\Rector\Class_\SingleStaticServiceToDynamicRector\Fixture;
use Rector\RemovingStatic\Tests\Rector\Class_\SingleStaticServiceToDynamicRector\Source\FirstStaticClass;
class ConstructAlreadyThereDependency
{
public function __construct(FirstStaticClass $firstStaticClass)
{
$firstStaticClass->someStaticFunction();
}
}
?>

View File

@ -50,10 +50,14 @@ final class VisibilityManipulator
} }
/** /**
* @param ClassMethod|Property|ClassConst $node * @param ClassMethod|Property $node
*/ */
public function makeNonStatic(Node $node): void public function makeNonStatic(Node $node): void
{ {
if (! $node->isStatic()) {
return;
}
$node->flags -= Class_::MODIFIER_STATIC; $node->flags -= Class_::MODIFIER_STATIC;
} }

View File

@ -95,7 +95,7 @@ trait VisibilityTrait
} }
/** /**
* @param ClassMethod|Property|ClassConst $node * @param ClassMethod|Property $node
*/ */
public function makeNonStatic(Node $node): void public function makeNonStatic(Node $node): void
{ {