Skip to content

Detect more possibly-impossible offset #4164

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
wants to merge 4 commits into
base: 2.1.x
Choose a base branch
from

Conversation

VincentLanglet
Copy link
Contributor

@ondrejmirtes
Copy link
Member

I feel like the error could always be reported on level 7+.

The difference is that the reportPossiblyUndefined... option is for offsets like string, but in this case this is about a union and one of the union parts never exists on the constant array so it'd be a good fit for L7+ without any extra option.

@VincentLanglet VincentLanglet changed the title Add non regression test Detect more possibly-impossible offset Jul 26, 2025
@VincentLanglet VincentLanglet marked this pull request as draft July 26, 2025 15:47
@VincentLanglet VincentLanglet force-pushed the test/1061 branch 10 times, most recently from c290ea1 to 2417411 Compare August 1, 2025 11:28
@VincentLanglet VincentLanglet marked this pull request as ready for review August 1, 2025 11:43
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -127,6 +131,10 @@ public function testRule(): void
'Offset int does not exist on array<string, string>.',
312,
],
[
'Offset int|null might not exist on array<string, string>.',
Copy link
Member

Choose a reason for hiding this comment

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

Well this is totally wrong, null gets cast to '' in an array key and that can exist on array<string, string>.. Not sure where to do the fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You misunderstood the error message.

You can see that phpstan already report on line 312 (the error above)

'Offset int does not exist on array<string, string>.',

So here we're reporting the same message with "might not exist" certainty because

  • int does not exists
  • null can exist

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.

Fails to detect possibly-impossible constant offset
3 participants