A
A
Alex Pts2013-04-06 14:42:53
PHP
Alex Pts, 2013-04-06 14:42:53

Code highlighting using trait in php

Many IDEs provide code highlighting and suggest auto substitution options based on the phpdoc documentation.

When using trait in php, it is not always possible to describe in advance what type the method will return or what type the property stores.


For example:

trait T_Singleton {




    /**
     * Вернуть единственный экземпляр класса
     * 
     * @return
     */
    static public function getInstance() {
        ...
    }


}


When using this mixin, it is not known in advance from which class the mixin will be called and what type of object it should return. Of course, you can try to override it explicitly in each class to try and describe the type correctly, but this is extra code.

Question: can PhpStorm be made to understand the $instance type as the type of the same class from which it will be called? Maybe there is some keyword that would be understood in dynamic code highlighting as the name of the current class where the call comes from?

- UPD: Solution
Having searched more, I found a solution through the use of phpDOC directives for the class, where we describe a method that is actually not in the class, but highlighting based on it just works fine.

Add a comment before the class with the method and return type
/**
 * @static @method Template_View getInstance()
 */
class Template_View {
    trait T_Singleton;
    ...
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nazar Mokrinsky, 2013-07-24
@nazarpc

This is a bug, I recently noticed this too, created an issue in the PhpStorm tracker, please vote for a faster fix:
youtrack.jetbrains.com/issue/WI-19305

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question