Skip to content

Fix array + array inference#4944

Merged
VincentLanglet merged 3 commits intophpstan:2.1.xfrom
VincentLanglet:fix13561
Feb 16, 2026
Merged

Fix array + array inference#4944
VincentLanglet merged 3 commits intophpstan:2.1.xfrom
VincentLanglet:fix13561

Conversation

@VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet marked this pull request as ready for review February 15, 2026 18:37
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

if ($leftCount > 0) {
// Use the first constant array as a reference to list potential offsets.
// We only need to check the first array because we're looking for offsets that exist in ALL arrays.
$constantArray = $leftConstantArrays[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure why this only works on index 0.

in array_merge we iterate over all left arrays

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 is something you could try for array_merge too I think.

Since we're looking only for certainty of hasOffsetValueType YES the key has to be in all the constant arrays, so checking only one constant array is enough.

  • only keys in the first constant array can have YES certainty
  • key which are not in the first contant array will at most have the MAYBE certainty

Copy link
Contributor

Choose a reason for hiding this comment

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

do you think it would make sense to adjust array_merge() and array_replace() with this PR, so we have it in sync over all implementations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly I'm not sure about it.

Those implementations seems more complicated, especially because there is an iteration

foreach ($argTypes as $argType) {

Also the logic seems to be different since there is some looking at code like

TrinaryLogic::createFromBoolean($argType->hasOffsetValueType($keyType)->yes());

or

if ($hasOffsetValue->yes()) {
					// the last string-keyed offset will overwrite previous values
					$hasOffsetType = new HasOffsetValueType(
						$keyType,
						$offsetType,
					);
				} elseif ($hasOffsetValue->maybe()) {
					$hasOffsetType = new HasOffsetType(
						$keyType,
					);
				} else {
					continue;
				}

which I don't have here.

So I would prefer avoid to delay with PR (with the risk of introducing a regression) and fixing the existing bug, and we could try a dedicated PR with the refacto for array_merge/replace ?

Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

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

thank you

@VincentLanglet VincentLanglet merged commit ebc5354 into phpstan:2.1.x Feb 16, 2026
637 of 651 checks passed
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.

3 participants