Skip to content

Commit 12e4ef5

Browse files
committed
fix
1 parent ff7af6b commit 12e4ef5

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

src/Rules/Variables/InvalidVariableAssignRule.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ public function processNode(Node $node, Scope $scope): array
3232
}
3333

3434
if ($variable->name === 'this') {
35-
$expr = $node->getAssignedExpr();
36-
$type = $scope->getType($expr);
35+
if ($scope->isInClass()) {
36+
$classReflection = $scope->getClassReflection();
3737

38-
if ((new ObjectType(ArrayAccess::class))->isSuperTypeOf($type)->yes()) {
39-
return [];
38+
if ($classReflection->is(ArrayAccess::class)) {
39+
return [];
40+
}
4041
}
4142

4243
return [

tests/PHPStan/Rules/Variables/InvalidVariableAssignRuleTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,36 @@ public function testBug3585(): void
5555
public function testBug14352(): void
5656
{
5757
$this->analyse([__DIR__ . '/data/bug-14352.php'], [
58+
/*
5859
[
5960
'Cannot re-assign $this.',
6061
13,
6162
],
63+
*/
6264
[
6365
'Cannot re-assign $this.',
6466
37,
6567
],
68+
[
69+
'Cannot re-assign $this.',
70+
39,
71+
],
72+
[
73+
'Cannot re-assign $this.',
74+
47,
75+
],
76+
[
77+
'Cannot re-assign $this.',
78+
49,
79+
],
80+
[
81+
'Cannot re-assign $this.',
82+
57,
83+
],
84+
[
85+
'Cannot re-assign $this.',
86+
63,
87+
],
6688
]);
6789
}
6890

tests/PHPStan/Rules/Variables/data/bug-14352.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ final class FinalTestPlain
3535
public function doFoo(string $key, string $value): void
3636
{
3737
$this[$key] = $value;
38+
39+
$this = $value;
3840
}
3941
}
4042

@@ -43,5 +45,20 @@ class TestPlain
4345
public function doFoo(string $key, string $value): void
4446
{
4547
$this[$key] = $value;
48+
49+
$this = $value;
50+
}
51+
}
52+
53+
class TestStatic
54+
{
55+
static public function doFoo(string $value): void
56+
{
57+
$this = $value;
4658
}
4759
}
60+
61+
function doFoo(string $value): void
62+
{
63+
$this = $value;
64+
}

0 commit comments

Comments
 (0)