Skip to content

Problem with definition of conditional return types #9890

Answered by orklah
Itatsi asked this question in Q&A
Discussion options

You must be logged in to vote

In theory, this is what you want:
https://psalm.dev/r/191c5d72a9

I made two changes:

  • I added an assertion on load() that affirms that fnc() will return an int if load() returned true
  • I documented the class as immutable

The last part is required by Psalm because it's not safe to try to assert types on methods that are subject to be changed.

However, your class is very much not immutable because you save things in a property.

That makes the whole pattern pretty risky in terms of static analysis (for example, you could imagine calling load a second time with an invalid input that would completely change the first assertion or you could call another method on $foo that could have an effect …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Itatsi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants