This Question is Possibly Answered

1 "correct" answer available (4 pts) 1 "helpful" answer available (2 pts)
1 Replies Last post: Jul 5, 2013 4:49 PM by Andriy Bazanov  
Alexey Bulenkov Newbie 5 posts since
Jul 27, 2010
Currently Being Moderated

Jul 5, 2013 3:41 PM

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(); 
}
Andriy Bazanov Master 3,910 posts since
May 28, 2010
Currently Being Moderated
Jul 5, 2013 4:49 PM in response to: Alexey Bulenkov
Re: Php-docs for class implements the Iterator interface

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();
}

More Like This

  • Retrieving data ...