Skip to content

Commit b1899f4

Browse files
authored
[DeadCode] Skip with assign to call with target has #[NoDiscard] attribute on RemoveUnusedVariableAssignRector (#7950)
* [DeadCode] Skip with assign to call with target has #[NoDiscard] attribute * [DeadCode] Skip with assign to call with target has #[NoDiscard] attribute * [DeadCode] Skip with assign to call with target has #[NoDiscard] attribute * [DeadCode] Skip with assign to call with target has #[NoDiscard] attribute * [DeadCode] Skip with assign to call with target has #[NoDiscard] attribute * rename test
1 parent 11b6e02 commit b1899f4

File tree

4 files changed

+42
-4
lines changed

4 files changed

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

rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use PhpParser\Node\Expr\Closure;
1313
use PhpParser\Node\Expr\FuncCall;
1414
use PhpParser\Node\Expr\Include_;
15+
use PhpParser\Node\Expr\MethodCall;
16+
use PhpParser\Node\Expr\New_;
17+
use PhpParser\Node\Expr\NullsafeMethodCall;
18+
use PhpParser\Node\Expr\StaticCall;
1519
use PhpParser\Node\Expr\Variable;
1620
use PhpParser\Node\Stmt;
1721
use PhpParser\Node\Stmt\Class_;
@@ -25,6 +29,8 @@
2529
use Rector\NodeAnalyzer\VariableAnalyzer;
2630
use Rector\NodeManipulator\StmtsManipulator;
2731
use Rector\Php\ReservedKeywordAnalyzer;
32+
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
33+
use Rector\PhpParser\AstResolver;
2834
use Rector\PhpParser\Enum\NodeGroup;
2935
use Rector\PhpParser\Node\BetterNodeFinder;
3036
use Rector\Rector\AbstractRector;
@@ -42,7 +48,9 @@ public function __construct(
4248
private readonly SideEffectNodeDetector $sideEffectNodeDetector,
4349
private readonly VariableAnalyzer $variableAnalyzer,
4450
private readonly BetterNodeFinder $betterNodeFinder,
45-
private readonly StmtsManipulator $stmtsManipulator
51+
private readonly StmtsManipulator $stmtsManipulator,
52+
private readonly AstResolver $astResolver,
53+
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer
4654
) {
4755
}
4856

@@ -265,6 +273,18 @@ function (Node $subNode) use (&$refVariableNames) {
265273
continue;
266274
}
267275

276+
if ($assign->expr instanceof FuncCall
277+
|| $assign->expr instanceof StaticCall
278+
|| $assign->expr instanceof MethodCall
279+
|| $assign->expr instanceof New_
280+
|| $assign->expr instanceof NullsafeMethodCall) {
281+
$targetCall = $this->astResolver->resolveClassMethodOrFunctionFromCall($assign->expr);
282+
if (($targetCall instanceof ClassMethod || $targetCall instanceof Function_)
283+
&& $this->phpAttributeAnalyzer->hasPhpAttribute($targetCall, 'NoDiscard')) {
284+
continue;
285+
}
286+
}
287+
268288
$assignedVariableNamesByStmtPosition[$key] = $variableName;
269289
}
270290

rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Scalar\String_;
1313
use PhpParser\Node\Stmt\ClassLike;
1414
use PhpParser\Node\Stmt\ClassMethod;
15+
use PhpParser\Node\Stmt\Function_;
1516
use PhpParser\Node\Stmt\Property;
1617
use Rector\NodeNameResolver\NodeNameResolver;
1718
use Rector\PhpAttribute\Enum\DocTagNodeState;
@@ -23,7 +24,7 @@ public function __construct(
2324
) {
2425
}
2526

26-
public function hasPhpAttribute(Property | ClassLike | ClassMethod | Param $node, string $attributeClass): bool
27+
public function hasPhpAttribute(Property | ClassLike | ClassMethod | Function_ | Param $node, string $attributeClass): bool
2728
{
2829
foreach ($node->attrGroups as $attrGroup) {
2930
foreach ($attrGroup->attrs as $attribute) {
@@ -41,7 +42,7 @@ public function hasPhpAttribute(Property | ClassLike | ClassMethod | Param $node
4142
/**
4243
* @param string[] $attributeClasses
4344
*/
44-
public function hasPhpAttributes(Property | ClassLike | ClassMethod | Param $node, array $attributeClasses): bool
45+
public function hasPhpAttributes(Property | ClassLike | ClassMethod | Function_ | Param $node, array $attributeClasses): bool
4546
{
4647
foreach ($attributeClasses as $attributeClass) {
4748
if ($this->hasPhpAttribute($node, $attributeClass)) {

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)