vimeo / psalm

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

Use template on class template to annotate function that modifies property #10304

Open kkmuffme opened 1 year ago

kkmuffme commented 1 year ago

Basically what I'd like to do is annotate a function that sets the property in an object: https://psalm.dev/r/53eb00963c

When not using a function but manually assigning psalm correctly handles this https://psalm.dev/r/cdfb2f2b49 - which means this is purely an issue of annotation (or the lack thereof) and won't require additional logic on psalm's end I guess.

Is there any other way to achieve this?

psalm-github-bot[bot] commented 1 year ago

I found these snippets:

https://psalm.dev/r/53eb00963c ```php $arg * @return T */ function add( $arg ) { $arg->id = fromApi( $arg ); return $arg; } function fromApi( A $arg ): int { return rand( 0, 1000 ); } ``` ``` Psalm output (using commit 75fcfe3): ERROR: UndefinedDocblockClass - 15:11 - Docblock-defined class, interface or enum named T does not exist ERROR: UndefinedDocblockClass - 16:12 - Docblock-defined class, interface or enum named T does not exist INFO: MixedInferredReturnType - 16:12 - Could not verify return type 'T' for add INFO: UnusedParam - 23:21 - Param arg is never referenced in this method ```
https://psalm.dev/r/cdfb2f2b49 ```php id; /** @psalm-trace $x */; $a->id = 1234; $y = $a->id; /** @psalm-trace $y */; ``` ``` Psalm output (using commit 75fcfe3): INFO: Trace - 12:23 - $x: int|null INFO: Trace - 16:23 - $y: 1234 ```