diff --git a/Makefile b/Makefile index 02b17ed80e..5fdb666448 100644 --- a/Makefile +++ b/Makefile @@ -134,6 +134,7 @@ lint: --exclude tests/PHPStan/Rules/EnumCases/data/bug-14252.php \ --exclude tests/PHPStan/Rules/Functions/data/bug-14241.php \ --exclude tests/PHPStan/Rules/Variables/data/bug-14349.php \ + --exclude tests/PHPStan/Rules/Variables/data/bug-14352.php \ src tests install-paratest: diff --git a/src/Rules/Variables/InvalidVariableAssignRule.php b/src/Rules/Variables/InvalidVariableAssignRule.php index b70a3b6f67..aeaa6e3511 100644 --- a/src/Rules/Variables/InvalidVariableAssignRule.php +++ b/src/Rules/Variables/InvalidVariableAssignRule.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Variables; +use ArrayAccess; use PhpParser\Node; use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\RegisteredRule; @@ -30,6 +31,14 @@ public function processNode(Node $node, Scope $scope): array } if ($variable->name === 'this') { + if ($scope->isInClass()) { + $classReflection = $scope->getClassReflection(); + + if ($classReflection->implementsInterface(ArrayAccess::class)) { + return []; + } + } + return [ RuleErrorBuilder::message('Cannot re-assign $this.') ->identifier('assign.this') diff --git a/tests/PHPStan/Rules/Variables/InvalidVariableAssignRuleTest.php b/tests/PHPStan/Rules/Variables/InvalidVariableAssignRuleTest.php index 06febdd1d0..77cbf2c1b4 100644 --- a/tests/PHPStan/Rules/Variables/InvalidVariableAssignRuleTest.php +++ b/tests/PHPStan/Rules/Variables/InvalidVariableAssignRuleTest.php @@ -4,6 +4,7 @@ use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; +use PHPUnit\Framework\Attributes\RequiresPhp; /** * @extends RuleTestCase @@ -50,6 +51,43 @@ public function testBug3585(): void ]); } + #[RequiresPhp('>= 8.0')] + public function testBug14352(): void + { + $this->analyse([__DIR__ . '/data/bug-14352.php'], [ + /* + [ + 'Cannot re-assign $this.', + 13, + ], + */ + [ + 'Cannot re-assign $this.', + 37, + ], + [ + 'Cannot re-assign $this.', + 39, + ], + [ + 'Cannot re-assign $this.', + 47, + ], + [ + 'Cannot re-assign $this.', + 49, + ], + [ + 'Cannot re-assign $this.', + 57, + ], + [ + 'Cannot re-assign $this.', + 63, + ], + ]); + } + public function testBug14349(): void { $this->analyse([__DIR__ . '/data/bug-14349.php'], [ diff --git a/tests/PHPStan/Rules/Variables/data/bug-14352.php b/tests/PHPStan/Rules/Variables/data/bug-14352.php new file mode 100644 index 0000000000..60f16d6544 --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-14352.php @@ -0,0 +1,64 @@ +