Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,13 @@ public function getType(Expr $node): Type
return $this->getType($node->getVar())->unsetOffset($this->getType($node->getDim()));
}
if ($node instanceof SetOffsetValueTypeExpr) {
$var = $node->getVar() instanceof OriginalPropertyTypeExpr ? $node->getVar()->getPropertyFetch() : $node->getVar();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels wrong. I don't really get the reason we generally wrap with OriginalPropertyTypeExpr

if ($node->getDim() !== null && $this->hasExpressionType(new Expr\ArrayDimFetch($var, $node->getDim()))->yes()) {
return $this->getType($node->getVar())->setExistingOffsetValueType(
$this->getType($node->getDim()),
$this->getType($node->getValue()),
);
}
return $this->getType($node->getVar())->setOffsetValueType(
$node->getDim() !== null ? $this->getType($node->getDim()) : null,
$this->getType($node->getValue()),
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/offset-access-value-assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,20 @@ public function foo(): void
$this->collection2[] = 2;
}
}

class TestList
{

/**
* @var list<int>
*/
public array $list = [];

public function setKnownOffset(int $offset): void
{
if (isset($this->list[$offset])) {
$this->list[$offset] = 123;
}
}

}
Loading