Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix setting known offset #4270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
SplotyCode wants to merge 1 commit into phpstan:2.1.x
base: 2.1.x
Choose a base branch
Loading
from SplotyCode:fix/set-array-offset
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
View file Open in desktop
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

@SplotyCode SplotyCode Aug 30, 2025

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
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /