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

Variable function support #2920

Open
VincentLanglet opened this issue Jan 29, 2020 · 9 comments · May be fixed by phpstan/phpstan-src#3553
Open

Variable function support #2920

VincentLanglet opened this issue Jan 29, 2020 · 9 comments · May be fixed by phpstan/phpstan-src#3553

Comments

@VincentLanglet
Copy link
Contributor

Feature request

Variable function

$foo = 'foo';

$this->$foo()

Does not look to be actually checked.

You can see here
https://phpstan.org/r/ad9416aa-e138-4825-a3f4-2e064955547c

call_user_func([$this, $a]);

Return an error

But

$this->$a

Does not (and should).

@phpstan-bot
Copy link
Contributor

@VincentLanglet PHPStan now reports different result with your code snippet:

@@ @@
+PHP 8.0 (2 errors)
+==========
+
+10: Parameter #1 $callback of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'a') given.
+20: Parameter #1 $callback of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'foo') given.
+
+PHP 7.1 – 7.4 (2 errors)
+==========
+
 10: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'a') given.
 20: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'foo') given.
Full report

PHP 8.0 (2 errors)

Line Error
10 Parameter #1 $callback of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'a') given.
20 Parameter #1 $callback of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'foo') given.

PHP 7.1 – 7.4 (2 errors)

Line Error
10 Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'a') given.
20 Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'foo') given.

@phpstan-bot
Copy link
Contributor

@VincentLanglet After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-10: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'a') given.
-20: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'foo') given.
+PHP 8.0 (2 errors)
+==========
+
+10: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
+
+PHP 7.1 – 7.4 (2 errors)
+==========
+
+10: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
Full report

PHP 8.0 (2 errors)

Line Error
10 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

PHP 7.1 – 7.4 (2 errors)

Line Error
10 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

@phpstan-bot
Copy link
Contributor

@VincentLanglet After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-10: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'a') given.
-20: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'foo') given.
+PHP 8.0 – 8.1 (2 errors)
+==========
+
+10: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
+
+PHP 7.1 – 7.4 (2 errors)
+==========
+
+10: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
Full report

PHP 8.0 – 8.1 (2 errors)

Line Error
10 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

PHP 7.1 – 7.4 (2 errors)

Line Error
10 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

@phpstan-bot
Copy link
Contributor

@VincentLanglet After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-10: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'a') given.
-20: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'foo') given.
+PHP 8.0 – 8.2 (2 errors)
+==========
+
+10: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
+
+PHP 7.1 – 7.4 (2 errors)
+==========
+
+10: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
Full report

PHP 8.0 – 8.2 (2 errors)

Line Error
10 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

PHP 7.1 – 7.4 (2 errors)

Line Error
10 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

@phpstan-bot
Copy link
Contributor

@VincentLanglet After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-10: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'a') given.
-20: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'foo') given.
+PHP 8.0 – 8.3 (2 errors)
+==========
+
+10: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
+
+PHP 7.1 – 7.4 (2 errors)
+==========
+
+10: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
Full report

PHP 8.0 – 8.3 (2 errors)

Line Error
10 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

PHP 7.1 – 7.4 (2 errors)

Line Error
10 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

@phpstan-bot
Copy link
Contributor

@VincentLanglet After the latest push in 1.12.x, PHPStan now reports different result with your code snippet:

@@ @@
-10: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'a') given.
-20: Parameter #1 $function of function call_user_func expects callable(): mixed, array($this(HelloWorld), 'foo') given.
+PHP 8.0 – 8.4 (2 errors)
+==========
+
+10: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
+
+PHP 7.1 – 7.4 (2 errors)
+==========
+
+10: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
+20: Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.
Full report

PHP 8.0 – 8.4 (2 errors)

Line Error
10 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

PHP 7.1 – 7.4 (2 errors)

Line Error
10 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'a'} given.
20 Parameter #1 $function of function call_user_func expects callable(): mixed, array{$this(HelloWorld), 'foo'} given.

@jlherren
Copy link

jlherren commented Oct 8, 2024

This would be a nice feature, especially that it already works amazingly well when using call_user_func(). PHPStan understands and validates the method calls when the name is being constructed out of constants: https://phpstan.org/r/3dce165f-e8ba-40f1-aab2-7cdd42db8a17

@staabm
Copy link
Contributor

staabm commented Oct 8, 2024

will have a look

@ondrejmirtes
Copy link
Member

This is a complex problem and we don't need to solve it now. Especially for dynamic code, which can often be rewritten not to be dynamic.

@staabm You don't need to carry everything on your shoulders, especially with the number of open PRs you already have :)

We solved a similar thing for properties, it can find errors with $this->{$foo} = $bar;. But we had to make sure that $foo and $bar combination is valid.

For example when $foo refers to property a, we need to make sure the type of $bar can be assigned to a in that code branch.

Which means that before analysing the method call, we'd have to correctly filter all the argument types properly.

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

Successfully merging a pull request may close this issue.

5 participants