diff --git a/rules/dead-code/tests/Rector/Class_/RemoveSetterOnlyPropertyAndMethodCallRector/Fixture/deal_with_property_fetches.php.inc b/rules/dead-code/tests/Rector/Class_/RemoveSetterOnlyPropertyAndMethodCallRector/Fixture/deal_with_property_fetches.php.inc index d356822d27ff..99a5af01e595 100644 --- a/rules/dead-code/tests/Rector/Class_/RemoveSetterOnlyPropertyAndMethodCallRector/Fixture/deal_with_property_fetches.php.inc +++ b/rules/dead-code/tests/Rector/Class_/RemoveSetterOnlyPropertyAndMethodCallRector/Fixture/deal_with_property_fetches.php.inc @@ -26,7 +26,7 @@ namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodC class MyClass { - public function __construct($unused) + public function __construct() { } } diff --git a/rules/dead-code/tests/Rector/Class_/RemoveSetterOnlyPropertyAndMethodCallRector/Fixture/in_constructor.php.inc b/rules/dead-code/tests/Rector/Class_/RemoveSetterOnlyPropertyAndMethodCallRector/Fixture/in_constructor.php.inc index a53ebdbbf0c5..9c8f6e23ab51 100644 --- a/rules/dead-code/tests/Rector/Class_/RemoveSetterOnlyPropertyAndMethodCallRector/Fixture/in_constructor.php.inc +++ b/rules/dead-code/tests/Rector/Class_/RemoveSetterOnlyPropertyAndMethodCallRector/Fixture/in_constructor.php.inc @@ -20,7 +20,7 @@ namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodC class InConstructor { - public function __construct($name) + public function __construct() { } } diff --git a/rules/dead-code/tests/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/constructor_only.php.inc b/rules/dead-code/tests/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/constructor_only.php.inc index 8120561d6f42..54032b7bd865 100644 --- a/rules/dead-code/tests/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/constructor_only.php.inc +++ b/rules/dead-code/tests/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/constructor_only.php.inc @@ -23,7 +23,7 @@ namespace Rector\DeadCode\Tests\Rector\Property\RemoveUnusedPrivatePropertyRecto class ConstructorOnly { - public function __construct(int $contentFinder) + public function __construct() { } } diff --git a/src/Rector/AbstractRector/ComplexRemovalTrait.php b/src/Rector/AbstractRector/ComplexRemovalTrait.php index f2f174f28722..fd0ca8b7a12c 100644 --- a/src/Rector/AbstractRector/ComplexRemovalTrait.php +++ b/src/Rector/AbstractRector/ComplexRemovalTrait.php @@ -12,6 +12,7 @@ use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\StaticPropertyFetch; +use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Property; @@ -19,6 +20,7 @@ use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\PhpParser\Node\Commander\NodeRemovingCommander; use Rector\Core\PhpParser\Node\Manipulator\PropertyManipulator; +use Rector\Core\PhpParser\Printer\BetterStandardPrinter; use Rector\DeadCode\NodeManipulator\LivingCodeManipulator; use Rector\NodeCollector\NodeCollector\ParsedNodeCollector; use Rector\NodeCollector\NodeFinder\FunctionLikeParsedNodesFinder; @@ -41,6 +43,11 @@ trait ComplexRemovalTrait */ protected $livingCodeManipulator; + /** + * @var BetterStandardPrinter + */ + protected $betterStandardPrinter; + /** * @var PropertyManipulator */ @@ -52,11 +59,13 @@ trait ComplexRemovalTrait public function autowireComplextRemovalTrait( PropertyManipulator $propertyManipulator, ParsedNodeCollector $parsedNodeCollector, - LivingCodeManipulator $livingCodeManipulator + LivingCodeManipulator $livingCodeManipulator, + BetterStandardPrinter $betterStandardPrinter ): void { $this->parsedNodeCollector = $parsedNodeCollector; $this->propertyManipulator = $propertyManipulator; $this->livingCodeManipulator = $livingCodeManipulator; + $this->betterStandardPrinter = $betterStandardPrinter; } abstract protected function removeNode(Node $node): void; @@ -91,15 +100,19 @@ protected function removePropertyAndUsages( continue; } + // remove assigns $assign = $this->resolveAssign($propertyFetch); - $this->removeAssignNode($assign); + + $this->removeConstructorDependency($assign); } if ($shouldKeepProperty) { return; } + // remove __contruct param + /** @var Property $property */ $property = $propertyProperty->getAttribute(AttributeKey::PARENT_NODE); $this->removeNode($propertyProperty); @@ -185,4 +198,31 @@ private function resolveAssign(Expr $expr): Assign return $assign; } + + private function removeConstructorDependency(Assign $assign): void + { + $methodName = $assign->getAttribute(AttributeKey::METHOD_NAME); + if ($methodName !== '__construct') { + return; + } + + $class = $assign->getAttribute(AttributeKey::CLASS_NODE); + if (! $class instanceof Class_) { + return; + } + + /** @var Class_|null $class */ + $constructClassMethod = $class->getMethod('__construct'); + if ($constructClassMethod === null) { + return; + } + + foreach ($constructClassMethod->getParams() as $param) { + if (! $this->betterStandardPrinter->areNodesWithoutCommentsEqual($param->var, $assign->expr)) { + continue; + } + + $this->removeNode($param); + } + } }