How to get hinting / autocompletion type for objects that came out of an array?

In my structure, I have a method that returns an array with objects. It looks something like this:

    /**
     * @return array Array with Action objects
     */
    public function getActions() {
        return $this->actions;
    }

The user of this Classwill receive a call to doc-popup when calling this method, which only says that this method will return an array. But this! Then, when the user gets the object from the array, the IDE is just stupid and doesn't know / suggest / autofill anything.

Is there anything I can do as an infrastructure developer to make life easier for users of the framework? Or what else can I suggest they do, so their IDE knows which object is returned from the array?

Or in general: How to let the IDE know which object came from the array so that it can suggest what methods are available for this object?

+3
5

getActions(), :

$actions = $o->getActions();

/* @var Action $action */
foreach ( $actions as $action ) {
    ...
}
+4

, , , :

$my_object = new myClass();
$my_array[567] = $my_object;

, - this->actions . IDE .

+3

php - / , . IDE , . :

array ( 1, "string", someObject, array (1,"string",3) );
+1

JetBrains new WebIDE , @return ElementType []. - .

+1

As Alexey Gopachenko said, some new IDEs (as well as phpdoc documentation ) support sintax@return ElementType[]

/**
 * @return MyObj[]
 */
function getObjects() { return [new MyObj(), new MyObj()]; }

In your example:

/**
 * @return Action[]
 */
public function getActions() {
    return $this->actions;
}
0
source

Source: https://habr.com/ru/post/1727153/


All Articles