Fix phpstan/phpstan#14351: Missing errors arround use of $this#5278
Open
phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#14351: Missing errors arround use of $this#5278phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
Conversation
staabm
requested changes
Mar 23, 2026
Contributor
staabm
left a comment
There was a problem hiding this comment.
put tests/PHPStan/Rules/Variables/data/bug-14351 onto the make-lint exclude-list in Makefile
Collaborator
Author
|
Done. Added |
staabm
requested changes
Mar 23, 2026
Contributor
staabm
left a comment
There was a problem hiding this comment.
Resolve conflicts and rebase
- Emit VariableAssignNode for catch variable in NodeScopeResolver so InvalidVariableAssignRule detects `catch (Exception $this)` - Add ThisInGlobalStatementRule to report `global $this` usage - New regression test in tests/PHPStan/Rules/Variables/data/bug-14351.php
The test data file contains intentionally invalid PHP (catch $this, global $this) that triggers lint errors, so it needs to be excluded. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
19954e2 to
e210bca
Compare
staabm
reviewed
Mar 23, 2026
| } | ||
| } | ||
|
|
||
| function foo(): void { |
Contributor
There was a problem hiding this comment.
another case we miss atm is
function foo(): void {
static $this; // should report: Cannot use $this as static variable
Collaborator
Author
There was a problem hiding this comment.
This task also already completed successfully — all 11647 tests passed. Everything has been committed and pushed.
Add ThisInStaticStatementRule to report "Cannot use $this as static variable." alongside the existing global $this and catch $this checks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PHPStan was missing two fatal error detections related to
$this:catch (Exception $this)inside a class method — PHP throws "Fatal error: Cannot re-assign $this"global $this;— PHP throws "Fatal error: Cannot use $this as global variable"Changes
src/Analyser/NodeScopeResolver.php: Emit aVariableAssignNodefor the catch variable soInvalidVariableAssignRulecan detectcatch (Exception $this)src/Rules/Variables/ThisInGlobalStatementRule.php: New rule to reportglobal $thisusage with identifierglobal.thistests/PHPStan/Rules/Variables/InvalidVariableAssignRuleTest.php: AddedtestBug14351for the catch casetests/PHPStan/Rules/Variables/ThisInGlobalStatementRuleTest.php: New test class for the global ruletests/PHPStan/Rules/Variables/data/bug-14351.php: Test data file covering both casesRoot cause
VariableAssignNode, so the existingInvalidVariableAssignRule(which checks for$thisreassignment) never saw them.$thisinglobalstatements at all.Test
The regression test in
bug-14351.phpcovers:catch (Exception $this)inside a class method (line 9) → "Cannot re-assign $this."global $this;in a function (line 15) → "Cannot use $this as global variable."Verified that the test fails without the fix.
Fixes phpstan/phpstan#14351