psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Callable type information is not preserved in class constants

Open danog opened this issue 3 years ago • 12 comments

See https://psalm.dev/r/ac4c096eb4

danog avatar May 10 '21 10:05 danog

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

psalm-github-bot[bot] avatar May 10 '21 10:05 psalm-github-bot[bot]

It's actually worse than that. I think @var doesn't work at all above a constant: https://psalm.dev/r/6e0994afa7

orklah avatar May 10 '21 10:05 orklah

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

psalm-github-bot[bot] avatar May 10 '21 10:05 psalm-github-bot[bot]

Closing this as a duplicate of https://github.com/vimeo/psalm/issues/942

orklah avatar Aug 10 '21 17:08 orklah

@orklah Would you reopen this since it's not likely to be fixed with #942?

AndrolGenhald avatar Jan 20 '22 23:01 AndrolGenhald

@AndrolGenhald I'm surprised this was not fixed though. Is there something special here?

orklah avatar Jan 31 '22 21:01 orklah

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.

AndrolGenhald avatar Jan 31 '22 21:01 AndrolGenhald

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

psalm-github-bot[bot] avatar Jan 31 '22 21:01 psalm-github-bot[bot]

Oh yeah, right, forgot the inferred type has priority

orklah avatar Jan 31 '22 22:01 orklah

Opened #7547.

AndrolGenhald avatar Jan 31 '22 22:01 AndrolGenhald

Same issue also for regular constants: https://psalm.dev/r/53d81047a5

kkmuffme avatar Sep 17 '22 20:09 kkmuffme

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

psalm-github-bot[bot] avatar Sep 17 '22 20:09 psalm-github-bot[bot]