Skip to content

Commit ec5ce0a

Browse files
committed
Fix setting known offset
1 parent 8bb2a20 commit ec5ce0a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Analyser/MutatingScope.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,13 @@ public function getType(Expr $node): Type
750750
return $this->getType($node->getVar())->unsetOffset($this->getType($node->getDim()));
751751
}
752752
if ($node instanceof SetOffsetValueTypeExpr) {
753+
$var = $node->getVar() instanceof OriginalPropertyTypeExpr ? $node->getVar()->getPropertyFetch() : $node->getVar();
754+
if ($node->getDim() !== null && $this->hasExpressionType(new Expr\ArrayDimFetch($var, $node->getDim()))->yes()) {
755+
return $this->getType($node->getVar())->setExistingOffsetValueType(
756+
$this->getType($node->getDim()),
757+
$this->getType($node->getValue()),
758+
);
759+
}
753760
return $this->getType($node->getVar())->setOffsetValueType(
754761
$node->getDim() !== null ? $this->getType($node->getDim()) : null,
755762
$this->getType($node->getValue()),

tests/PHPStan/Rules/Arrays/data/offset-access-value-assignment.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,20 @@ public function foo(): void
5959
$this->collection2[] = 2;
6060
}
6161
}
62+
63+
class TestList
64+
{
65+
66+
/**
67+
* @var list<int>
68+
*/
69+
public array $list = [];
70+
71+
public function setKnownOffset(int $offset): void
72+
{
73+
if (isset($this->list[$offset])) {
74+
$this->list[$offset] = 123;
75+
}
76+
}
77+
78+
}

0 commit comments

Comments
 (0)