Skip to content

Fix phpstan/phpstan#14349: Missing "Fatal error: Cannot re-assign $this"#5276

Merged
ondrejmirtes merged 4 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-w4zks1f
Mar 23, 2026
Merged

Fix phpstan/phpstan#14349: Missing "Fatal error: Cannot re-assign $this"#5276
ondrejmirtes merged 4 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-w4zks1f

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

PHP produces a fatal error "Cannot re-assign $this" when $this is used as a foreach value or key variable, but PHPStan did not report this. This PR adds a new rule to detect this error.

Changes

  • Added src/Rules/Variables/InvalidForeachVariableRule.php — a new level 0 rule that checks foreach statements for $this as value or key variable
  • Added tests/PHPStan/Rules/Variables/InvalidForeachVariableRuleTest.php — test case for the new rule
  • Added tests/PHPStan/Rules/Variables/data/bug-14349.php — test data covering $this as value, by-ref value, and key in foreach

Root cause

There was no rule checking foreach variable names for $this. The existing InvalidParameterNameRule covered function/method parameters and InvalidLexicalVariablesInClosureUseRule covered closure use() clauses, but foreach was a gap.

Test

The regression test verifies that foreach ($a as $this), foreach ($a as &$this), and foreach ($a as $this => $v) all produce "Cannot re-assign $this." errors, while foreach ($a as $ok) produces no error.

Fixes phpstan/phpstan#14349

… variable

- Added new InvalidForeachVariableRule that detects $this used as foreach value or key variable
- Rule is registered at level 0 since this is a fatal error in PHP
- New regression test in tests/PHPStan/Rules/Variables/data/bug-14349.php
Copy link
Copy Markdown
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Create a rule for VariableAssignNode. Extract the part about assigning $this from InvalidAssignVarRule there. And also make sure the same rule for VariableAssignNode also covers this foreach scenario.

Create InvalidVariableAssignRule for VariableAssignNode that detects
$this reassignment in all contexts (regular assignments, foreach
variables, list destructuring). Extract the $this check from
InvalidAssignVarRule into this new unified rule.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@phpstan-bot
Copy link
Copy Markdown
Collaborator Author

All done. Here's a summary of the changes:

  1. Created src/Rules/Variables/InvalidVariableAssignRule.php - A new level 0 rule that listens on VariableAssignNode and reports "Cannot re-assign $this." when $this is the target of any variable assignment (regular assignments, foreach variables, list destructuring).

  2. Modified src/Rules/Operators/InvalidAssignVarRule.php - Removed the $this check from containsNonAssignableExpression() (now returns false for all variables, since $this is handled by the new rule).

  3. Deleted src/Rules/Variables/InvalidForeachVariableRule.php - No longer needed since the new VariableAssignNode-based rule covers foreach scenarios.

  4. Updated tests - The InvalidVariableAssignRuleTest covers all scenarios: foreach value/key/by-ref, regular assignment, compound assignment, and list destructuring with $this. The InvalidAssignVarRuleTest::testBug3585 now expects no errors since those were all $this assignments.

All 11,634 tests pass and make phpstan reports no errors.

Copy link
Copy Markdown
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Add bug-3585.php to the test of the new rule

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@phpstan-bot
Copy link
Copy Markdown
Collaborator Author

Already handled — the change has been committed and pushed. All tests and static analysis passed.

Copy link
Copy Markdown
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

tests/PHPStan/Rules/Variables/data/bug-14349.php should be added to excluded files in make lint in Makefile

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@phpstan-bot
Copy link
Copy Markdown
Collaborator Author

Done. Added tests/PHPStan/Rules/Variables/data/bug-14349.php to the excluded files in make lint in the Makefile, since that test file contains intentionally invalid PHP ($this reassignment) that would fail the linter. The lint target passes cleanly.

@ondrejmirtes ondrejmirtes merged commit 05092ed into phpstan:2.1.x Mar 23, 2026
647 of 651 checks passed
@ondrejmirtes ondrejmirtes deleted the create-pull-request/patch-w4zks1f branch March 23, 2026 10:33
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.

2 participants