Skip to content
Closed
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 @@ -4,6 +4,7 @@

namespace Rector\DeadCode\Rector\Class_;

use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
Expand Down Expand Up @@ -162,6 +163,9 @@ private function processClassStmts(Node $node, array $setterOnlyPropertiesAndMet
if ($this->assignManipulator->isLocalPropertyAssign($node)) {
/** @var Assign $node */
$propertyFetch = $node->var;
if ($propertyFetch instanceof ArrayDimFetch) {
$propertyFetch = $propertyFetch->var;
}
/** @var PropertyFetch $propertyFetch */
if ($this->isNames($propertyFetch->name, $propertyNames)) {
$this->removeNode($node);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector\deal_with_property_of_methodcall;

class MyClass
{
private $unused;
private $prop;

public function __construct($unused)
{
$this->unused = $unused;
}

public function foo()
{
$this->prop[] = 'foo';

}
}
?>
-----
<?php

namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector\deal_with_property_of_methodcall;

class MyClass
{
private $prop;
public function __construct($unused)
{
}
public function foo()
{
$this->prop[] = 'foo';

}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector\node_removal_on_non_expression;

class SomeClass
{
/** @var */
private $foo;

public function setFoo($foo)
{
return $this->foo = $foo;
}

}
?>
-----
<?php
namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector\node_removal_on_non_expression;

class SomeClass
{
public function setFoo($foo)
{
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function provideDataForTest(): Iterator
yield [__DIR__ . '/Fixture/keep_static_property.php.inc'];
yield [__DIR__ . '/Fixture/keep_public_property.php.inc'];
yield [__DIR__ . '/Fixture/keep_serializable_object.php.inc'];
yield [__DIR__ . '/Fixture/deal_with_property_fetches.php.inc'];
yield [__DIR__ . '/Fixture/node_removal_on_non_expression.php.inc'];
}

protected function getRectorClass(): string
Expand Down
5 changes: 3 additions & 2 deletions src/PhpParser/Node/Commander/NodeRemovingCommander.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeDumper;
use PhpParser\NodeTraverser;
use Rector\Contract\PhpParser\Node\CommanderInterface;
use Rector\Exception\ShouldNotHappenException;
Expand Down Expand Up @@ -37,8 +38,8 @@ public function addNode(Node $node): void
$this->ensureIsNotPartOfChainMethodCall($node);

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $node instanceof Expression && $parentNode instanceof Expression) {
// only expressions can be removed
if (! $node instanceof Stmt && $parentNode instanceof Expression) {
// only stmts can be removed
$node = $parentNode;
}

Expand Down