Skip to content

[DeadCode] Avoid scan whole new stmts on RemoveAlwaysTrueIfConditionRector on dynamic variable check#8057

Merged
TomasVotruba merged 7 commits into
mainfrom
clean-up-scan-whole-new-stmts
Jun 20, 2026
Merged

[DeadCode] Avoid scan whole new stmts on RemoveAlwaysTrueIfConditionRector on dynamic variable check#8057
TomasVotruba merged 7 commits into
mainfrom
clean-up-scan-whole-new-stmts

Conversation

@samsonasik

@samsonasik samsonasik commented Jun 20, 2026

Copy link
Copy Markdown
Member

check directly on $scope->getDefinedVariables() seems enough.

Ref #8055 (review)

@samsonasik

Copy link
Copy Markdown
Member Author

Fixed 🎉 /cc @TomasVotruba

Handled with check from $scope->getDefinedVariables().

@samsonasik samsonasik changed the title [DeadCode] Avoid scan whole new stmts on RemoveAlwaysTrueIfConditionRector [DeadCode] Avoid scan whole new stmts on RemoveAlwaysTrueIfConditionRector on dynamic variable check Jun 20, 2026
@samsonasik samsonasik requested a review from TomasVotruba June 20, 2026 09:41
@samsonasik

Copy link
Copy Markdown
Member Author

@TomasVotruba ready 👍

continue;
}

$variableType = $scope->getVariableType($definedVariable);

@TomasVotruba TomasVotruba Jun 20, 2026

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.

This call would be more expensive than simple $node->name instanceof Expr

We need a better solution without type resolution.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this only check defined variables inside the current scope, eg: method, so should be cheap process as scoped.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've cleaned up check hasVariableType() in loop as always defined on getDefinedVariables() usage d32d28d

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've moved check on defined variables on last resort after other check d40ba5d

should be cheaper now as last resort, and this is already only lookup variables on this scope 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@TomasVotruba I've added fixture to proof that the getDefinedVariables() only lookup on scoped method ec3414d

Different method will use different scope so it proceed 👍

Comment on lines -178 to -184
private function hasDynamicVariable(): bool
{
return (bool) $this->betterNodeFinder->findFirst(
$this->getFile()
->getNewStmts(),
static fn (Node $node): bool => $node instanceof Variable && $node->name instanceof Expr
);

@TomasVotruba TomasVotruba Jun 20, 2026

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.

This structure is indeed weird, should be either cached per file or replaced by check inside aprent node, e.g. ClassMethod, Function_ or FileNode

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

that's seems will make too much clutter imo, as the flag need to be reset on enter another scope.

@samsonasik samsonasik requested a review from TomasVotruba June 20, 2026 10:20
@samsonasik

Copy link
Copy Markdown
Member Author

@TomasVotruba I've added fixture to proof that the getDefinedVariables() only lookup on scoped method ec3414d

Different method will use different scope so it proceed 👍

Should be ready to merge 👍

@samsonasik samsonasik force-pushed the clean-up-scan-whole-new-stmts branch from ec3414d to ff19731 Compare June 20, 2026 21:55
@TomasVotruba

Copy link
Copy Markdown
Member

Looks good now, thank you 👍

@TomasVotruba TomasVotruba merged commit 580b374 into main Jun 20, 2026
67 checks passed
@TomasVotruba TomasVotruba deleted the clean-up-scan-whole-new-stmts branch June 20, 2026 22:26
@samsonasik

Copy link
Copy Markdown
Member Author

@TomasVotruba can we have new immediate release for it? Thank you.

@TomasVotruba

Copy link
Copy Markdown
Member

@samsonasik Just made one, so not yet. I want to add few fixes first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants