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

Cannot resolve template parameters of empty array #5065

Closed
AlexandruGG opened this issue May 25, 2021 · 11 comments
Closed

Cannot resolve template parameters of empty array #5065

AlexandruGG opened this issue May 25, 2021 · 11 comments
Labels
Milestone

Comments

@AlexandruGG
Copy link

Bug report

When using an empty array in a place with template parameters, PHPStan seems to not be able to resolve the needed types, assigning type *NEVER* to the types of keys and values.

Code snippet that reproduces the problem

https://phpstan.org/r/1f55bae3-c09d-49b0-be85-3f6a28e08dae

Expected output

I am not 100% sure if this is intended behaviour, please let me know if this is not a genuine bug. 🐛
I've included a couple "workarounds" that can be used to fix the error, but I'm not sure if they should be needed or not.

The same code in Psalm does not show an error: https://psalm.dev/r/0775d2d6b0

@phpstan-bot
Copy link
Contributor

@AlexandruGG After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 7.4 – 8.0 (1 error)
+PHP 7.4 – 8.0 (2 errors)
 ==========
 
+12: Property Collection::$source is never read, only written.
 30: Method Collection::empty() should return Collection<NewTKey of (int|string), NewT> but returns Collection<*NEVER*, *NEVER*>.
 
 PHP 7.1 – 7.3 (6 errors)
Full report

PHP 7.4 – 8.0 (2 errors)

Line Error
12 Property Collection::$source is never read, only written.
30 `Method Collection::empty() should return Collection<NewTKey of (int

PHP 7.1 – 7.3 (6 errors)

Line Error
30 Syntax error, unexpected ':' on line 30
30 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 30
44 Syntax error, unexpected T_DOUBLE_ARROW on line 44
44 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 44
56 Syntax error, unexpected ':' on line 56
56 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 56

@phpstan-bot
Copy link
Contributor

@AlexandruGG After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 7.4 – 8.0 (1 error)
+PHP 7.4 – 8.0 (2 errors)
 ==========
 
+12: Property Collection::$source is never read, only written.
 30: Method Collection::empty() should return Collection<NewTKey of (int|string), NewT> but returns Collection<*NEVER*, *NEVER*>.
 
 PHP 7.1 – 7.3 (6 errors)
Full report

PHP 7.4 – 8.0 (2 errors)

Line Error
12 Property Collection::$source is never read, only written.
30 `Method Collection::empty() should return Collection<NewTKey of (int

PHP 7.1 – 7.3 (6 errors)

Line Error
30 Syntax error, unexpected ':' on line 30
30 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 30
44 Syntax error, unexpected T_DOUBLE_ARROW on line 44
44 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 44
56 Syntax error, unexpected ':' on line 56
56 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 56

@phpstan-bot
Copy link
Contributor

@AlexandruGG After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 7.4 – 8.0 (1 error)
+PHP 7.4 – 8.0 (8 errors)
 ==========
 
+12: Property Collection::$source is never read, only written.
+28: Template type NewT of method Collection::empty() is not referenced in a parameter.
+28: Template type NewTKey of method Collection::empty() is not referenced in a parameter.
 30: Method Collection::empty() should return Collection<NewTKey of (int|string), NewT> but returns Collection<*NEVER*, *NEVER*>.
+39: Template type NewT of method Collection::emptyWorkaround() is not referenced in a parameter.
+39: Template type NewTKey of method Collection::emptyWorkaround() is not referenced in a parameter.
+53: Template type NewT of method Collection::emptyWorkaround2() is not referenced in a parameter.
+53: Template type NewTKey of method Collection::emptyWorkaround2() is not referenced in a parameter.
 
 PHP 7.1 – 7.3 (6 errors)
 ==========
Full report

PHP 7.4 – 8.0 (8 errors)

Line Error
12 Property Collection::$source is never read, only written.
28 Template type NewT of method Collection::empty() is not referenced in a parameter.
28 Template type NewTKey of method Collection::empty() is not referenced in a parameter.
30 `Method Collection::empty() should return Collection<NewTKey of (int
39 Template type NewT of method Collection::emptyWorkaround() is not referenced in a parameter.
39 Template type NewTKey of method Collection::emptyWorkaround() is not referenced in a parameter.
53 Template type NewT of method Collection::emptyWorkaround2() is not referenced in a parameter.
53 Template type NewTKey of method Collection::emptyWorkaround2() is not referenced in a parameter.

PHP 7.1 – 7.3 (6 errors)

Line Error
30 Syntax error, unexpected ':' on line 30
30 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 30
44 Syntax error, unexpected T_DOUBLE_ARROW on line 44
44 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 44
56 Syntax error, unexpected ':' on line 56
56 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 56

@phpstan-bot
Copy link
Contributor

@AlexandruGG After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
 PHP 7.4 – 8.0 (1 error)
 ==========
 
-30: Method Collection::empty() should return Collection<NewTKey of (int|string), NewT> but returns Collection<*NEVER*, *NEVER*>.
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
+Run PHPStan with --debug option and post the stack trace to:
+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
 
 PHP 7.1 – 7.3 (6 errors)
 ==========
Full report

PHP 7.4 – 8.0 (1 error)

Line Error
-1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md

PHP 7.1 – 7.3 (6 errors)

Line Error
30 Syntax error, unexpected ':' on line 30
30 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 30
44 Syntax error, unexpected T_DOUBLE_ARROW on line 44
44 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 44
56 Syntax error, unexpected ':' on line 56
56 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 56

@phpstan-bot
Copy link
Contributor

@AlexandruGG After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 7.4 – 8.0 (1 error)
+PHP 7.4 – 8.0 (8 errors)
 ==========
 
+28: Template type NewTKey of method Collection::empty() is not referenced in a parameter.
+28: Template type NewT of method Collection::empty() is not referenced in a parameter.
 30: Method Collection::empty() should return Collection<NewTKey of (int|string), NewT> but returns Collection<*NEVER*, *NEVER*>.
+39: Template type NewTKey of method Collection::emptyWorkaround() is not referenced in a parameter.
+39: Template type NewT of method Collection::emptyWorkaround() is not referenced in a parameter.
+53: Template type NewTKey of method Collection::emptyWorkaround2() is not referenced in a parameter.
+53: Template type NewT of method Collection::emptyWorkaround2() is not referenced in a parameter.
+12: Property Collection::$source is never read, only written.
 
 PHP 7.1 – 7.3 (6 errors)
 ==========
 
+30: Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 30
 30: Syntax error, unexpected ':' on line 30
-30: Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 30
+44: Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 44
 44: Syntax error, unexpected T_DOUBLE_ARROW on line 44
-44: Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 44
-56: Syntax error, unexpected ':' on line 56
-56: Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 56
+56: Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 56
+56: Syntax error, unexpected ':' on line 56
Full report

PHP 7.4 – 8.0 (8 errors)

Line Error
28 Template type NewTKey of method Collection::empty() is not referenced in a parameter.
28 Template type NewT of method Collection::empty() is not referenced in a parameter.
30 `Method Collection::empty() should return Collection<NewTKey of (int
39 Template type NewTKey of method Collection::emptyWorkaround() is not referenced in a parameter.
39 Template type NewT of method Collection::emptyWorkaround() is not referenced in a parameter.
53 Template type NewTKey of method Collection::emptyWorkaround2() is not referenced in a parameter.
53 Template type NewT of method Collection::emptyWorkaround2() is not referenced in a parameter.
12 Property Collection::$source is never read, only written.

PHP 7.1 – 7.3 (6 errors)

Line Error
30 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 30
30 Syntax error, unexpected ':' on line 30
44 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 44
44 Syntax error, unexpected T_DOUBLE_ARROW on line 44
56 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 56
56 Syntax error, unexpected ':' on line 56

@drupol
Copy link

drupol commented Jan 6, 2022

I have to admit that revisiting the code-snippets after each PHPStan new commit, and displaying a comment when the result is different from previous one is a genius idea ! Congrats for this feature !!!

@phpstan-bot
Copy link
Contributor

@AlexandruGG After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 7.4 – 8.0 (1 error)
+PHP 7.4 – 8.0 (7 errors)
 ==========
 
-30: Method Collection::empty() should return Collection<NewTKey of (int|string), NewT> but returns Collection<*NEVER*, *NEVER*>.
+12: Property Collection::$source is never read, only written.
+28: Template type NewT of method Collection::empty() is not referenced in a parameter.
+28: Template type NewTKey of method Collection::empty() is not referenced in a parameter.
+39: Template type NewT of method Collection::emptyWorkaround() is not referenced in a parameter.
+39: Template type NewTKey of method Collection::emptyWorkaround() is not referenced in a parameter.
+53: Template type NewT of method Collection::emptyWorkaround2() is not referenced in a parameter.
+53: Template type NewTKey of method Collection::emptyWorkaround2() is not referenced in a parameter.
 
 PHP 7.1 – 7.3 (6 errors)
 ==========
Full report

PHP 7.4 – 8.0 (7 errors)

Line Error
12 Property Collection::$source is never read, only written.
28 Template type NewT of method Collection::empty() is not referenced in a parameter.
28 Template type NewTKey of method Collection::empty() is not referenced in a parameter.
39 Template type NewT of method Collection::emptyWorkaround() is not referenced in a parameter.
39 Template type NewTKey of method Collection::emptyWorkaround() is not referenced in a parameter.
53 Template type NewT of method Collection::emptyWorkaround2() is not referenced in a parameter.
53 Template type NewTKey of method Collection::emptyWorkaround2() is not referenced in a parameter.

PHP 7.1 – 7.3 (6 errors)

Line Error
30 Syntax error, unexpected ':' on line 30
30 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 30
44 Syntax error, unexpected T_DOUBLE_ARROW on line 44
44 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 44
56 Syntax error, unexpected ':' on line 56
56 Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM on line 56

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@c2b9e71

@drupol
Copy link

drupol commented Mar 4, 2022

Cool ! Thanks @ondrejmirtes ! Looking forward to test this in the next release.

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue Mar 4, 2022
@AlexandruGG
Copy link
Author

Nice one, thank you very much 🚀

@github-actions
Copy link

github-actions bot commented Apr 5, 2022

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 Apr 5, 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

4 participants