vimeo/psalm

Psalm is not able to infer types from doc blocks of Google\Protobuf with 'protobuf' extension installed

Open

#8281 aperta il 18 lug 2022

Vedi su GitHub
 (5 commenti) (0 reazioni) (0 assegnatari)PHP (668 fork)batch import
Help wantedeasy problemsgood first issueinternal stubs/callmap

Metriche repository

Star
 (5369 star)
Metriche merge PR
 (Merge medio 3g 12h) (5 PR mergiate in 30 g)

Descrizione

Hi, I have the protobuf extension installed in container in which I run Psalm, this causes psalm to not be able to infer types from Google\Protobuf classes, resulting in MixedAssignment and other errors, please see example below:

ERROR: MixedAssignment - index.php:3:1 - Unable to determine the type that $t is being assigned to (see https://psalm.dev/032)
$t = (new \Google\Protobuf\Timestamp())->setSeconds(0)->setNanos(0);

If I remove the protobuf extension, Psalm no longer reports those errors.

I've created a repository with reproducible example here: https://github.com/benkelukas/psalm-google-protobuf-issue

For now, I'm using container without protobuf for Psalm, but somebody might not have the options, so wanted to leave this here in case somebody else has this weird problem.

I've done some digging as well, and from my very much limited knowledge of Psalm, it seems that with the protobuf extension, the Google\Protobuf classes are not located via composer, but analyzed using reflection instead, and these classes do not have return types specified in actual code, but rather via annotations, so maybe that is what is breaking this? If you compare output of --debug-by-line with the extension and without it is different when processing the Google\Protobuf classes.

Some info on my environment:

  • Psalm version: 4.24.0
  • PHP: 8.1.1

Guida contributor