Skip to content

Treat all possible dynamic method invocations the same way #4608

Closed

Description

@wouterj

Feature request

https://phpstan.org/r/5fe67304-fc83-41c3-84d1-0c019eb8b90a

$c = new class {
	public function abc(): void {}
};

$s = rand(0, 1) ? 'abc' : 'not_abc';

$c->{$s}();
call_user_func([$c, $s]);
[$c, $s]();

As far as I know, there are 3 ways to dynamically build a method name and invoke it. The (2) and (3) in the code example above are reported by PHPstan, whereas all three are reported in strict mode. As I think there is no semantical difference between them (I can be wrong here), I think it makes sense if PHPstan reports all of them the same way.

cc @dkarlovi

Activity

  1. ondrejmirtes commented on Feb 26, 2021

    @ondrejmirtes
    Member

    A similar treatment as has been done to properties also has to be done here: phpstan/phpstan-src@265afac

  2. phpstan-bot commented on Oct 13, 2021

    @phpstan-bot
    Contributor

    @wouterj After the latest commit in dev-master, 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(class@anonymous/tmp.php:3, 'abc'|'not_abc') given.
    -11: Trying to invoke array(class@anonymous/tmp.php:3, 'abc'|'not_abc') but it might not be a callable.
    +10: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{class@anonymous/tmp.php:3, 'abc'|'not_abc'} given.
    +11: Trying to invoke array{class@anonymous/tmp.php:3, 'abc'|'not_abc'} but it might not be a callable.
     
     PHP 7.1 – 7.4 (2 errors)
     ==========
     
    -10: Parameter #1 $function of function call_user_func expects callable(): mixed, array(class@anonymous/tmp.php:3, 'abc'|'not_abc') given.
    -11: Trying to invoke array(class@anonymous/tmp.php:3, 'abc'|'not_abc') but it might not be a callable.
    +10: Parameter #1 $function of function call_user_func expects callable(): mixed, array{class@anonymous/tmp.php:3, 'abc'|'not_abc'} given.
    +11: Trying to invoke array{class@anonymous/tmp.php:3, 'abc'|'not_abc'} but it might not be a callable.
    Full report

    PHP 8.0 (2 errors)

    Line Error
    10 `Parameter #1 $callback of function call_user_func expects callable(): mixed, array{class@anonymous/tmp.php:3, 'abc'
    11 `Trying to invoke array{class@anonymous/tmp.php:3, 'abc'

    PHP 7.1 – 7.4 (2 errors)

    Line Error
    10 `Parameter #1 $function of function call_user_func expects callable(): mixed, array{class@anonymous/tmp.php:3, 'abc'
    11 `Trying to invoke array{class@anonymous/tmp.php:3, 'abc'
  3. phpstan-bot commented on Sep 2, 2022

    @phpstan-bot
    Contributor

    @wouterj After the latest push in 1.8.x, 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(class@anonymous/tmp.php:3, 'abc'|'not_abc') given.
    -11: Trying to invoke array(class@anonymous/tmp.php:3, 'abc'|'not_abc') but it might not be a callable.
    -
    -PHP 7.1 – 7.4 (2 errors)
    -==========
    -
    -10: Parameter #1 $function of function call_user_func expects callable(): mixed, array(class@anonymous/tmp.php:3, 'abc'|'not_abc') given.
    -11: Trying to invoke array(class@anonymous/tmp.php:3, 'abc'|'not_abc') but it might not be a callable.
    +No errors
  4. phpstan-bot commented on Mar 19, 2025

    @phpstan-bot
    Contributor

    @wouterj After the latest push in 2.1.x, 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(class@anonymous/tmp.php:3, 'abc'|'not_abc') given.
    -11: Trying to invoke array(class@anonymous/tmp.php:3, 'abc'|'not_abc') but it might not be a callable.
    -
    -PHP 7.1 – 7.4 (2 errors)
    -==========
    -
    -10: Parameter #1 $function of function call_user_func expects callable(): mixed, array(class@anonymous/tmp.php:3, 'abc'|'not_abc') given.
    -11: Trying to invoke array(class@anonymous/tmp.php:3, 'abc'|'not_abc') but it might not be a callable.
    + 9: Call to an undefined method class@anonymous/tmp.php:3::not_abc().
    Full report
    Line Error
    9 Call to an undefined method class@anonymous/tmp.php:3::not_abc().
  5. phpstan-bot commented on Apr 7, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5409

  6. github-actions commented on May 9, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  7. locked as resolved and limited conversation to collaborators on May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions