vimeo / psalm

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

Generic class with static return type coming from a interface #7204

Open jenshaase opened 2 years ago

jenshaase commented 2 years ago

Hi,

I have defined an interface with the return type static. Afterwards I implemented a generic class that uses the interface. When I call the interface method the type is not correctly resolved. See the example:

https://psalm.dev/r/b59404794e

I fix the error by added a doc comment in the class implementation:

class Collection implements CopyInterface {
  /** @return static */                 # <--- This line solves the problem
  public function copy(): static {
    // No real implementation
    return $this;
  }
}

Is this an intended behavior?

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

I found these snippets:

https://psalm.dev/r/b59404794e ```php x = $x; } public function copy(): static { // No real implementation return $this; } } /** @var Collection $intCollection */ $intCollection = new Collection([1]); // The following function is only to check the inferred types /** @param Collection $x */ function checkCollection($x): string { return get_class($x); } checkCollection($intCollection); checkCollection($intCollection->copy()); ``` ``` Psalm output (using commit f2db139): INFO: MixedArgumentTypeCoercion - 36:17 - Argument 1 of checkCollection expects Collection, parent type Collection&Collection provided ```