mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 20:22:24 +00:00
add different scope support to RemoveDoubleAssignRector
This commit is contained in:
parent
3f2f64de22
commit
cbddceb06a
|
@ -77,7 +77,7 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($this->shouldSkipDueToForeachOverride($node, $previousExpression)) {
|
||||
if ($this->shouldSkipForDifferentScope($node, $previousExpression)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -110,4 +110,40 @@ CODE_SAMPLE
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function shouldSkipForDifferenceParent(Node $firstNode, Node $secondNode): bool
|
||||
{
|
||||
$firstNodeParent = $this->betterNodeFinder->findFirstParentInstanceOf(
|
||||
$firstNode,
|
||||
[Node\Stmt\Foreach_::class, Node\Stmt\If_::class, Node\Stmt\While_::class, Node\Stmt\Do_::class]
|
||||
);
|
||||
|
||||
$secondNodeParent = $this->betterNodeFinder->findFirstParentInstanceOf(
|
||||
$secondNode,
|
||||
[Node\Stmt\Foreach_::class, Node\Stmt\If_::class, Node\Stmt\While_::class, Node\Stmt\Do_::class]
|
||||
);
|
||||
|
||||
if ($firstNodeParent === null || $secondNodeParent === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->areNodesEqual($firstNodeParent, $secondNodeParent)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function shouldSkipForDifferentScope(Assign $assign, Node $anotherNode): bool
|
||||
{
|
||||
if ($this->shouldSkipDueToForeachOverride($assign, $anotherNode)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->shouldSkipForDifferenceParent($assign, $anotherNode)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\Assign\RemoveDoubleAssignRector\Fixture;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
|
||||
class KeepPropertyAssignInDifferentIfs
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isBoolAssert = false;
|
||||
|
||||
public function resolveBoolMethodName(string $name): string
|
||||
{
|
||||
if ($name === 'assertSame') {
|
||||
$this->isBoolAssert = true;
|
||||
}
|
||||
|
||||
if ($name === 'assertNotSame') {
|
||||
$this->isBoolAssert = true;
|
||||
}
|
||||
|
||||
return $this->isBoolAssert;
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@ final class RemoveDoubleAssignRectorTest extends AbstractRectorTestCase
|
|||
__DIR__ . '/Fixture/keep_dim_assign.php.inc',
|
||||
__DIR__ . '/Fixture/property_assign.php.inc',
|
||||
__DIR__ . '/Fixture/keep_array_reset.php.inc',
|
||||
__DIR__ . '/Fixture/keep_property_assign_in_different_ifs.php.inc',
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
@ -143,3 +143,5 @@ parameters:
|
|||
- '#Unreachable statement \- code above always terminates#'
|
||||
- '#Method Rector\\NodeTypeResolver\\NodeVisitor\\(.*?)\:\:enterNode\(\) should return int\|PhpParser\\Node\|void\|null but return statement is missing#'
|
||||
- '#Negated boolean expression is always true#'
|
||||
- '#Strict comparison using \=\=\= between PhpParser\\Node and null will always evaluate to false#'
|
||||
|
||||
|
|
|
@ -27,14 +27,27 @@ final class BetterNodeFinder
|
|||
$this->betterStandardPrinter = $betterStandardPrinter;
|
||||
}
|
||||
|
||||
public function findFirstParentInstanceOf(Node $node, string $type): ?Node
|
||||
/**
|
||||
* @param string|string[] $type
|
||||
*/
|
||||
public function findFirstParentInstanceOf(Node $node, $type): ?Node
|
||||
{
|
||||
if (! is_array($type)) {
|
||||
$type = [$type];
|
||||
}
|
||||
|
||||
/** @var Node|null $parentNode */
|
||||
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
|
||||
|
||||
if ($parentNode === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
do {
|
||||
if ($parentNode instanceof $type) {
|
||||
if ($this->isTypes($parentNode, $type)) {
|
||||
return $parentNode;
|
||||
}
|
||||
|
||||
if ($parentNode === null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -145,4 +158,18 @@ final class BetterNodeFinder
|
|||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $types
|
||||
*/
|
||||
private function isTypes(Node $node, array $types): bool
|
||||
{
|
||||
foreach ($types as $type) {
|
||||
if (is_a($node, $type, true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user