Skip to content

Commit dddda67

Browse files
committed
[DeadCode] Skip with assign to call with target has #[NoDiscard] attribute
1 parent febd1cc commit dddda67

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;
4+
5+
class SkipAssignNoDiscardFunctionCall
6+
{
7+
public function run()
8+
{
9+
$value = $this->some_call();
10+
}
11+
12+
#[\NoDiscard]
13+
private function some_call()
14+
{
15+
16+
}
17+
}
18+
19+
?>

rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\Assign;
1010
use PhpParser\Node\Expr\AssignRef;
11+
use PhpParser\Node\Expr\CallLike;
1112
use PhpParser\Node\Expr\Cast;
1213
use PhpParser\Node\Expr\Closure;
1314
use PhpParser\Node\Expr\FuncCall;
@@ -25,6 +26,8 @@
2526
use Rector\NodeAnalyzer\VariableAnalyzer;
2627
use Rector\NodeManipulator\StmtsManipulator;
2728
use Rector\Php\ReservedKeywordAnalyzer;
29+
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
30+
use Rector\PhpParser\AstResolver;
2831
use Rector\PhpParser\Enum\NodeGroup;
2932
use Rector\PhpParser\Node\BetterNodeFinder;
3033
use Rector\Rector\AbstractRector;
@@ -42,7 +45,9 @@ public function __construct(
4245
private readonly SideEffectNodeDetector $sideEffectNodeDetector,
4346
private readonly VariableAnalyzer $variableAnalyzer,
4447
private readonly BetterNodeFinder $betterNodeFinder,
45-
private readonly StmtsManipulator $stmtsManipulator
48+
private readonly StmtsManipulator $stmtsManipulator,
49+
private readonly AstResolver $astResolver,
50+
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer
4651
) {
4752
}
4853

@@ -265,6 +270,14 @@ function (Node $subNode) use (&$refVariableNames) {
265270
continue;
266271
}
267272

273+
if ($assign->expr instanceof CallLike) {
274+
$targetCall = $this->astResolver->resolveClassMethodOrFunctionFromCall($assign->expr);
275+
if (($targetCall instanceof ClassMethod || $targetCall instanceof Function_)
276+
&& $this->phpAttributeAnalyzer->hasPhpAttribute($targetCall, 'NoDiscard')) {
277+
continue;
278+
}
279+
}
280+
268281
$assignedVariableNamesByStmtPosition[$key] = $variableName;
269282
}
270283

src/PhpParser/AstResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function (Node $node) use ($classLikeName, $methodName, &$classMethod): bool {
112112
}
113113

114114
public function resolveClassMethodOrFunctionFromCall(
115-
FuncCall | StaticCall | MethodCall | New_ $call
115+
FuncCall | StaticCall | MethodCall | New_ | NullsafeMethodCall $call
116116
): ClassMethod | Function_ | null {
117117
if ($call instanceof FuncCall) {
118118
return $this->resolveFunctionFromFuncCall($call);

0 commit comments

Comments
 (0)