Skip to content

[perf] Add PHPStan rule to catch ScopeFetcher::fetch() before a scope-free guard - #8584

Merged
TomasVotruba merged 2 commits into
mainfrom
perf-rule-scope-fetch-guard
Oct 10, 2026
Merged

TomasVotruba merged 2 commits into
mainfrom
perf-rule-scope-fetch-guard

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Adds NoScopeFetchBeforeCheapGuardRule under utils/phpstan, registered in phpstan.neon, to catch the perf shape fixed across #8579 / #8580 / #8582 before it lands again.

It flags a refactor() method where $scope = ScopeFetcher::fetch($node); is immediately followed by an if (...) { return null; } guard whose condition does not reference $scope. Such a guard is cheap and scope-free, so it should run before the fetch - most nodes then bail out before any scope resolution.

Scope: deliberately narrow and dataflow-free to stay reliable. It only looks at the statement right after the fetch, so it does not catch cheap checks hidden inside a shouldSkip($node, $scope) call (that call references $scope). Keeps false positives at zero - a full composer phpstan run over the current tree reports no hits outside the rule test fixtures.

Covered by NoScopeFetchBeforeCheapGuardRuleTest with one failing and one passing fixture.

@TomasVotruba TomasVotruba changed the title perf rule scope fetch guard [perf] Add NoScopeFetchBeforeCheapGuardRule to spot scope fetched before a scope-free return-null guard Oct 10, 2026
@TomasVotruba
TomasVotruba enabled auto-merge (squash) October 10, 2026 09:15
@TomasVotruba TomasVotruba changed the title [perf] Add NoScopeFetchBeforeCheapGuardRule to spot scope fetched before a scope-free return-null guard [perf] Add PHPStan rule to catch ScopeFetcher::fetch() before a scope-free guard Oct 10, 2026
@TomasVotruba
TomasVotruba merged commit b31f6ae into main Oct 10, 2026
48 of 49 checks passed
@TomasVotruba
TomasVotruba deleted the perf-rule-scope-fetch-guard branch October 10, 2026 09:16
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.

1 participant