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

ReflectionClass::newInstanceArgs() - error on argument type #8679

Closed
s3b4stian opened this issue Jan 9, 2023 · 7 comments
Closed

ReflectionClass::newInstanceArgs() - error on argument type #8679

s3b4stian opened this issue Jan 9, 2023 · 7 comments
Labels
Milestone

Comments

@s3b4stian
Copy link

Bug report

PHPStan version: 1.9.8

Analyzing a piece of code with ReflectionClass::newInstanceArgs(), PHPStan report an error about the type of the method argument.

newInstanceArgs expects an array with int keys and mixed values, array<int, mixed> but since PHP 8.0 it is possible to use named arguments, in this case the type of the array keys is string and a type array<string, mixed> should be also valid.

Code snippet that reproduces the problem

I tested a piece of code both in PHPStan Playground and in 3v4l.org

https://phpstan.org/r/ddada382-718d-4dd0-b495-b545ddddb151
https://3v4l.org/TAJHT

In 3v4l.org the code snippet works as expected.

Expected output

No errors instead of the one at line 27.

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

I found years old bugs on my code thanks to this tool!

@staabm
Copy link
Contributor

staabm commented Jan 9, 2023

this might be as simple as fixing the ReflectionClass stub:
https://github.com/phpstan/phpstan-src/blob/8fbeb9e4ed0c703fb9237362d34d37e7c81f6df7/stubs/ReflectionClass.stub#L34

you could give it a try and create a PR, and corresponding tests

@s3b4stian
Copy link
Author

Great, I saw it but wasn't sure it was the right one to make changes to. I didn't go deep in PHPStan internals. I'll try to do the pull request.

@staabm
Copy link
Contributor

staabm commented Jan 9, 2023

Fwiw, in psalm they seem to fix the same issue atm:

vimeo/psalm#9085

@s3b4stian
Copy link
Author

Before changing the code, I was doing a little reflection:

Changing the data type should not break the compatibility if PHPStan being used to analyze php code for a version prior to PHP 8.0, I did some tries and i saw that using an associative array with string keys isn't a problem because arguments are passed in any case positionally.

array<int|string, mixed>

  • always positionally in PHP prior 8.0
  • positionally for int keys, named for string keys since PHP 8.0

Locally I have updated the stub, and the tool seem work as expected on my code. I have to identify how to test this change and update unit tests.

@s3b4stian
Copy link
Author

Fwiw, in psalm they seem to fix the same issue atm:

vimeo/psalm#9085

It seems to be doing just that, I hadn't thought to check the other similar tools.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#2176

@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 Feb 12, 2023
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