From ef2062bda9db741accb4c9cdebffbee63f508cfe Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 24 May 2023 11:09:13 +0700 Subject: [PATCH 1/3] [NodeTypeResolver] Reduce parent attribute usage on PHPStanNodeScopeResolver --- .../PHPStan/Scope/PHPStanNodeScopeResolver.php | 18 +++++------------- .../skip_assign_closure_bind_reference.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_assign_closure_bind_reference.php diff --git a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index e84670846ab..1891f0c9a6c 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -13,7 +13,6 @@ use PhpParser\Node\Expr\AssignOp; use PhpParser\Node\Expr\BinaryOp; use PhpParser\Node\Expr\Cast; -use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; @@ -47,7 +46,6 @@ use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\NodeAnalyzer\ClassAnalyzer; -use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\Util\Reflection\PrivatesAccessor; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -79,7 +77,6 @@ public function __construct( private readonly ScopeFactory $scopeFactory, private readonly PrivatesAccessor $privatesAccessor, private readonly NodeNameResolver $nodeNameResolver, - private readonly BetterNodeFinder $betterNodeFinder, private readonly ClassAnalyzer $classAnalyzer ) { $this->nodeTraverser = new NodeTraverser(); @@ -236,25 +233,20 @@ public function processNodes( private function setChildOfUnreachableStatementNodeAttribute(Stmt $stmt): void { - if ($stmt->getAttribute(AttributeKey::IS_UNREACHABLE) === true) { + if ($stmt->getAttribute(AttributeKey::IS_UNREACHABLE) !== true) { return; } - $parentStmt = $stmt->getAttribute(AttributeKey::PARENT_NODE); - if (! $parentStmt instanceof Node) { + if (! $stmt instanceof StmtsAwareInterface) { return; } - if ($parentStmt instanceof Closure) { - $parentStmt = $this->betterNodeFinder->resolveCurrentStatement($parentStmt); - } - - if (! $parentStmt instanceof Stmt) { + if ($stmt->stmts === null) { return; } - if ($parentStmt->getAttribute(AttributeKey::IS_UNREACHABLE) === true) { - $stmt->setAttribute(AttributeKey::IS_UNREACHABLE, true); + foreach ($stmt->stmts as $childStmt) { + $childStmt->setAttribute(AttributeKey::IS_UNREACHABLE, true); } } diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_assign_closure_bind_reference.php b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_assign_closure_bind_reference.php new file mode 100644 index 00000000000..9d872d078f5 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_assign_closure_bind_reference.php @@ -0,0 +1,12 @@ + $container->locked, null, $container)($container); + $containerLocked = false; + } +} From 517815b23ea2eac6f8dc2f0c7fd2367ed9e1d70f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 24 May 2023 11:11:29 +0700 Subject: [PATCH 2/3] increase kernel key --- src/Kernel/RectorKernel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kernel/RectorKernel.php b/src/Kernel/RectorKernel.php index 71d933c0d47..e6f329d8380 100644 --- a/src/Kernel/RectorKernel.php +++ b/src/Kernel/RectorKernel.php @@ -17,7 +17,7 @@ final class RectorKernel /** * @var string */ - private const CACHE_KEY = 'v24'; + private const CACHE_KEY = 'v25'; private ContainerInterface|null $container = null; From e8366d5481805863c593bd7940f50db60d470e4b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 24 May 2023 11:12:31 +0700 Subject: [PATCH 3/3] clean up --- .../Fixture/skip_assign_closure_bind_reference.php | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_assign_closure_bind_reference.php diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_assign_closure_bind_reference.php b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_assign_closure_bind_reference.php deleted file mode 100644 index 9d872d078f5..00000000000 --- a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_assign_closure_bind_reference.php +++ /dev/null @@ -1,12 +0,0 @@ - $container->locked, null, $container)($container); - $containerLocked = false; - } -}