diff --git a/src/PhpParser/Node/Manipulator/ChildAndParentClassManipulator.php b/src/PhpParser/Node/Manipulator/ChildAndParentClassManipulator.php index 20cf988da25..e08f8cf92bc 100644 --- a/src/PhpParser/Node/Manipulator/ChildAndParentClassManipulator.php +++ b/src/PhpParser/Node/Manipulator/ChildAndParentClassManipulator.php @@ -18,7 +18,7 @@ final class ChildAndParentClassManipulator /** * @var string */ - private const __CONSTRUCT = '__construct'; + private const CONSTRUCT = '__construct'; /** * @var NodeFactory @@ -71,7 +71,7 @@ final class ChildAndParentClassManipulator } // complete parent call for __construct() - if ($parentClassName !== '' && method_exists($parentClassName, self::__CONSTRUCT)) { + if ($parentClassName !== '' && method_exists($parentClassName, self::CONSTRUCT)) { $parentConstructCallNode = $this->nodeFactory->createParentConstructWithParams([]); $classMethod->stmts[] = new Expression($parentConstructCallNode); } @@ -87,55 +87,49 @@ final class ChildAndParentClassManipulator $childClassNodes = $this->classLikeParsedNodesFinder->findChildrenOfClass($className); foreach ($childClassNodes as $childClassNode) { - if ($childClassNode->getMethod(self::__CONSTRUCT) === null) { + $childConstructorClassMethod = $childClassNode->getMethod(self::CONSTRUCT); + if ($childConstructorClassMethod === null) { continue; } - /** @var ClassMethod $childClassConstructorMethodNode */ - $childClassConstructorMethodNode = $childClassNode->getMethod(self::__CONSTRUCT); - // replicate parent parameters - $childClassConstructorMethodNode->params = array_merge( + $childConstructorClassMethod->params = array_merge( $constructorClassMethod->params, - $childClassConstructorMethodNode->params + $childConstructorClassMethod->params ); $parentConstructCallNode = $this->nodeFactory->createParentConstructWithParams( $constructorClassMethod->params ); - $childClassConstructorMethodNode->stmts = array_merge( + $childConstructorClassMethod->stmts = array_merge( [new Expression($parentConstructCallNode)], - (array) $childClassConstructorMethodNode->stmts + (array) $childConstructorClassMethod->stmts ); } } private function completeParentConstructorBasedOnParentNode(Class_ $parentClassNode, ClassMethod $classMethod): void { - // iterate up? $firstParentConstructMethodNode = $this->findFirstParentConstructor($parentClassNode); if ($firstParentConstructMethodNode === null) { return; } - if ($firstParentConstructMethodNode->params === []) { - return; - } - // replicate parent parameters $classMethod->params = array_merge($firstParentConstructMethodNode->params, $classMethod->params); $parentConstructCallNode = $this->nodeFactory->createParentConstructWithParams( $firstParentConstructMethodNode->params ); + $classMethod->stmts[] = new Expression($parentConstructCallNode); } private function findFirstParentConstructor(Class_ $classNode): ?ClassMethod { while ($classNode !== null) { - $constructMethodNode = $classNode->getMethod(self::__CONSTRUCT); + $constructMethodNode = $classNode->getMethod(self::CONSTRUCT); if ($constructMethodNode !== null) { return $constructMethodNode; } diff --git a/src/PhpParser/Node/Manipulator/ClassDependencyManipulator.php b/src/PhpParser/Node/Manipulator/ClassDependencyManipulator.php index 7bdb8ab5fdb..b5ffa4a707b 100644 --- a/src/PhpParser/Node/Manipulator/ClassDependencyManipulator.php +++ b/src/PhpParser/Node/Manipulator/ClassDependencyManipulator.php @@ -77,7 +77,7 @@ final class ClassDependencyManipulator ): void { $constructorMethod = $classNode->getMethod(self::CONSTRUCTOR); - /** @var ClassMethod $constructorMethod */ + /** @var ClassMethod|null $constructorMethod */ if ($constructorMethod !== null) { $this->classMethodAssignManipulator->addParameterAndAssignToMethod( $constructorMethod, diff --git a/tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/Fixture/fixture6.php.inc b/tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/Fixture/add_parent_construct.php.inc similarity index 70% rename from tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/Fixture/fixture6.php.inc rename to tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/Fixture/add_parent_construct.php.inc index eabb88cd9c4..ff1ec5ee473 100644 --- a/tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/Fixture/fixture6.php.inc +++ b/tests/Rector/Architecture/DependencyInjection/AnnotatedPropertyInjectToConstructorInjectionRector/Fixture/add_parent_construct.php.inc @@ -4,7 +4,7 @@ namespace Rector\Core\Tests\Rector\Architecture\DependencyInjection\AnnotatedPro use Rector\Core\Tests\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector\Source\SomeProductWithParent; -class ClassWithInjects6 +class ClassWithParentWithConstructor extends SomeClassWithParent { /** * @var SomeProductWithParent @@ -13,6 +13,16 @@ class ClassWithInjects6 protected $property; } +class SomeClassWithParent +{ + private $value; + + public function __construct() + { + $this->value = '123'; + } +} + ?> ----- property = $property; + parent::__construct(); + } +} + +class SomeClassWithParent +{ + private $value; + + public function __construct() + { + $this->value = '123'; } }