Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Oct 31, 2025

closes phpstan/phpstan#13747


fixes ~55x "offset might not exist" errors on phpstan-src with reportPossiblyNonexistentGeneralArrayOffset: true

@staabm staabm marked this pull request as ready for review October 31, 2025 14:59
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm force-pushed the great branch 4 times, most recently from ca2ffe9 to fc8643e Compare November 2, 2025 11:22
Comment on lines +1195 to +1215
$intersection = [];
$intersection[] = $arrayType;
$intersection[] = new NonEmptyArrayType();

$zero = new ConstantIntegerType(0);
$i = 0;
foreach ($builderData as [$offsetType, $valueType]) {
// non-empty-list already implies the offset 0
if ($zero->isSuperTypeOf($offsetType)->yes()) {
continue;
}

if ($i > self::MAX_ACCESSORIES_LIMIT) {
break;
}

$intersection[] = new HasOffsetValueType($offsetType, $valueType);
$i++;
}

$resultTypes[] = TypeCombinator::intersect(...$intersection);
Copy link
Contributor Author

@staabm staabm Dec 6, 2025

Choose a reason for hiding this comment

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

could also use a AccessoryArraySize, but this likely would be more involved and require a bigger refactor.

therefore I went with this simple approach which covers most useful use-cases

@staabm staabm marked this pull request as draft December 6, 2025 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

infer offset after count

2 participants