title: How to typehint generators in php?
body: <p>Assume you have a class like:</p>
  <pre><code>class MyClass
  {
      public function __construct(public int $id, public string $foo)
      {
      }
  }
  </code></pre>
  <p>and a generator creating multiple instances of that class like this:</p>
  <pre><code>$myGenerator = function (): \Generator {
       yield new MyClass(id: 21, foo: 'fnord');
       yield new MyClass(id: 13, foo: 'baz');
       yield new MyClass(id: 42, foo: 'Thanks for the fish');
  };
  </code></pre>
  <p>How can I typehint a generator function so that it infers the type of each element during iteration?</p>
  <pre class="lang-php prettyprint-override"><code>foreach ($myGenerator as $instance) {
      $instance-&gt; // no autocompletion in IDE as type is not known
  }
  </code></pre>
  <p>I don't want to do:</p>
  <pre class="lang-php prettyprint-override"><code>foreach ($myGenerator as $instance) {
      /** @var MyClass $instance */
      $instance-&gt; // now autocomplete works inside an IDE
  }
  </code></pre>
  <p>as it would pester each usage of the generator with redundant typehints. I want to provide it once at best on the level of the return type of the generator.</p>
  <p>Is there a better alternative to typehint a generator?</p>
  <p>In pseudo-code, I would expect of being able to do something along the lines of:</p>
  <pre class="lang-php prettyprint-override"><code>$myGenerator = function (): \Generator&lt;MyClass&gt; {...}
  </code></pre>
  <p>I assume php's type-system isn't up for this, I wouldn't mind using phpdocs / phpstan. Main use case is IDE support (in my case phpStorm though it should work beyond that).</p>