-
-
Notifications
You must be signed in to change notification settings - Fork 864
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 key type not being checked for validity #3753
Comments
This is expected behaviour. See #3269 for more details. |
Shouldn't PHPstan complain about illegal types regardless of using @var or @param? As far as I understand, I expected it to complain like when you use an invalid typehint (for example a class that doesn't exist or incompatible types on inheritance): |
Please read the linked issue. Here are the main takeaways:
So you should enforce the array type when passing arrays into another property or function/method, or when returning from the current function/method, PHPStan otherwise currently doesn't support enforcing types of local variables. |
I'm not talking about enforcing the type of a variable. The issue I'm talking about is the usage of ilegal types. Take this example: <?php declare (strict_types=1);
/**
* @param InexistentClass|null $var
*/
function example (?InexistentClass $var): void { var_dump ($var); }
/** @var InexistentClass|null */
$test = null; phpstan will (rightfully) complain about both docblocks because the type InexistentClass is invalid (in this case it doesn't exist). Now look at this example: <?php declare (strict_types=1);
/**
* @param array<float, mixed> $var
*/
function example (array $var): void { var_dump ($var); }
/** @var array<float, mixed> */
$test = array (0.5 => 'test');
example ($test); This one passes all the phpstan checks on level 8 (even with the strict rules in use), yet the type |
Oh, I get what you’re complaining about now 😊 |
@ondrejmirtes Would it be appropriate to check for this in |
@leongersen Nope, that's a wrong place, that rule checks literal arrays in code. In order to implement this, we need to |
@ondrejmirtes Maybe I misunderstand, but |
@leongersen Good observation. This is fine and it's already how PHPStan works. If you have something in your PHPDoc that gets normalized (like |
Implemented: phpstan/phpstan-src@1de5de8 |
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. |
Bug report
Array keys in typehints are not being checked for validity
Code snippet that reproduces the problem
https://phpstan.org/r/465607c6-3434-4b3f-bfbe-9add36193243
Expected output
I expected PHPStan to complain about the array key not being integer or string, in fact other tools (like phpdocumentor) do complain about it
The text was updated successfully, but these errors were encountered: