Bug report
Code snippet that reproduces the problem
<?php declare(strict_types = 1);
use function PHPStan\Testing\assertType;
/**
* @template T
* @param array<T,mixed> $array
* @return (T is int ? ($array is non-empty-array ? non-empty-list<numeric-string> : list<numeric-string>) : ($array is non-empty-array ? non-empty-list<numeric-string> : list<string>))
*/
function keysAsString(array $array): array
{
$keys = [];
foreach ($array as $k => $_) {
$keys[] = (string)$k;
}
return $keys;
}
assertType('array<int, numeric-string>', keysAsString([]));
assertType('non-empty-array<int, numeric-string>', keysAsString(['' => '']));
And another case:
/**
* @template N of Num
* @template _FIZZ of n3|n6|n9|n12
* @template _BUZZ of n5|n10
* @template _FIZZBUZZ of n15
* @param N $n
* @return ($n is _FIZZBUZZ ? FizzBuzz : ($n is _BUZZ ? Buzz : ($n is _FIZZ ? Fizz : N)))
*/
function fizzbuzz(Num $n)
{
return match($n % 3) {
0 => match($n % 5) {
0 => new FizzBuzz,
default => new Fizz,
},
default => match($n % 5) {
0 => new Buzz,
default => $n,
},
};
}
@template is useful not only as an argument but also as a variable for the return values.
Expected output
Expect no message output in either case.
Did PHPStan help you today? Did it make you happy in any way?
In the days when the conditional type was only Psalm, I was afraid that it would bring complexity. For the past few weeks, when I actually use it, I realize that it realizes a rich expression of types in a function like the use case presented this time. It's just the right ability to the extent that it's not Turing complete.
Bug report
Code snippet that reproduces the problem
And another case:
@templateis useful not only as an argument but also as a variable for the return values.Expected output
Expect no message output in either case.
Did PHPStan help you today? Did it make you happy in any way?
In the days when the conditional type was only Psalm, I was afraid that it would bring complexity. For the past few weeks, when I actually use it, I realize that it realizes a rich expression of types in a function like the use case presented this time. It's just the right ability to the extent that it's not Turing complete.