psalm
psalm copied to clipboard
Weird !falsy assertion paradox when using template conditionals
https://psalm.dev/r/46b3483706, but the non-templated version works fine: https://psalm.dev/r/46b3483706 Changing the $onlyType assertion to a non-null assertion also fixes it: https://psalm.dev/r/55b41e85e3
I found these snippets:
https://psalm.dev/r/46b3483706
<?php
/**
* @param array<array{type: string}> $v
* @param "prof"|"organization"|null $onlyType
* @return array<array{type: string}>
*/
function getOwnerObjects(array $v, ?string $onlyType = null): array {
$ok = [];
foreach ($v as $owner) {
if ($onlyType && $onlyType !== $owner['type']) {
continue;
}
$ok []= $owner;
}
return $ok;
}
Psalm output (using commit 42462b8):
No issues!
https://psalm.dev/r/55b41e85e3
<?php
/**
* @param array<array{type: string}> $v
* @param "prof"|"organization"|null $onlyType
* @return ($onlyType is null ? array<array{type: string}> : array<array{type: $onlyType}>)
*/
function getOwnerObjects(array $v, ?string $onlyType = null): array {
$ok = [];
foreach ($v as $owner) {
if ($onlyType !== null && $onlyType !== $owner['type']) {
continue;
}
$ok []= $owner;
}
return $ok;
}
Psalm output (using commit 42462b8):
No issues!
https://psalm.dev/r/46b3483706, but the non-templated version works fine: https://psalm.dev/r/46b3483706
Those are the same link.
I found these snippets:
https://psalm.dev/r/46b3483706
<?php
/**
* @param array<array{type: string}> $v
* @param "prof"|"organization"|null $onlyType
* @return array<array{type: string}>
*/
function getOwnerObjects(array $v, ?string $onlyType = null): array {
$ok = [];
foreach ($v as $owner) {
if ($onlyType && $onlyType !== $owner['type']) {
continue;
}
$ok []= $owner;
}
return $ok;
}
Psalm output (using commit 42462b8):
No issues!
Whoops, I meant https://psalm.dev/r/82163ab6b7 :)
I found these snippets:
https://psalm.dev/r/82163ab6b7
<?php
/**
* @param array<array{type: string}> $v
* @param "prof"|"organization"|null $onlyType
* @return array<array{type: string}>
*/
function getOwnerObjects(array $v, ?string $onlyType = null): array {
$ok = [];
foreach ($v as $owner) {
if ($onlyType !== null && $onlyType !== $owner['type']) {
continue;
}
$ok []= $owner;
}
return $ok;
}
Psalm output (using commit 42462b8):
No issues!
That one also has no issues :stuck_out_tongue:
That's the point, weird how a !falsy is buggy but a !null assertion isn't
I'm still a bit confused, none of the links you posted have issues. What's different?
Huh, my bad, I'm apparently blind, must've been an issue on my outdated fork which I didn't notice had disappeared in the psalm.dev REPL :)
Is this a false-positive maybe?
I found these snippets:
https://psalm.dev/r/c23cbf01bc
<?php
/**
* @param array<array{type: string}> $v
* @param "prof"|"organization"|null $onlyType
* @return ($onlyType is null ? array<array{type: string}> : array<array{type: $onlyType}>)
*/
function getOwnerObjects(array $v, ?string $onlyType = null): array {
$ok = [];
foreach ($v as $owner) {
if ($onlyType && $onlyType !== $owner['type']) {
continue;
}
$ok []= $owner;
}
return $ok;
}
Psalm output (using commit 42462b8):
ERROR: RedundantCondition - 10:8 - Type TGeneratedFromParam1:fn-getownerobjects as 'organization'|'prof' for $onlyType is never falsy
Whoops, yes it is :)