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) { 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 {