phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

Method return value not recognized when calling an other method in between

Open gregor-tb opened this issue 3 years ago • 2 comments
trafficstars

Code snippet that reproduces the problem

https://phpstan.org/r/88b4ba76-8196-4568-9da7-943ea5b5695b

Expected output

Should detect valid not-null array here, as it does when you remove $this->doSomething(); here.

Did PHPStan help you today? Did it make you happy in any way?

Working hard to increase 2 levels this week :)

gregor-tb avatar Feb 08 '22 13:02 gregor-tb

PHPStan forgets that you set $this->member = [] because $this->doSomething() is void and therefore impure (it might change the property type to something else).

This might be solved in the future but right now it isn't.

ondrejmirtes avatar Feb 08 '22 14:02 ondrejmirtes

@gregor-tb After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
-18: Method HelloWorld::getIT() should return array but returns array|null.
+18: Method HelloWorld::getIT() should return array<mixed> but returns array<mixed>|null.
Full report
Line Error
18 `Method HelloWorld::getIT() should return array but returns array

phpstan-bot avatar Oct 12 '24 15:10 phpstan-bot