Hello. How to correctly describe the return type of the method in PHPDoc:

/** * @return \Generator */ public function getLayers() { foreach($this->layers as $layer) yield $layer; } } 

When used in a loop, PHPStorm does not understand which type it iterates:

 foreach ($this->getLayers() as $layer) $layer->... //PHPStorm: "я не знаю что это!" 

At the same time, if just returning an array, it would be possible to describe it like this:

 /** * @return LayerType[] */ public function getLayers() { return $this->layers; } 

This way, when iterating, it recognizes objects.

  • Yes, I read already. I thought maybe there is a more correct way. Now, it turns out that I am deceiving the end user by saying that I am returning an array of layers to him, actually returning the generator. - A1essandro
  • Unfortunately, generic drugs haven’t been delivered yet - etki
  • @Etki, would rather) - A1essandro

1 answer 1

You can combine types to satisfy both the code reader and PhpStorm:

 /** * @return \Generator|LayerType[] */ public function getLayers() { foreach($this->layers as $layer) yield $layer; } } 

A small addition. This syntax does mean that a function can return values ​​of different types. Therefore, specifically in this case, the reason for its use is better documented in addition to avoid ambiguous interpretation.