PHPStorm Dynamic static method typehint
How can I tell PhpStorm two (2) things:
#1 that the dynamic method (static or not) does exist
#2 return type and parameter types of the dynamic method
Example:
X::register('plus', function($a,$b) { return $a+$b; } );
echo X::plus(1,2), "\n";
In which X is something like this:
class X {
static $functions = [];
private function __construct() {}
public static function register($name,$callback) { X::$functions[$name]=$callback; }
public static function __callStatic($name,$arguments) { return call_user_func_array(X::$functions[$name], $arguments); }
}
请先登录再写评论。
Posted a bit too quickly. Currently the problem is that PhpStorm is complaining about the missing methods.
I know, I could do it like this, but that's not very dynamic. I am looking for something that could be written along with the code that actually adds the dynamic method. X is external, so I can not (nor do I want to) change that file.
If I only could do this:
or this
Or maybe I am missing something?
Hi there,
You cannot do that with PHPDoc in the middle of the code (ad-hoc). This can only be done at the class declaration level.
What you can do is declare your own X class with all such methods defined there, place such file in subfolder that will be used by the IDE only.
This is how https://github.com/barryvdh/laravel-ide-helper does when it describes such dynamic/magic methods in Laravel (Laravel has a lot of magic methods where you call static method but in reality it makes a call to the instance method, Laravel facades, Macroable classes (the same as your code); this package it creates a file named _ide_helper.php with all the stuff inside).
P.S. By default PhpStorm will complain on such "multiple class declarations", but that inspection can easily be disabled.
Thanks. I was afraid that this might be the current state of things.
Shame really, it is so close. It does recognize it as a static method "::plus(..)", only the class is missing. One day, perhaps.
You can also submit this to our tracker at https://youtrack.jetbrains.com/newIssue as a new feature request. The idea looks interesting.
@Dmitry Tronin
Not the same, but overall similar idea:
Submitted. https://youtrack.jetbrains.com/issue/WI-62435
Thanks