Skip to content
Merged
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions src/Rules/Variables/InvalidVariableAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Variables;

use ArrayAccess;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
Expand Down Expand Up @@ -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')
Expand Down
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Variables/InvalidVariableAssignRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;

/**
* @extends RuleTestCase<InvalidVariableAssignRule>
Expand Down Expand Up @@ -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'], [
Expand Down
64 changes: 64 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14352.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types = 1);

namespace Bug14352;

use ArrayAccess;

class TestArrayAccess implements ArrayAccess
{
public function doFoo(string $key, string $value): void
{
$this[$key] = $value; // fine because ArrayAccess

$this = $value; // should still fail
}

public function offsetExists(mixed $offset): bool
{
}

public function offsetGet(mixed $offset): mixed
{
}

public function offsetSet(mixed $offset, mixed $value): void
{
}

public function offsetUnset(mixed $offset): void
{
}
}

final class FinalTestPlain
{
public function doFoo(string $key, string $value): void
{
$this[$key] = $value;

$this = $value;
}
}

class TestPlain
{
public function doFoo(string $key, string $value): void
{
$this[$key] = $value;

$this = $value;
}
}

class TestStatic
{
static public function doFoo(string $value): void
{
$this = $value;
}
}

function doFoo(string $value): void
{
$this = $value;
}
Loading