Skip to content

Commit ff7af6b

Browse files
committed
Revert "Use dimFetch flag and check $this type with !->no() for ArrayAccess reassignment"
This reverts commit 5c5e3c7.
1 parent 5c5e3c7 commit ff7af6b

5 files changed

Lines changed: 7 additions & 38 deletions

File tree

src/Analyser/ExprHandler/AssignHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ public function processAssignVar(
463463

464464
if ($varType->isArray()->yes() || !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()) {
465465
if ($var instanceof Variable && is_string($var->name)) {
466-
$nodeScopeResolver->callNodeCallback($nodeCallback, new VariableAssignNode($var, new TypeExpr($valueToWrite), true), $scopeBeforeAssignEval, $storage);
466+
$nodeScopeResolver->callNodeCallback($nodeCallback, new VariableAssignNode($var, new TypeExpr($valueToWrite)), $scopeBeforeAssignEval, $storage);
467467
$scope = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes());
468468
} else {
469469
if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
@@ -480,7 +480,7 @@ public function processAssignVar(
480480
}
481481
} else {
482482
if ($var instanceof Variable) {
483-
$nodeScopeResolver->callNodeCallback($nodeCallback, new VariableAssignNode($var, $assignedPropertyExpr, true), $scopeBeforeAssignEval, $storage);
483+
$nodeScopeResolver->callNodeCallback($nodeCallback, new VariableAssignNode($var, $assignedPropertyExpr), $scopeBeforeAssignEval, $storage);
484484
} elseif ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
485485
$nodeScopeResolver->callNodeCallback($nodeCallback, new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scopeBeforeAssignEval, $storage);
486486
if ($var instanceof PropertyFetch && $var->name instanceof Node\Identifier && !$isAssignOp) {
@@ -799,7 +799,7 @@ public function processAssignVar(
799799
}
800800

801801
if ($var instanceof Variable && is_string($var->name)) {
802-
$nodeScopeResolver->callNodeCallback($nodeCallback, new VariableAssignNode($var, $assignedPropertyExpr, true), $scope, $storage);
802+
$nodeScopeResolver->callNodeCallback($nodeCallback, new VariableAssignNode($var, $assignedPropertyExpr), $scope, $storage);
803803
$scope = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes());
804804
} else {
805805
if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {

src/Node/VariableAssignNode.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ final class VariableAssignNode extends NodeAbstract implements VirtualNode
1212
public function __construct(
1313
private Expr\Variable $variable,
1414
private Expr $assignedExpr,
15-
private bool $dimFetch = false,
1615
)
1716
{
1817
parent::__construct($variable->getAttributes());
@@ -28,11 +27,6 @@ public function getAssignedExpr(): Expr
2827
return $this->assignedExpr;
2928
}
3029

31-
public function isDimFetch(): bool
32-
{
33-
return $this->dimFetch;
34-
}
35-
3630
#[Override]
3731
public function getType(): string
3832
{

src/Rules/Variables/InvalidVariableAssignRule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public function processNode(Node $node, Scope $scope): array
3232
}
3333

3434
if ($variable->name === 'this') {
35-
if ($node->isDimFetch() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($scope->getType($variable))->no()) {
35+
$expr = $node->getAssignedExpr();
36+
$type = $scope->getType($expr);
37+
38+
if ((new ObjectType(ArrayAccess::class))->isSuperTypeOf($type)->yes()) {
3639
return [];
3740
}
3841

tests/PHPStan/Rules/Variables/InvalidVariableAssignRuleTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ public function testBug14352(): void
6363
'Cannot re-assign $this.',
6464
37,
6565
],
66-
[
67-
'Cannot re-assign $this.',
68-
61,
69-
],
7066
]);
7167
}
7268

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,6 @@ public function offsetUnset(mixed $offset): void
3030
}
3131
}
3232

33-
class TestArrayAccessReassign implements ArrayAccess
34-
{
35-
public function doFoo(self $other): void
36-
{
37-
$this = $other; // should still fail
38-
}
39-
40-
public function offsetExists(mixed $offset): bool
41-
{
42-
}
43-
44-
public function offsetGet(mixed $offset): mixed
45-
{
46-
}
47-
48-
public function offsetSet(mixed $offset, mixed $value): void
49-
{
50-
}
51-
52-
public function offsetUnset(mixed $offset): void
53-
{
54-
}
55-
}
56-
5733
final class FinalTestPlain
5834
{
5935
public function doFoo(string $key, string $value): void

0 commit comments

Comments
 (0)