Skip to content
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

Types not being pulled from @phpstan-type correctly #6556

Closed
dehrk opened this issue Feb 3, 2022 · 7 comments
Closed

Types not being pulled from @phpstan-type correctly #6556

dehrk opened this issue Feb 3, 2022 · 7 comments
Labels
Milestone

Comments

@dehrk
Copy link

dehrk commented Feb 3, 2022

Bug report

When defining array structure with @phpstan-type, checking some keys using array_key_exists does not correctly determine the type associated.

Code snippet that reproduces the problem

https://phpstan.org/r/2f4d63e8-1e34-4f3d-8d6c-912db0cef960

Expected output

4

PHPStan should be able to determine the types for all defined keys. If the keys in the array are not listed in the PHPStan type, it can determine which keys are missing (https://phpstan.org/r/286792cb-f9ef-4536-8751-84f86217ef69).

Did PHPStan help you today? Did it make you happy in any way?

I love PHPstan! It's helping me get my code ready for PHP8 and making it much easier!

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 6, 2022
@ondrejmirtes
Copy link
Member

Looks like the type gets corrupted after the first array_key_exists: https://phpstan.org/r/ac800fe0-3ed7-4043-bc7d-fd0f50794a01

@phpstan-bot
Copy link
Contributor

@dehrk After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet:

@@ @@
-26: Binary operation "." between '<p>' and array<literal-string&non-empty-string, string>|string results in an error.
-32: Offset 'title' does not exist on array<literal-string&non-empty-string, string>|string.
-33: Offset 'details' does not exist on array<literal-string&non-empty-string, string>|string.
+26: Binary operation "." between '<p>' and array{title: string, details: string}|string results in an error.
+32: Offset 'title' does not exist on array{title: string, details: string}|string.
+33: Offset 'details' does not exist on array{title: string, details: string}|string.
Full report
Line Error
26 `Binary operation "." between '

' and array{title: string, details: string}

32 `Offset 'title' does not exist on array{title: string, details: string}
33 `Offset 'details' does not exist on array{title: string, details: string}

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet:

@@ @@
 17: Dumped type: array{test1?: string, test2?: string, test3?: array{title: string, details: string}}
-27: Binary operation "." between '<p>' and array<literal-string&non-empty-string, string>|string results in an error.
-31: Dumped type: array<literal-string&non-empty-string, array<literal-string&non-empty-string, string>|string>
-35: Offset 'title' does not exist on array<literal-string&non-empty-string, string>|string.
-36: Offset 'details' does not exist on array<literal-string&non-empty-string, string>|string.
+27: Binary operation "." between '<p>' and array{title: string, details: string}|string results in an error.
+31: Dumped type: array<'test1'|'test2'|'test3', array{title: string, details: string}|string>
+35: Offset 'title' does not exist on array{title: string, details: string}|string.
+36: Offset 'details' does not exist on array{title: string, details: string}|string.
Full report
Line Error
17 Dumped type: array{test1?: string, test2?: string, test3?: array{title: string, details: string}}
27 `Binary operation "." between '

' and array{title: string, details: string}

31 `Dumped type: array<'test1'
35 `Offset 'title' does not exist on array{title: string, details: string}
36 `Offset 'details' does not exist on array{title: string, details: string}

@phpstan-bot
Copy link
Contributor

@dehrk After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-26: Binary operation "." between '<p>' and array<literal-string&non-empty-string, string>|string results in an error.
-32: Offset 'title' does not exist on array<literal-string&non-empty-string, string>|string.
-33: Offset 'details' does not exist on array<literal-string&non-empty-string, string>|string.
+26: Offset 'test1'|'test2' does not exist on array{test1?: string, test2?: string, test3?: array{title: string, details: string}}.
Full report
Line Error
26 `Offset 'test1'

@phpstan-bot
Copy link
Contributor

@dehrk After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-23: Call to function array_key_exists() with 'test1'|'test2' and array{test3?: array{title: string, details: string}} will always evaluate to false.
+24: Offset 'test1'|'test2' does not exist on array{test3?: array{title: string, details: string}}.
Full report
Line Error
24 `Offset 'test1'

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
 17: Dumped type: array{test1?: string, test2?: string, test3?: array{title: string, details: string}}
-27: Binary operation "." between '<p>' and array<literal-string&non-empty-string, string>|string results in an error.
-31: Dumped type: array<literal-string&non-empty-string, array<literal-string&non-empty-string, string>|string>
-35: Offset 'title' does not exist on array<literal-string&non-empty-string, string>|string.
-36: Offset 'details' does not exist on array<literal-string&non-empty-string, string>|string.
+27: Offset 'test1'|'test2' does not exist on array{test1?: string, test2?: string, test3?: array{title: string, details: string}}.
+31: Dumped type: array{test1?: string, test2?: string, test3?: array{title: string, details: string}}
Full report
Line Error
17 Dumped type: array{test1?: string, test2?: string, test3?: array{title: string, details: string}}
27 `Offset 'test1'
31 Dumped type: array{test1?: string, test2?: string, test3?: array{title: string, details: string}}

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants