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
Unable to infer type when using a getter #1157
Comments
Some people would disagree with you, like @muglug 😊 In the future, I want to have a concept of "pure" methods - has no side effects, returns the same thing unless you call an impure method in the object meanwhile. I'll add Right now, you can help yourself with a hack:
...as a more explicit check still works. |
Thanks for the quick fix! |
Yeah, this is a risky thing for a static analysis tool to assume, I think. I decided that people should explicitly have to opt-in (via a config flag) to this behaviour. However, there's other behaviour that I think is less risky that I decided people should opt out of (via a different config flag). With that config flag on, this code class X {
/** @var ?int **/
private $x;
public function getX(): int {
$this->x = 5;
$this->foo();
return $this->x;
}
public function foo(): void {}
} must be rewritten as class X {
/** @var ?int **/
private $x;
public function getX(): int {
$x = $this->x = 5;
$this->foo();
return $x;
}
public function foo(): void {}
} to pass checks, because the function As I said, Psalm doesn't force you to write code like that by default, but Hack does, and I like the absolute strictness of it (no side-effects allowed). I ticketed a similar (opt-in) feature for PHPStan here: #929 |
@ondrejmirtes I understand your point. |
@ondrejmirtes I understand that detecting pure methods may be hard. However, I don't understand why |
@JanJakes Yeah, it's currently a hack that will be solved later with pure functions/methods. I can't think of any other example like this. |
@ondrejmirtes Thanks for the clarification. In the meantime I might be better not to assume anything (also for |
@ondrejmirtes Great to hear that you're considering pure functions! Just wanted to add a side note that whilst pure functions in the traditional sense do provide a solution to a related problem, it would likely not fix the problem in the common case of getters with setters (which can still be fixed by putting it in a local variable, as already suggested). This is because pure functions should not just not produce any side effects, but also return the same return value for the exact same set of arguments. This, of course, depends on your definition of pure functions, as you may also see pure functions as those just not having side effects. In the mentioned traditional sense, as a getter has no arguments, the return value should thus be constant (e.g. Just wanted to add these two cents in the hope the difference will be made clear in the naming if it is implemented 😄 . |
Fixed: phpstan/phpstan-src@d4edc59 |
Closes phpstan/phpstan#4588 Closes phpstan/phpstan#4091 Closes phpstan/phpstan#3382 Closes phpstan/phpstan#4177 Closes phpstan/phpstan#2288 Closes phpstan/phpstan#1157
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. |
Summary of a problem or a feature request
I encountered this problem when checking this little piece of code inside a Symfony controller:
PHPStan should infer that, since we checked the getter inside the
if
, we can safely exclude that the getter returnsnull
here.Code snippet that reproduces the problem
The code to reproduce the issue is here: https://phpstan.org/r/10c11895e307276cd46b1501d63e1cba
Expected output
The expected output should be no errors, instead we get
Cannot call method format() on DateTimeInterface|null.
The text was updated successfully, but these errors were encountered: