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
Incorrect PossiblyUndefinedVariable
issue reporting
#8361
Comments
I found these snippets: https://psalm.dev/r/ef08112a04<?php
final class Foo
{
/**
* @template T of non-empty-string
* @param non-empty-list<T> $aliases
* @return non-empty-array<T,list<non-empty-string>>
*/
private function detectIndicesForAliases(array $aliases): array
{
/** @var non-empty-array<T,list<non-empty-string>> $indices */
$indices = array_combine(
$aliases,
array_fill(0, count($aliases), [])
);
foreach ($aliases as $alias) {
try {
/** @var list<non-empty-string> $indicesByAlias */
$indicesByAlias = array_keys([]);
} catch (Throwable $throwable) {
if ($throwable->getCode() === 123) {
continue;
}
throw $throwable;
}
$indices[$alias] = $indicesByAlias;
}
return $indices;
}
}
|
Will almost certainly be fixed by #7688, but I need to find some time to get back to it. The last I checked I had to also completely redo how we're handling variables defined in |
I had a similar issue regarding a try-catch block: https://psalm.dev/r/b1102ead61. In this example, psalm does not correctly track the value of |
I found these snippets: https://psalm.dev/r/b1102ead61<?php
function returnsString(): string
{
$exception = null;
$string = null;
try {
$string = generatesStringOrThrows();
} catch (Exception $exception) {
}
// Imagine we're doing other stuff here
if ($exception !== null) {
throw $exception;
}
return $string;
}
function generatesStringOrThrows(): string
{
if (rand(0, 1) == 1) {
return 'foo';
}
throw new Exception('nope');
}
|
@alcaeus That's a separate issue that comes up now and then, having types of variables be dependent on type checks of other variables is really complicated and not likely to be supported any time soon: https://psalm.dev/r/f6d94dc39e |
I found these snippets: https://psalm.dev/r/f6d94dc39e<?php
/** @var mixed */
$foo = null;
$isString = false;
if (is_string($foo)) {
$isString = true;
}
if ($isString) {
/** @psalm-trace $foo */; // Technically should be `string`
}
|
It seems that psalm somehow does not realize that due to the
try-catch
block, the variable$indicesByAlias
is always set in case there are no errors.Somehow, the
catch
is not parsed properly as we do usecontinue
in there. Usingcontinue
should continue the loop and thus, there should be no codepath which actually leads to the$indicesByAlias
being undefined.https://psalm.dev/r/ef08112a04
The text was updated successfully, but these errors were encountered: