Any way to condition the return type of a method, based on the return value of another method? #8259
-
Consider the following example: class ItemContainer
{
public function hasNew() : bool
{
/* ... */
}
public function getNew() : ?SomeItemType
{
/* ... */
}
} What I would like to be able to do, is to tell Psalm that in the following code, if ($container->hasNew())
$container->getNew()->useTheItem(); // Would like no error here, related to the nullable type. Is this possible? |
Beta Was this translation helpful? Give feedback.
Answered by
AndrolGenhald
Jul 13, 2022
Replies: 1 comment 2 replies
-
Method calls can be asserted when memoizeMethodCallResults is |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
rzvc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Method calls can be asserted when memoizeMethodCallResults is
true
or when the methods are@psalm-mutation-free
, but you can't call any possibly mutating methods between thehas()
call and theget()
call: https://psalm.dev/r/e33bfb35a9