Search code examples
phpphpstan

denoting optional parameters via annotations for phpstan


I have a class that has a __call method. The first parameter to the __call method, per the PHP docs, is the method name. If the method name is unsupported I throw an exception.

The problem with this is that if I do vendor/bin/phpstan analyze test.php --level=9, where test.php is a file that calls a method that ultimately invokes __call, then PHPStan will give a Call to an undefined method MyClass::myFunction(). error.

The way to workaround this is, as I understand it, is to add * @method mixed myFunction(string $a, int $b) in the DocBlock comment at the top of the class. But what if the second parameter is optional? eg. what if the second parameter has a default argument value (to use the terminology from https://www.php.net/manual/en/functions.arguments.php). How would I denote that for PHPStan?


Solution

  • what if the second parameter has a default argument value. How would I denote that for PHPStan?

    Same way as with normal PHP code:

    * @method mixed myFunction(string $a, int $b = 123)
    

    On-line playground link: https://phpstan.org/r/06f85674-b0c4-4dc7-935b-9dfa3196456e