Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodC

class MyClass
{
public function __construct($unused)
public function __construct()
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodC

class InConstructor
{
public function __construct($name)
public function __construct()
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Rector\DeadCode\Tests\Rector\Property\RemoveUnusedPrivatePropertyRecto

class ConstructorOnly
{
public function __construct(int $contentFinder)
public function __construct()
{
}
}
Expand Down
44 changes: 42 additions & 2 deletions src/Rector/AbstractRector/ComplexRemovalTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
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;
use PhpParser\Node\Stmt\PropertyProperty;
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;
Expand All @@ -41,6 +43,11 @@ trait ComplexRemovalTrait
*/
protected $livingCodeManipulator;

/**
* @var BetterStandardPrinter
*/
protected $betterStandardPrinter;

/**
* @var PropertyManipulator
*/
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}