-
-
Notifications
You must be signed in to change notification settings - Fork 863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Empty array passed to foreach #1826
Comments
This is essentially a dupe of #929. The only easy option is to allow people who write code this way to remove all knowledge of property types after any function or method call (but it needs to be a config that's off by default). |
I plan to solve this with pure methods that will have two features:
Getters will be pure by default, additional methods will be configurable. |
I don't think that approach is practical. There are lots of complicated use cases that don't have an obvious answer e.g. class C {
public ?int $x;
public function foo(int $x) : int {
$this->x = 5;
$this->maybePure();
return $this->x;
}
public function maybePure() : void {}
public function setNull() : void {
$this->x = null;
}
}
class D extends C {
public function maybePure() : void {
$this->setNull();
}
} |
There's also so many ways that functions can be called in PHP (e.g. magic getters, destructors) that aren't transparently obvious unless you have full type coverage. |
I think that your example verifies that my approach is useful, PHPStan would understand the code exactly as it behaves. But of course, if you put side-effects into your getters, it would be worse. |
if phpstan does semantics by method names, it would be consistent to also complain about side effects in getters. one side effect that i can think of off my head would be in a caching scenario, e.g. this code: https://github.com/symfony-cmf/Routing/blob/4762e60a04d9a1b4c825db0a0a864b307bbc8ec5/src/ChainRouter.php#L103-L119 (that method is not called |
Yes, that's a great idea! Although side-effects are really hard to detect. For example - if there's some lazy initialization in the getter, it looks like a side-effect, but isn't one. |
The assignment is now forgotten if |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Since updating from 0.10.6 to 0.11.1 we get false positives about supposedly empty arrays.
A minimal example to reproduce the problem is this:
IMHO the information that $foo->bar is empty should be discarded when we pass $foo to a method because that method might change the value of the object.
The same is when we call a method on the object itself:
The text was updated successfully, but these errors were encountered: