diff --git a/rules/magic-disclosure/src/Rector/MethodCall/AbstractFluentChainMethodCallRector.php b/rules/magic-disclosure/src/Rector/MethodCall/AbstractFluentChainMethodCallRector.php index 134895c7f5f..95754e14ea9 100644 --- a/rules/magic-disclosure/src/Rector/MethodCall/AbstractFluentChainMethodCallRector.php +++ b/rules/magic-disclosure/src/Rector/MethodCall/AbstractFluentChainMethodCallRector.php @@ -134,6 +134,7 @@ abstract class AbstractFluentChainMethodCallRector extends AbstractRector $classOfClassMethod[] = null; } } + return count(array_unique($classOfClassMethod)) <= 1; } diff --git a/rules/removing-static/src/Rector/Class_/SingleStaticServiceToDynamicRector.php b/rules/removing-static/src/Rector/Class_/SingleStaticServiceToDynamicRector.php index 2f7139bca76..74eca443385 100644 --- a/rules/removing-static/src/Rector/Class_/SingleStaticServiceToDynamicRector.php +++ b/rules/removing-static/src/Rector/Class_/SingleStaticServiceToDynamicRector.php @@ -195,13 +195,18 @@ CODE_SAMPLE // is the same class or external call? $className = $this->getName($staticCall->class); - if ($className === 'self') { return new MethodCall(new Variable(self::THIS), $staticCall->name, $staticCall->args); } $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); } @@ -296,10 +301,30 @@ CODE_SAMPLE } $propertyExpectedName = $this->propertyNaming->fqnToVariableName(new ObjectType($classType)); + + if ($this->isTypeAlreadyInParamMethod($constructClassMethod, $classType)) { + return; + } + $constructClassMethod->params[] = new Param( new Variable($propertyExpectedName), 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; + } } diff --git a/rules/removing-static/tests/Rector/Class_/SingleStaticServiceToDynamicRector/Fixture/construct_already_there_dependency.php.inc b/rules/removing-static/tests/Rector/Class_/SingleStaticServiceToDynamicRector/Fixture/construct_already_there_dependency.php.inc new file mode 100644 index 00000000000..cbd403e6897 --- /dev/null +++ b/rules/removing-static/tests/Rector/Class_/SingleStaticServiceToDynamicRector/Fixture/construct_already_there_dependency.php.inc @@ -0,0 +1,31 @@ + +----- +someStaticFunction(); + } +} + +?> diff --git a/src/PhpParser/Node/Manipulator/VisibilityManipulator.php b/src/PhpParser/Node/Manipulator/VisibilityManipulator.php index 899a9d7140b..d8515a9da24 100644 --- a/src/PhpParser/Node/Manipulator/VisibilityManipulator.php +++ b/src/PhpParser/Node/Manipulator/VisibilityManipulator.php @@ -50,10 +50,14 @@ final class VisibilityManipulator } /** - * @param ClassMethod|Property|ClassConst $node + * @param ClassMethod|Property $node */ public function makeNonStatic(Node $node): void { + if (! $node->isStatic()) { + return; + } + $node->flags -= Class_::MODIFIER_STATIC; } diff --git a/src/Rector/AbstractRector/VisibilityTrait.php b/src/Rector/AbstractRector/VisibilityTrait.php index cf11b42035c..17788593f62 100644 --- a/src/Rector/AbstractRector/VisibilityTrait.php +++ b/src/Rector/AbstractRector/VisibilityTrait.php @@ -95,7 +95,7 @@ trait VisibilityTrait } /** - * @param ClassMethod|Property|ClassConst $node + * @param ClassMethod|Property $node */ public function makeNonStatic(Node $node): void {