Fix phpstan/phpstan#3585: Reassignment of $this is not an error#5146
Merged
VincentLanglet merged 4 commits intophpstan:2.1.xfrom Mar 8, 2026
Merged
Conversation
- Added $this reassignment check to InvalidAssignVarRule - Handles direct assignment ($this = ...), compound assignment ($this .= ...), and destructuring ([$this] = ...) - Error is reported in all contexts (instance methods, static methods, functions) - New regression test in tests/PHPStan/Rules/Operators/data/bug-3585.php Closes phpstan/phpstan#3585
Contributor
|
Playing with it I found a similar error with https://3v4l.org/1rJJM#veol (Which can be considered a separate issue) |
Contributor
It's another issue to me indeed. works fine. Your example is about variable for the parameter, which should be a different rule from the InvalidAssign one (which is about Same will exist for this |
staabm
approved these changes
Mar 8, 2026
Contributor
|
I think it requires your review @ondrejmirtes since we're introducing a new error (?) |
ondrejmirtes
approved these changes
Mar 8, 2026
Contributor
created a new issue for it |
uekann
pushed a commit
to uekann/phpstan-src
that referenced
this pull request
Mar 9, 2026
…tan#5146) Co-authored-by: VincentLanglet <[email protected]> Co-authored-by: Vincent Langlet <[email protected]>
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
PHP reports a fatal error (
Cannot re-assign $this) when$thisis used as an assignment target, but PHPStan did not report any error. This PR adds a check toInvalidAssignVarRuleto detect and report$thisreassignment.Changes
tests/PHPStan/Rules/Operators/data/bug-3585.phptestBug3585inInvalidAssignVarRuleTestRoot cause
The
InvalidAssignVarRulealready validated assignment targets for nullsafe operators and non-assignable expressions, but had no check for the special$thispseudo-variable. PHP itself prevents$thisreassignment at the parser/compiler level, so PHPStan needs to replicate this check.Test
The regression test covers
$thisreassignment in:All cases expect the error "Cannot re-assign $this." to be reported.
Fixes phpstan/phpstan#3585