Skip to content

Commit 538dfac

Browse files
[DeadCode] Skip on object with __destruct method on RemoveUnusedVariableAssignRector (#7711)
* [DeadCode] Skip on object with __destruct method on RemoveUnusedVariableAssignRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 943b146 commit 538dfac

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;
4+
5+
use Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Source\SomeLock;
6+
7+
final class SkipObjectWithDestructMethod
8+
{
9+
public function run()
10+
{
11+
$lock = $this->createLock();
12+
13+
echo 'foobar';
14+
}
15+
16+
private function createLock(): SomeLock
17+
{
18+
return new SomeLock();
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Source;
6+
7+
class SomeLock
8+
{
9+
public function __destruct()
10+
{
11+
// Do important things
12+
}
13+
}

rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
use PhpParser\Node\Stmt\Expression;
2020
use PhpParser\Node\Stmt\Function_;
2121
use PhpParser\NodeVisitor;
22+
use PHPStan\Reflection\ClassReflection;
23+
use PHPStan\Type\ObjectType;
2224
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
2325
use Rector\NodeAnalyzer\VariableAnalyzer;
2426
use Rector\NodeManipulator\StmtsManipulator;
2527
use Rector\Php\ReservedKeywordAnalyzer;
2628
use Rector\PhpParser\Enum\NodeGroup;
2729
use Rector\PhpParser\Node\BetterNodeFinder;
2830
use Rector\Rector\AbstractRector;
31+
use Rector\ValueObject\MethodName;
2932
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
3033
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
3134

@@ -107,6 +110,10 @@ public function refactor(Node $node): null|ClassMethod|Function_
107110
/** @var Assign $assign */
108111
$assign = $currentStmt->expr;
109112

113+
if ($this->isObjectWithDestructMethod($assign->expr)) {
114+
continue;
115+
}
116+
110117
if ($this->hasCallLikeInAssignExpr($assign)) {
111118
// clean safely
112119
$cleanAssignedExpr = $this->cleanCastedExpr($assign->expr);
@@ -127,6 +134,21 @@ public function refactor(Node $node): null|ClassMethod|Function_
127134
return null;
128135
}
129136

137+
private function isObjectWithDestructMethod(Expr $expr): bool
138+
{
139+
$exprType = $this->getType($expr);
140+
if (! $exprType instanceof ObjectType) {
141+
return false;
142+
}
143+
144+
$classReflection = $exprType->getClassReflection();
145+
if (! $classReflection instanceof ClassReflection) {
146+
return false;
147+
}
148+
149+
return $classReflection->hasNativeMethod(MethodName::DESTRUCT);
150+
}
151+
130152
private function cleanCastedExpr(Expr $expr): Expr
131153
{
132154
if (! $expr instanceof Cast) {

0 commit comments

Comments
 (0)