Skip to content

Commit 76909bb

Browse files
committed
[dead-code] Fix RemoveUnusedVariableAssignRector, allow for SplFileInfo as cleanup on purpose for gc
1 parent 0b70133 commit 76909bb

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;
4+
5+
final class SkipNullResetOfObject
6+
{
7+
public function process(string $path): bool
8+
{
9+
try {
10+
$file = new \SplFileObject($path);
11+
} catch (\Exception $e) {
12+
return false;
13+
}
14+
15+
$file->seek(0);
16+
echo $file->current();
17+
18+
// Close the file
19+
$file = null;
20+
21+
return true;
22+
}
23+
}

‎rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Rector\Php\ReservedKeywordAnalyzer;
2929
use Rector\PhpParser\Enum\NodeGroup;
3030
use Rector\PhpParser\Node\BetterNodeFinder;
31+
use Rector\PhpParser\Node\Value\ValueResolver;
3132
use Rector\Rector\AbstractRector;
3233
use Rector\ValueObject\MethodName;
3334
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -45,6 +46,7 @@ public function __construct(
4546
private readonly BetterNodeFinder $betterNodeFinder,
4647
private readonly StmtsManipulator $stmtsManipulator,
4748
private readonly NoDiscardCallAnalyzer $noDiscardCallAnalyzer,
49+
private readonly ValueResolver $valueResolver,
4850
) {
4951
}
5052

@@ -116,6 +118,10 @@ public function refactor(Node $node): null|ClassMethod|Function_
116118
continue;
117119
}
118120

121+
if ($this->isNullResetOfObject($assign)) {
122+
continue;
123+
}
124+
119125
if ($this->hasCallLikeInAssignExpr($assign)) {
120126
// clean safely
121127
$cleanAssignedExpr = $this->cleanCastedExpr($assign->expr);
@@ -136,6 +142,16 @@ public function refactor(Node $node): null|ClassMethod|Function_
136142
return null;
137143
}
138144

145+
private function isNullResetOfObject(Assign $assign): bool
146+
{
147+
// resetting an object variable to null releases the held resource, e.g. $file = null on a SplFileObject
148+
if (! $this->valueResolver->isNull($assign->expr)) {
149+
return false;
150+
}
151+
152+
return $this->getType($assign->var) instanceof ObjectType;
153+
}
154+
139155
private function isObjectWithDestructMethod(Expr $expr): bool
140156
{
141157
$exprType = $this->getType($expr);

‎rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Php80\NodeAnalyzer;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ArrayDimFetch;
89
use PhpParser\Node\Expr\Assign;
910
use PhpParser\Node\Expr\PropertyFetch;
1011
use PhpParser\Node\Expr\Variable;

0 commit comments

Comments
 (0)