Open bendavies opened 1 week ago
You need to dump the type on the line after the assert.
This works as expected:
function b(?int $i): void
{
\PHPStan\dumpType($i);
Type\nonnull()->assert($i);
\PHPStan\dumpType($i);
}
The fact that the returned value is not correctly inferred might be a bug in PHPStan.
Thanks for looking like that. However, all the other PSL types work like i posted in the OP.
i.e. this does not need the dump to be on the line after the insert This works:
function c(?int $i): void
{
\PHPStan\dumpType($i);
\PHPStan\dumpType(Type\int()->assert($i));
}
Is this maybe what https://github.com/php-standard-library/phpstan-extension/blob/1.0.x/src/Type/AssertTypeSpecifyingExtension.php is handling, and is not working for Type\nonnull()
for some reason?
Describe the bug
Type\nonnull()
is not narrowed by phpstanTo Reproduce
Expected behavior First method shows a way (provided by @ondrejmirtes) that this can work in phpstan. The second method use
Type\nonnull()
.The dumped types here should be
int|null
followed byint
for bothdumpTypes
. but we get:The Psl version of the function "narrows"
int|null
tomixed
, notint