From 29be2b49a28daeefd4d7085944544eefb0e47db6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 3 Dec 2025 18:54:00 +0700 Subject: [PATCH 1/2] [DeadCode] Skip on object with __destruct method on RemoveUnusedVariableAssignRector --- .../skip_object_with_destruct_method.php.inc | 20 +++++++++++++++++ .../Source/SomeLock.php | 13 +++++++++++ .../RemoveUnusedVariableAssignRector.php | 22 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_object_with_destruct_method.php.inc create mode 100644 rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Source/SomeLock.php diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_object_with_destruct_method.php.inc b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_object_with_destruct_method.php.inc new file mode 100644 index 00000000000..64b52e44c59 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/skip_object_with_destruct_method.php.inc @@ -0,0 +1,20 @@ +createLock(); + + echo 'foobar'; + } + + private function createLock(): SomeLock + { + return new SomeLock(); + } +} \ No newline at end of file diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Source/SomeLock.php b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Source/SomeLock.php new file mode 100644 index 00000000000..4b80744d84d --- /dev/null +++ b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Source/SomeLock.php @@ -0,0 +1,13 @@ +expr; + if ($this->isObjectWithDestructMethod($assign->expr)) { + continue; + } + if ($this->hasCallLikeInAssignExpr($assign)) { // clean safely $cleanAssignedExpr = $this->cleanCastedExpr($assign->expr); @@ -127,6 +134,21 @@ public function refactor(Node $node): null|ClassMethod|Function_ return null; } + private function isObjectWithDestructMethod(Expr $expr): bool + { + $exprType = $this->getType($expr); + if (! $exprType instanceof ObjectType) { + return false; + } + + $classReflection = $exprType->getClassReflection(); + if (! $classReflection instanceof ClassReflection) { + return false; + } + + return $classReflection->hasNativeMethod(MethodName::DESTRUCT); + } + private function cleanCastedExpr(Expr $expr): Expr { if (! $expr instanceof Cast) { From 121d0f09b76387ee6efd243280be4732ed9a0508 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 3 Dec 2025 11:57:04 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- .../Scope/NodeVisitor/PhpVersionConditionNodeVisitor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/PhpVersionConditionNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/PhpVersionConditionNodeVisitor.php index 7127ae5deb2..b81f526c00d 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/PhpVersionConditionNodeVisitor.php +++ b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/PhpVersionConditionNodeVisitor.php @@ -27,7 +27,7 @@ public function enterNode(Node $node): ?Node if (($node instanceof Ternary || $node instanceof If_) && $this->hasVersionCompareCond($node)) { if ($node instanceof Ternary) { $nodes = [$node->else]; - if ($node->if instanceof \PhpParser\Node) { + if ($node->if instanceof Node) { $nodes[] = $node->if; } } else {