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

Allow calling methods by a literal string variable #8538

Open
danog opened this issue Oct 6, 2022 · 5 comments
Open

Allow calling methods by a literal string variable #8538

danog opened this issue Oct 6, 2022 · 5 comments

Comments

@danog
Copy link
Collaborator

danog commented Oct 6, 2022

https://psalm.dev/r/501d719ba6

@psalm-github-bot
Copy link

psalm-github-bot bot commented Oct 6, 2022

I found these snippets:

https://psalm.dev/r/501d719ba6
<?php
class a {
    public static function a(): int { return 0; }
	public static function b(): string { return "test"; }
}

$c = random_int(0, 1) ? 'a' : 'b';
/** @psalm-trace $c */;
$c = a::$c();
/** @psalm-trace $c */;
Psalm output (using commit 028ac7f):

INFO: Trace - 8:23 - $c: 'a'|'b'

INFO: MixedAssignment - 9:1 - Unable to determine the type that $c is being assigned to

INFO: Trace - 10:23 - $c: mixed

INFO: UnusedVariable - 9:1 - $c is never referenced or the value is not used

@orklah
Copy link
Collaborator

orklah commented Oct 6, 2022

I'm quite surprised it doesn't work. It's not even an issue with the 'a'|'b' thing. Even with 'a' it fails. It shouldn't be difficult as I think something close to that must be already working for callable-arrays resolution

@AndrolGenhald
Copy link
Collaborator

Related: #5743 (comment)

We don't currently resolve literal strings to callables: https://psalm.dev/r/13a7a514f3. Not sure if that's an intentional choice or not.

@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 028ac7f):

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 Oct 7, 2022

I don't think this was intentional, more like a nice to have feature that never made it.

Now that we had an RFC to declare what we can and can't do with callable-arrays, it makes even more sense to support all that is still relevant

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

3 participants