psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Iterator stubs not validated and broken?

Open ondrejmirtes opened this issue 3 years ago • 3 comments

Hi, I just got this pull request https://github.com/phpstan/phpstan-src/pull/796 and I want PHPStan and Psalm's generic stubs to be compatible so I cross-checked how the stubs for these classes look here.

I noticed that Psalm's IteratorIterator looks like this:

/**
 * @template-covariant TKey
 * @template-covariant TValue
 * @template TIterator as Traversable<TKey, TValue>
 *
 * @template-implements OuterIterator<TKey, TValue>
 *
 * @mixin TIterator
 */
class IteratorIterator implements OuterIterator {

Which is the same as in PHPStan. But a class that extends it looks differently:


/**
 * @template-covariant TKey
 * @template-covariant TValue
 *
 * @template-extends IteratorIterator<TKey, TValue>
 */
class FilterIterator extends IteratorIterator {

Somehow Psalm doesn't care that IteratorIterator has three @templates and accepts just IteratorIterator<TKey, TValue>. Normally it's a reported error ("ERROR: MissingTemplateParam - 21:7 - Bar has missing template params when extending Foo , expecting 3") but not here.

PHPStan's stubs are validated regardless of the user's rule level with a few selected rules that make sense for stubs, so maybe Psalm should do the same.

ondrejmirtes avatar Nov 22 '21 10:11 ondrejmirtes

Hey @ondrejmirtes, can you reproduce the issue on https://psalm.dev ?

psalm-github-bot[bot] avatar Nov 22 '21 10:11 psalm-github-bot[bot]

Seems like Stubs are scanned but not analyzed. Due to how Psalm runs, I don't think we can just select what we want to check and what we don't want. Worse, when trying to analyze, Psalm sees that those classes already exists, emit an error and just stop the analysis.

I prepared a PR to fix the errors on stubs, but I'm not sure we can do better than that...

orklah avatar Nov 22 '21 19:11 orklah

The original issue with stubs is fixed, but we still need to find a way to validate them.

weirdan avatar Feb 12 '24 03:02 weirdan