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

array_chunk return type should take into account the $preserve_keys argument #7277

Closed
gnutix opened this issue May 21, 2022 · 3 comments · Fixed by phpstan/phpstan-src#1356
Closed

Comments

@gnutix
Copy link

gnutix commented May 21, 2022

Feature request

Hello there. While adding some types and types' tests for a wrapper function around array_chunk() in our codebase, I noticed that the return type was always array<array> (not very precise), and it does not account for the $preserve_keys argument.

Example: https://phpstan.org/r/ccba8215-391a-43bf-b0c3-1bd779a1c16c

I'm not sure if this should be fixed using conditional return types or with an extension.

Talking about extensions, I wonder if PHPStan could easily go one step further and be able to infer the specific values of the array (if it is static/known), like below ? (PS: I know it sometimes provides types that are that specific, that's why I think of this, but I don't know how easy it is to do it in this scenario)

$array = [
    'test1' => 'this is test 1',
    'test2' => 'this is test 2',
    'test3' => 'this is test 3',
    'test4' => 'this is test 4',
];
array_chunk($array, 2); // array{0: array{0: 'this is test 1', 1: 'this is test 2'}, 1: array{0: 'this is test 3', 1: 'this is test 4'}}
array_chunk($array, 2, true); // array{0: array{test1: 'this is test 1', test2: 'this is test 2'}, 1: array{test3: 'this is test 3', test4: 'this is test 4'}}

Not sure if it's "that useful" either. Anyway, it's just an idea. 😉

(I don't currently have the time to work on an implementation, but maybe someone will.)

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

(I have a bunch of small issues to create, sorry if I don't come up with something new for each of them 🙏 )

@gnutix gnutix changed the title array_chunk return type does not take into account $preserve_keys argument array_chunk return type should take into account the $preserve_keys argument May 21, 2022
@herndlm
Copy link
Contributor

herndlm commented May 23, 2022

Currently no return type extension for array_chunk exists at all. I think this can be solved relatively easily by creating one and using mostly already existing features from ConstantArrayType. E.g. slice and reindex (for $preserve_keys).
I can take a look at this next.

@gnutix
Copy link
Author

gnutix commented May 29, 2022

Thanks @herndlm for your work!

@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 Jun 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants