Skip to content

Fix phpstan/phpstan#3585: Reassignment of $this is not an error#5146

Merged
VincentLanglet merged 4 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-yfjwukp
Mar 8, 2026
Merged

Fix phpstan/phpstan#3585: Reassignment of $this is not an error#5146
VincentLanglet merged 4 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-yfjwukp

Conversation

@phpstan-bot
Copy link
Collaborator

@phpstan-bot phpstan-bot commented Mar 8, 2026

Summary

PHP reports a fatal error (Cannot re-assign $this) when $this is used as an assignment target, but PHPStan did not report any error. This PR adds a check to InvalidAssignVarRule to detect and report $this reassignment.

Changes

  • Added test data file tests/PHPStan/Rules/Operators/data/bug-3585.php
  • Added test method testBug3585 in InvalidAssignVarRuleTest

Root cause

The InvalidAssignVarRule already validated assignment targets for nullsafe operators and non-assignable expressions, but had no check for the special $this pseudo-variable. PHP itself prevents $this reassignment at the parser/compiler level, so PHPStan needs to replicate this check.

Test

The regression test covers $this reassignment in:

  • Instance methods (direct assign, object assign, compound assign, destructuring)
  • Static methods
  • Regular functions

All cases expect the error "Cannot re-assign $this." to be reported.

Fixes phpstan/phpstan#3585

VincentLanglet and others added 2 commits March 8, 2026 11:30
- 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
@staabm
Copy link
Contributor

staabm commented Mar 8, 2026

Playing with it I found a similar error with https://3v4l.org/1rJJM#veol

(Which can be considered a separate issue)

https://phpstan.org/r/5e387380-3e06-4050-84b9-df9540355ffe

@VincentLanglet
Copy link
Contributor

Playing with it I found a similar error with 3v4l.org/1rJJM#veol

(Which can be considered a separate issue)

phpstan.org/r/5e387380-3e06-4050-84b9-df9540355ffe

It's another issue to me indeed.

function doFoo() {
    $_FILES = 1;
}

works fine.

Your example is about variable for the parameter, which should be a different rule from the InvalidAssign one (which is about = operators).

Same will exist for this
https://phpstan.org/r/8f8a2041-ccd3-4f99-89fa-df496d9cd521

@VincentLanglet
Copy link
Contributor

I think it requires your review @ondrejmirtes since we're introducing a new error (?)

@VincentLanglet VincentLanglet merged commit a275ce9 into phpstan:2.1.x Mar 8, 2026
639 of 648 checks passed
@staabm staabm deleted the create-pull-request/patch-yfjwukp branch March 8, 2026 21:34
@staabm
Copy link
Contributor

staabm commented Mar 8, 2026

Playing with it I found a similar error with 3v4l.org/1rJJM#veol

created a new issue for it

phpstan/phpstan#14241

uekann pushed a commit to uekann/phpstan-src that referenced this pull request Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants