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
Flaw in the co-dependant variables types #7249
Labels
Comments
I found these snippets: https://psalm.dev/r/9bc618221f<?php
function scope(){
try {
$completion_data = getThingOrNullOrThrow();
} catch (Exception $e) {
return new stdClass();
}
try {
$type_context = getThingOrNullOrThrow();
if (!$completion_data && !$type_context) {
return new stdClass(); //HERE!
}
} catch (Exception $e) {
return new stdClass();
}
if ($completion_data) {
// do something
} else {
/** @psalm-trace $type_context */
// $type_context can't be null here or else we would have entered the conditional "HERE!" because both variables are false
}
}
/**
* @return string|null
* @throws Exception
*/
function getThingOrNullOrThrow(){
}
https://psalm.dev/r/ab2b600564<?php
function scope(){
try {
$completion_data = getThingOrNullOrThrow();
} catch (Exception $e) {
return new stdClass();
}
try {
$type_context = getThingOrNullOrThrow();
} catch (Exception $e) {
return new stdClass();
}
if (!$completion_data && !$type_context) {
return new stdClass(); //HERE!
}
if ($completion_data) {
// do something
} else {
/** @psalm-trace $type_context */
// $type_context can't be null here or else we would have entered the conditional "HERE!" because both variables are false
}
}
/**
* @return string|null
* @throws Exception
*/
function getThingOrNullOrThrow(){
}
|
Psalm now infers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the example here: https://psalm.dev/r/9bc618221f
Psalm doesn't detect that $type_context can't be null because we're in a scope where $completion_data is null and we already stated that both can't be null.
Psalm manages to make it work if the conditional is outside the try/catch: https://psalm.dev/r/ab2b600564.
This is pretty low priority because it's an edge case of a complex module and the code, while correct, is a little confusing anyway.
Found by: #7247
The text was updated successfully, but these errors were encountered: