custom completion rules for PHP?

Hey IntelliJ Community!

I am working on a large project with IntelliJ 12 using the PHP plugin, and I have quite a few classes that have some magic functions (using __call and __callStatic). I would love to be able to get autocompletion for these classes, as the magic methods provided can be statically determined with some computations.

For example, I have a model class that defines an array of relationships with other classes

class Store{
    public static $has_many = array(
        'product',
        'transactions',
    );
    
    public function __call($method, $param) {
        if (in_array($method, self::$has_many)) {
            // return relationship object
        }
        // magic for setters
    }  
}

The goal is to be able to do:

$my_store = // some store instance;
$my_store->p.. // <--- autocompletes to 'products()'

As an autocomplete extension, I would say that any Store instance should also have methods for all its relations (like $has_many, $belongs_to, etc.). It would also be great to do this sort of magic autocomplete for other classes that have deterministic rules to define what extra methods can be called on the class.

Some of my objects have tens of methods that I'd like to have autocomplete for, so explicitly defining them all is not a scalable option.

Thanks for the help!

2 comments

You can create a Live Template to do such.  See Help > IntelliJ IDEA > IntelliJ IDEA Usage Guidelines > Live Templates and its sub-pages for information (or the same information in web help).  You can go to File > Settings > [IDE Settings] Live Templates to create them. Be sure to give them a scope of PHP in the 'avaiable in' setting below the large text box (some people miss that setting).

<edit>
p.s. There are also File Templates for creating new files. See Help > IntelliJ IDEA > IntelliJ IDEA Usage Guidelines > File Templates
</edit>

0

Thanks for the suggestion, Mark.

I might have been unclear in my question. I am actually looking for the IntelliJ autocomplete support (where it brings up a list of all available functions on an object) rather than shortcuts for code templates.

Specifically, once I type $my_store-> I would see a list of all methods availabe, which would include product() even though it is not explicitly defined in the Store class.

0

Please sign in to leave a comment.