Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPDoc and __callStatic

Tags:

tl;dr

What is the correct way to annotate (in PHPDoc) functions implemented via __callStatic? More important: is there a way that will make NetBeans and PHPStorm understand that these are static methods?

Motivation

If you want the bigger picture, here's how I got to this question.

Problem: In my current project we have a ton of classes that should really be singletons (DB proxies and the like). Needless to say, we have at least a few hundred require_once and $foo = new FooProxy(); lines.

Solution: I created a Loader class to solve this, using the __callStatic magic method so we can just say $foo = Loader::FooProxy();. It's perfect for our purposes, but:

Problem: This way there's obviously no type hinting in either IDE used in the team.

Solution: Every module defines a subclass of Loader, adding methods that just route to __callStatic.

Problem: Adding actually interpreted code just for auto-completion's sake is not acceptable (this could be argued, but let's accept it for the time being).

Solution: Let's not add any real methods, only declare the methods in PHPDoc like this:

<?php
/**
 * @method FooProxy FooProxy()
 */
 class BarLoader extends Loader {}
?>

Problem: FooProxy is not a static method. None of the following make it static either:

<?php
/**
 * @static
 * @method FooProxy FooProxy()
 */

///////////////

/**
 * @static @method A A()
 * @method static A A()
 * @method A static A()
 * @method A A() static
 */

Making the class abstract makes no difference. About an hour of Google'ing turned up no solutions. The primary goal is to make the IDEs aware of these functions; having correct PHPDoc is not really a necessity.

like image 315
abesto Avatar asked May 12 '11 18:05

abesto


1 Answers

Well, PhpStorm 3.0 will accept

@method static type name() description

See relevant feature request http://youtrack.jetbrains.net/issue/WI-4051

like image 138
Alexey Gopachenko Avatar answered Nov 09 '22 15:11

Alexey Gopachenko