vimeo / psalm

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

require-extends implications on the interface #10538

Open staabm opened 9 months ago

staabm commented 9 months ago

recently I worked on adding psalm-require-extends semantics into phpstan.

in phpstan we allowed to access public properties and public methods on the interface type as an implication of using psalm-require-extends. It seems this is something missing in psalm

this issue is meant to discuss whether we could achieve feature parity in this regard.

see https://psalm.dev/r/8aefff36a3 vs https://phpstan.org/r/707a92dc-d8e7-4749-9eb4-06f796e5e432

you might be interessted more test-cases which have been added in the following PRs:

psalm-github-bot[bot] commented 9 months ago

I found these snippets:

https://psalm.dev/r/8aefff36a3 ```php x; $test->y; $test->z; $test->doFoo(); } function testExtendedInterface(AnotherInterface $test): void { $test->x; $test->y; $test->z; $test->doFoo(); } interface AnotherInterface extends SampleInterface { } class SomeSubClass extends SomeClass {} class ValidClass extends SomeClass implements SampleInterface {} class ValidSubClass extends SomeSubClass implements SampleInterface {} class InvalidClass implements SampleInterface {} ``` ``` Psalm output (using commit a75d26a): ERROR: NoInterfaceProperties - 27:2 - Interfaces cannot have properties ERROR: NoInterfaceProperties - 28:2 - Interfaces cannot have properties ERROR: NoInterfaceProperties - 29:2 - Interfaces cannot have properties ERROR: UndefinedInterfaceMethod - 31:9 - Method Bug10302InterfaceExtends\SampleInterface::doFoo does not exist ERROR: NoInterfaceProperties - 36:2 - Interfaces cannot have properties ERROR: NoInterfaceProperties - 37:2 - Interfaces cannot have properties ERROR: NoInterfaceProperties - 38:2 - Interfaces cannot have properties ERROR: UndefinedInterfaceMethod - 40:9 - Method Bug10302InterfaceExtends\AnotherInterface::doFoo does not exist ERROR: MissingConstructor - 15:13 - Bug10302InterfaceExtends\SomeClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$x, but no constructor ERROR: MissingConstructor - 16:19 - Bug10302InterfaceExtends\SomeClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$y, but no constructor ERROR: MissingConstructor - 15:13 - Bug10302InterfaceExtends\SomeSubClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$x, but no constructor ERROR: MissingConstructor - 16:19 - Bug10302InterfaceExtends\SomeSubClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$y, but no constructor ERROR: MissingConstructor - 15:13 - Bug10302InterfaceExtends\ValidClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$x, but no constructor ERROR: MissingConstructor - 16:19 - Bug10302InterfaceExtends\ValidClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$y, but no constructor ERROR: MissingConstructor - 15:13 - Bug10302InterfaceExtends\ValidSubClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$x, but no constructor ERROR: MissingConstructor - 16:19 - Bug10302InterfaceExtends\ValidSubClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$y, but no constructor ```