Php-docs for class implements the Iterator interface

Hello. How can I help PhpShtorm understand that the return type for the iteration is a Model?

I have model and collection:

class Model {     public function getName(){} } class ModelCollection implements \Iterator {     /**      * @var Model[]      */     private $_models = array();     private $_position = 0;     /**      * @return Model      */     public function current() {         return $this->_models[$this->_position];     }     /** ... other Iterator methods ... */ }


Now I write every time you iterate a return type. But wants the type is determined automatically or by php-doc current method or something else :)

$collection = new ModelCollection();

/**
* @var Model $model
*/
foreach($collection as $model) {
   echo $model->getName();
}

1 comment

Hi there,

I do not think it's possible to do this nicely ATM, otherwise this ticket would not exist:


But see if this will be any better solution for you:

/** @var ModelCollection|Model[] $collection */
$collection = new ModelCollection();




foreach($collection as $model) {
   echo $model->getName();
}

0

Please sign in to leave a comment.