psalm
psalm copied to clipboard
Callable type information is not preserved in class constants
See https://psalm.dev/r/ac4c096eb4
I found these snippets:
https://psalm.dev/r/ac4c096eb4
<?php
class a {
/** @var list<callable(mixed): bool> */
public const a = ['is_array', 'is_int'];
}
$a = a::a;
/** @psalm-trace $a */;
$res = $a[0]();
/** @psalm-trace $res */;
Psalm output (using commit 859b4a2):
INFO: Trace - 8:23 - $a: array{"is_array", "is_int"}
INFO: MixedAssignment - 10:1 - Unable to determine the type that $res is being assigned to
INFO: Trace - 11:25 - $res: mixed
INFO: UnusedVariable - 10:1 - $res is never referenced or the value is not used
It's actually worse than that. I think @var doesn't work at all above a constant: https://psalm.dev/r/6e0994afa7
I found these snippets:
https://psalm.dev/r/6e0994afa7
<?php
class a {
/** @var bool */
public const a = ['is_array', 'is_int'];
}
$a = a::a;
/** @psalm-trace $a */;
$res = $a[0]();
/** @psalm-trace $res */;
Psalm output (using commit 859b4a2):
INFO: Trace - 8:23 - $a: array{"is_array", "is_int"}
INFO: MixedAssignment - 10:1 - Unable to determine the type that $res is being assigned to
INFO: Trace - 11:25 - $res: mixed
INFO: UnusedVariable - 10:1 - $res is never referenced or the value is not used
Closing this as a duplicate of https://github.com/vimeo/psalm/issues/942
@orklah Would you reopen this since it's not likely to be fixed with #942?
@AndrolGenhald I'm surprised this was not fixed though. Is there something special here?
We don't currently resolve literal strings to callables: https://psalm.dev/r/13a7a514f3. Not sure if that's an intentional choice or not.
It would also be fixed if we add some way to force the @var
tag type to always be used instead of the inferred type, I meant to create an issue to discuss that but I'm not sure I ever got around to it.
I found these snippets:
https://psalm.dev/r/13a7a514f3
<?php
$callable = "is_array";
$test = $callable([]);
/** @psalm-trace $test */;
Psalm output (using commit 2e01e9b):
INFO: MixedAssignment - 4:1 - Unable to determine the type that $test is being assigned to
INFO: Trace - 5:26 - $test: mixed
INFO: UnusedVariable - 4:1 - $test is never referenced or the value is not used
Oh yeah, right, forgot the inferred type has priority
Opened #7547.
Same issue also for regular constants: https://psalm.dev/r/53d81047a5
I found these snippets:
https://psalm.dev/r/53d81047a5
<?php
if ( defined( 'FOO' ) && is_int( FOO ) ) {
$a = FOO;
/** @psalm-trace $a */;
}
Psalm output (using commit d957ff2):
INFO: MixedAssignment - 4:3 - Unable to determine the type that $a is being assigned to
INFO: Trace - 5:27 - $a: mixed
INFO: UnusedVariable - 4:3 - $a is never referenced or the value is not used