phpstan-nette icon indicating copy to clipboard operation
phpstan-nette copied to clipboard

Container::getComponents() returns incorrect type

Open jtojnar opened this issue 1 year ago • 3 comments

component-model 3.1.0 returns array Container::getComponents() when $deep === false:

https://github.com/nette/component-model/commit/7f613eed7f5e57b6bde2d0be1bfdbb7e161620b3

instead of Iterator:

https://github.com/phpstan/phpstan-nette/blob/8af94743efcc6d1e685f2ffd7ab279e39c96429c/stubs/ComponentModel/Container.stub#L11-L13

The return type should be something like

@phpstan-return (
    $deep is true
    ? ($filterType is null ? array<int|string, \Nette\ComponentModel\IComponent> : array<int|string, T>)
    : ($filterType is null ? \Iterator<int|string, \Nette\ComponentModel\IComponent> : \Iterator<int|string, T>)
)

I can open a PR but not sure what to do about older versions. Should I just add conflict with < 3.1.0?

jtojnar avatar May 17 '24 10:05 jtojnar

Or maybe we should even remove the stub since the arguments were deprecated in 3.1.0 and Nette has correct annotations for argument-less use.

jtojnar avatar May 17 '24 21:05 jtojnar

You can detect which version of Nette the project uses. This has similar approach: https://github.com/phpstan/phpstan-doctrine/blob/4058fdc7333cb9e46f8cf5b952cd9cb59658f2e5/src/Stubs/Doctrine/StubFilesExtensionLoader.php#L75-L84

ondrejmirtes avatar May 18 '24 04:05 ondrejmirtes

Thanks for the pointer, I opened https://github.com/phpstan/phpstan-nette/pull/142

jtojnar avatar May 18 '24 12:05 jtojnar