How to document magic methods (_call and _callStatic) for an IDE

After many happy years of coding in notepad ++ and sublime, I was advised to give a PHP IDE. I am trying phpStorm and it seems to me that this is good. Code completion and documentation is a great feature, but it doesn’t work for me when magic methods are used. Is there any work to make phpStorm understand what is going on in magic methods?

Our situation is something like this:

abstract class a { public static function __callStatic($method,$args) { if(strpos($method,"get_by_") === 0) { //do stuff } elseif(strpos($method,"get_first_by_") === 0) { //do stuff } elseif($method == "get_all") { //do stuff } } } class b extends a { // some more stuff } b::get_by_user_id(27); b::get_first_by_id(156); b::get_all(); 

The magic callStatic method allows you to get a collection of objects through 1 or more arguments that make up the function call.

I see that in these cases there is an @method operator, but phpStorm only picks up the first of these statements. In addition, I can only set the return type to mixed, where I would prefer to set it like any class it was called on (b in my example).

Any ideas or suggestions would be greatly appreciated, thanks.

+54
php phpstorm magic-methods documentation docblocks
Mar 26 '13 at 9:54 on
source share
2 answers

Use a class level PHPDoc comment - especially the @method tag - works fine in PhpStorm:

 /** * @method static someClass get_by_user_id(int $id) Bla-bla * @method static someClass get_first_by_id(int $id) */ abstract class a { ... 

In the above example:

  • @method - PHPDoc tag
  • static - indicates that this is a static method
  • someClass or $this - return type
  • get_by_user_id - method name
  • (int $id) - method signature: ([[type] [parameter]<, ...>])
  • Bla-bla - some optional description

More about @method :

PS While @method static works fine in PhpStorm (tells the IDE that the method is static), it may not be (yet?), Supported by the actual phpDocumentor tool (sorry, haven't used it for a while).




Alternatively : (in PhpStorm, of course) Settings | Inspections | PHP | Undefined | Undefined method --> Downgrade severity if __magic methods are present in class Settings | Inspections | PHP | Undefined | Undefined method --> Downgrade severity if __magic methods are present in class Settings | Inspections | PHP | Undefined | Undefined method --> Downgrade severity if __magic methods are present in class - this will not help at all when completing the code for such methods, but will not mark these magic methods as an "undefined method".




phpDocumentor ticket regarding the use of register / unqualified names for @property / @method (how this can be useful for documentation and how a little help can lead to an actual IDE when working with code completion):

+98
Mar 26 '13 at 10:17
source share

Partly related to the original question:

You can also define this in the phpstorm metafile. Here is an example factory method (v2016.3):

 // Define in .phpstorm.meta.php namespace PHPSTORM_META { $STATIC_METHOD_TYPES = [ \Factory::create('') => [], ]; } // Then use in code $factory = new \Factory(); $user = $factory->create(\User::class); // Here you get autocomplete. $user->subscribe(); 

Thus, you do not need to execute docblock every opportunity when magic happens.

See docs for more details.

+3
Nov 27 '16 at 10:28
source share



All Articles