Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;

class SkipAssignNoDiscardMethodCall
{
public function run()
{
$value = $this->some_call();
}

#[\NoDiscard]
private function some_call()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
Expand All @@ -25,6 +29,8 @@
use Rector\NodeAnalyzer\VariableAnalyzer;
use Rector\NodeManipulator\StmtsManipulator;
use Rector\Php\ReservedKeywordAnalyzer;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\PhpParser\AstResolver;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
Expand All @@ -42,7 +48,9 @@ public function __construct(
private readonly SideEffectNodeDetector $sideEffectNodeDetector,
private readonly VariableAnalyzer $variableAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly StmtsManipulator $stmtsManipulator
private readonly StmtsManipulator $stmtsManipulator,
private readonly AstResolver $astResolver,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer
) {
}

Expand Down Expand Up @@ -265,6 +273,18 @@ function (Node $subNode) use (&$refVariableNames) {
continue;
}

if ($assign->expr instanceof FuncCall
|| $assign->expr instanceof StaticCall
|| $assign->expr instanceof MethodCall
|| $assign->expr instanceof New_
|| $assign->expr instanceof NullsafeMethodCall) {
Comment on lines +276 to +280
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about instanceof CallLike?

Copy link
Copy Markdown
Member Author

@samsonasik samsonasik Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That cause phpstan notice, see

https://github.com/rectorphp/rector-src/actions/runs/23905789349/job/69714124150#step:5:19

use the required instance fix it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see

$targetCall = $this->astResolver->resolveClassMethodOrFunctionFromCall($assign->expr);
if (($targetCall instanceof ClassMethod || $targetCall instanceof Function_)
&& $this->phpAttributeAnalyzer->hasPhpAttribute($targetCall, 'NoDiscard')) {
continue;
}
}

$assignedVariableNamesByStmtPosition[$key] = $variableName;
}

Expand Down
5 changes: 3 additions & 2 deletions rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpAttribute\Enum\DocTagNodeState;
Expand All @@ -23,7 +24,7 @@ public function __construct(
) {
}

public function hasPhpAttribute(Property | ClassLike | ClassMethod | Param $node, string $attributeClass): bool
public function hasPhpAttribute(Property | ClassLike | ClassMethod | Function_ | Param $node, string $attributeClass): bool
{
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attribute) {
Expand All @@ -41,7 +42,7 @@ public function hasPhpAttribute(Property | ClassLike | ClassMethod | Param $node
/**
* @param string[] $attributeClasses
*/
public function hasPhpAttributes(Property | ClassLike | ClassMethod | Param $node, array $attributeClasses): bool
public function hasPhpAttributes(Property | ClassLike | ClassMethod | Function_ | Param $node, array $attributeClasses): bool
{
foreach ($attributeClasses as $attributeClass) {
if ($this->hasPhpAttribute($node, $attributeClass)) {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function (Node $node) use ($classLikeName, $methodName, &$classMethod): bool {
}

public function resolveClassMethodOrFunctionFromCall(
FuncCall | StaticCall | MethodCall | New_ $call
FuncCall | StaticCall | MethodCall | New_ | NullsafeMethodCall $call
): ClassMethod | Function_ | null {
if ($call instanceof FuncCall) {
return $this->resolveFunctionFromFuncCall($call);
Expand Down
Loading