Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to document (phpdoc) generators (methods that yield)

What is the best way to document, for phpdocumentor2, a method that is a generator.

I don't think @return really works for yield, but I can't seem to find any proper alternative.

Is it just a matter of waiting for phpdoc to catch up?

like image 759
Mat-Locomotive Avatar asked May 06 '15 17:05

Mat-Locomotive


2 Answers

I went with @return Generator|SomeObject[], where SomeObject is the thing being yielded.

PhpStorm handles this well too, as it now normally hints Generator methods and when iterated it hints SomeObject methods.

(Still, I would prefer a native @yield.)

like image 179
Robbert Avatar answered Oct 31 '22 20:10

Robbert


From the PHP Manual:

When a generator function is called for the first time, an object of the internal Generator class is returned.

So strictly speaking, @return Generator would be correct, although not super descriptive of what you can expect to get back when you iterate over the generator.

like image 12
Maxime Rainville Avatar answered Oct 31 '22 20:10

Maxime Rainville