vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.56k stars 660 forks source link

Possibly false UndefinedDocblockClass error #5224

Open githoober opened 3 years ago

githoober commented 3 years ago

https://psalm.dev/r/52d0b846cc

The error reads: "Docblock-defined class or interface K does not exist".

The provided template tag "@psalm-template K of object" does not seem to be recognized properly.

psalm-github-bot[bot] commented 3 years ago

I found these snippets:

https://psalm.dev/r/52d0b846cc ```php */ private $keyMap; /** @var array */ private $valueMap; /** * @param array $keyMap * @param array $valueMap */ private function __construct(array $keyMap, array $valueMap) { $this->keyMap = $keyMap; $this->valueMap = $valueMap; } /** @param list $data */ public static function of(array $data): HashSet { $keyMap = []; $valueMap = []; foreach ($data as [$keyObj, $value]) { $hash = spl_object_hash($keyObj); $keyMap[$hash] = $keyObj; $valueMap[$hash] = $value; } return new self($keyMap, $valueMap); } /** @return list */ public function keys(): array { return array_values($this->keyMap); } /** @return list */ public function values(): array { return array_values($this->valueMap); } } ``` ``` Psalm output (using commit b7792ab): ERROR: UndefinedDocblockClass - 36:16 - Docblock-defined class or interface K does not exist INFO: MixedInferredReturnType - 37:45 - Could not verify return type 'HashSet' for HashSet::of ```
fezfez commented 3 years ago

I got the same error, any news ? :sparkles:

weirdan commented 3 years ago

I don't think we can support templates in type aliases. E.g. what should happen if you import such a type with @psalm-import-type tuple from HashSet?

So the issue here is Psalm not explaining that to you in a meaningful way.

fox91 commented 3 years ago

I think I ran into the same error: https://psalm.dev/r/f71fc33269

An explanation/solution would help.

psalm-github-bot[bot] commented 3 years ago

I found these snippets:

https://psalm.dev/r/f71fc33269 ```php $inData * @return non-empty-array */ public static function convertData( array $inData ): array; } ``` ``` Psalm output (using commit 9222b24): ERROR: UndefinedDocblockClass - 17:15 - Docblock-defined class, interface or enum named Foo\TIn does not exist ```
orklah commented 3 years ago

@fox91 using class level template for static methods is forbidden

fox91 commented 3 years ago

Thanks @orklah, can you link reference please?

orklah commented 3 years ago

Initial comment: https://github.com/vimeo/psalm/issues/2697#issuecomment-578518859 Most recent discussion: https://github.com/vimeo/psalm/issues/6429#issuecomment-913710528