-
Notifications
You must be signed in to change notification settings - Fork 671
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
Incorrect Const Array Type #7729
Comments
I found these snippets: https://psalm.dev/r/81398074e3<?php
class Foo {
public const VARS = [
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'y', // REMOVE THIS ARRAY ELEMENT TO MAKE THIS PASS
];
}
/** @param list<string> $vars */
function foo(array $vars): void {
print_r($vars);
}
foo(Foo::VARS);
|
There is indeed a limit in the number of offset Psalm allow in a shape. But the inference could be improved in this case to infer |
Thanks for the response! Do you happen to have have any workarounds to suggest in this case please? It doesn't seem possible to override this ... /** @var list<string> */
public const VARS = [ |
I found these snippets: https://psalm.dev/r/581ff3a3cd<?php
class Foo {
/** @var list<string> */
public const VARS = [
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'y', // REMOVE THIS ARRAY ELEMENT TO MAKE THIS PASS
];
}
/** @param list<string> $vars */
function foo(array $vars): void {
print_r($vars);
}
foo(Foo::VARS);
|
Just wait a few days and update Psalm to the next version 😄 |
We should also probably fix it so that suppression works: https://psalm.dev/r/ecec0a4f0f |
I found these snippets: https://psalm.dev/r/ecec0a4f0f<?php
class Foo {
/** @psalm-suppress ArgumentTypeCoercion */
public const VARS = [
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'y', // REMOVE THIS ARRAY ELEMENT TO MAKE THIS PASS
];
}
/** @param list<string> $vars */
function foo(array $vars): void {
print_r($vars);
}
foo(Foo::VARS);
|
Why add that here? Shouldn't it be here: https://psalm.dev/r/f48dd3e62a ? |
I found these snippets: https://psalm.dev/r/f48dd3e62a<?php
class Foo {
public const VARS = [
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'x',
'y', // REMOVE THIS ARRAY ELEMENT TO MAKE THIS PASS
];
}
/** @param list<string> $vars */
function foo(array $vars): void {
print_r($vars);
}
/** @psalm-suppress ArgumentTypeCoercion */
foo(Foo::VARS);
|
Edit: nevermind I'm totally wrong, thought this was something else that's popped up before. |
https://psalm.dev/r/81398074e3
(I don't know Psalm but I'm guessing that...) From the above example, Psalm is trying to automatically determine the type of the class constant
Foo::VARS
(which should belist<string>
, which works up until a certain length, but then returns a different type, causing it not to be the intendedlist<string>
.Remove the last element from the above example and Psalm passes correctly.
Is there a limit here in Psalm? If so is there any way to work around this (annotating the
const
does not seem to have any effect).Thanks!
The text was updated successfully, but these errors were encountered: