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

Callable type information is not preserved in class constants #5743

Open
danog opened this issue May 10, 2021 · 12 comments
Open

Callable type information is not preserved in class constants #5743

danog opened this issue May 10, 2021 · 12 comments

Comments

@danog
Copy link
Collaborator

danog commented May 10, 2021

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

@psalm-github-bot
Copy link

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

@orklah
Copy link
Collaborator

orklah commented May 10, 2021

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

@psalm-github-bot
Copy link

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

@orklah
Copy link
Collaborator

orklah commented Aug 10, 2021

Closing this as a duplicate of #942

@AndrolGenhald
Copy link
Collaborator

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

@orklah orklah reopened this Jan 20, 2022
@orklah
Copy link
Collaborator

orklah commented Jan 31, 2022

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

@AndrolGenhald
Copy link
Collaborator

AndrolGenhald commented Jan 31, 2022

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.

@psalm-github-bot
Copy link

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

@orklah
Copy link
Collaborator

orklah commented Jan 31, 2022

Oh yeah, right, forgot the inferred type has priority

@AndrolGenhald
Copy link
Collaborator

Opened #7547.

@kkmuffme
Copy link
Contributor

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

@psalm-github-bot
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants