vimeo/psalm

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

Open

#8,281 opened on Jul 18, 2022

View on GitHub
 (5 comments) (0 reactions) (0 assignees)PHP (668 forks)batch import
Help wantedeasy problemsgood first issueinternal stubs/callmap

Repository metrics

Stars
 (5,369 stars)
PR merge metrics
 (Avg merge 3d 12h) (5 merged PRs in 30d)

Description

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

Contributor guide