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

Make array_combine() generic #3351

Closed
b1rdex opened this issue May 28, 2020 · 4 comments
Closed

Make array_combine() generic #3351

b1rdex opened this issue May 28, 2020 · 4 comments

Comments

@b1rdex
Copy link
Contributor

b1rdex commented May 28, 2020

https://phpstan.org/r/59427afd-8dc6-4b14-8d40-2c52b5fc8d34

Currently, array_combine produces array|false. It should be enhanced with this:

/**
 * @template TKey
 * @template TValue
 * @param array<TKey> $keys
 * @param array<TValue> $values
 *
 * @return array<TKey, TValue>|false
 */
array_combine(array $keys, array $values);
@b1rdex
Copy link
Contributor Author

b1rdex commented Jun 3, 2020

@ondrejmirtes I'd like to contribute this but I don't get where should I put that stub?

@ondrejmirtes
Copy link
Member

Stub files (https://phpstan.org/user-guide/stub-files) are supported above userland classes, userland functions, userland methods, useland properties, builtin classes, builtin methods, built properties, but unfortunately not above builtin functions, there wasn't a use case until now.

So the support for that needs to be added first. For userland functions it was done here phpstan/phpstan-src@7f07659#diff-1cf42bada627951c5878a8529e3f81cc for builtin functions it would have to be done in https://github.com/phpstan/phpstan-src/blob/master/src/Reflection/SignatureMap/NativeFunctionReflectionProvider.php.

@phpstan-bot
Copy link
Contributor

@b1rdex PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.0 (2 errors)
+==========
+
 11: Cannot access offset 'a' on array<string, int>|false.
-13: Cannot access offset 'a' on array|false.
+24: Method HelloWorld::combine() never returns false so it can be removed from the return typehint.
+
+PHP 7.1 – 7.4 (1 error)
+==========
+
+11: Cannot access offset 'a' on array<string, int>|false.
Full report

PHP 8.0 (2 errors)

Line Error
11 `Cannot access offset 'a' on array<string, int>
24 Method HelloWorld::combine() never returns false so it can be removed from the return typehint.

PHP 7.1 – 7.4 (1 error)

Line Error
11 `Cannot access offset 'a' on array<string, int>

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

No branches or pull requests

3 participants