vimeo / psalm

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

private constructor with __callStatic fails with PropertyNotSetInConstructor #6364

Open pauljohnston2009 opened 3 years ago

pauljohnston2009 commented 3 years ago

using __callStatic with a private constructor errors with PropertyNotSetInConstructor

https://psalm.dev/r/6881a89db9

I figured out that it can be fixed it in a few ways don't use the magic __callStatic method (existing test that this works is already included https://github.com/vimeo/psalm/blob/0317f1dbece01849bbf9e51acd816cfe536e2a54/tests/PropertyTypeTest.php#L1719) eg. https://psalm.dev/r/aec125745e

or making the constructor public eg. https://psalm.dev/r/217a77d382

I expected the the magic method would not make a difference, and should be treated like any other static method.

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

I found these snippets:

https://psalm.dev/r/6881a89db9 ```php setA(); } private function setA() : void { $this->a = 5; } public static function __callStatic(string $function, array $args): self { return new static; } } class Concrete extends Base {} ``` ``` Psalm output (using commit 9222b24): ERROR: PropertyNotSetInConstructor - 15:7 - Property Concrete::$a is not defined in constructor of Concrete or in any methods called in the constructor ```
https://psalm.dev/r/aec125745e ```php setA(); } private function setA() : void { $this->a = 5; } public static function build(string $function, array $args): self { return new static; } } class Concrete extends Base {} ``` ``` Psalm output (using commit 9222b24): No issues! ```
https://psalm.dev/r/217a77d382 ```php setA(); } private function setA() : void { $this->a = 5; } public static function __callStatic(string $function, array $args): self { return new static; } } class Concrete extends Base {} ``` ``` Psalm output (using commit 9222b24): No issues! ```