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
Support for compound expressions #2515
Labels
Comments
It's a bit longer, but yes, you can rewrite the code using intermediate variables so there's no need to memoize the method call results: <?php
class Assessment {
public function getAttributeRoot(): ?string {
return rand(0, 1) ? 'root' : null;
}
}
class Project {
public function getAssessment(): ?Assessment {
return rand(0, 1) ? new Assessment : null;
}
}
function f(Project $project): int {
$assessment = $project->getAssessment();
if (is_null($assessment)) {
throw new RuntimeException('no assessment');
}
$attributeRoot = $assessment->getAttributeRoot();
if (is_null($attributeRoot)) {
throw new RuntimeException('no attribute root');
}
return strlen($attributeRoot);
} |
This should definitely work: <?php
class Assessment {
private ?string $root = null;
/** @psalm-mutation-free */
public function getRoot(): ?string {
return $this->root;
}
}
class Project {
private ?Assessment $assessment = null;
/** @psalm-mutation-free */
public function getAssessment(): ?Assessment {
return $this->assessment;
}
}
function f(Project $project): int {
if (($project->getAssessment() === null)
|| ($project->getAssessment()->getRoot() !== null)
) {
throw new RuntimeException('no assessment');
}
return strlen($project->getAssessment()->getRoot());
} |
@muglug you're awesome, I can confirm it works with the latest master version and the |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a compound expression as seen in the following example
I would expect psalm to be able to infer that if these two checks pass (I throw an exception in the if block), those returns cannot be null afterwards.
While this isn't supported yet, is there a way for me to rewrite the code as to have psalm be able to infer everything is safe without just suppressing the PossiblyNullReference?
The text was updated successfully, but these errors were encountered: