psalm: Template psalm-throws
I could not find any similar issue. I have a usecase
<?php
/**
* @template T
*
* @psalm-param string|null $nullable
* @psalm-param T $exception
*
* @psalm-throws T
*/
function expect(?string $nullable, Exception $exception)
{
if ($nullable === null) {
throw $exception;
}
return $nullable;
}
expect('test', new RuntimeException('Test is null'));
My proposal:
@psalm-throws
should take generic parameter same as @psalm-param
or @psalm-var
.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 18 (1 by maintainers)
I have use case for this (I initially reported #6098 which was closed as duplicate of this).
Sometimes you want to pass as input the exception you’re going to throw, and in that case it would be nice to now that you’re throwing the same time of exception you’re passing in.
I would like to have this code to actually work
That would mean type parameter inference would affect surrounding code (the catch statement).
@orklah so is that snipped now pointing to an potential feature request or is it a bug? Because I still think that using
* @psalm-throws IdReferencedEntityNotFoundException<EntityIdClass>
should be possible or?(Using in terms of causing a check.)
use
@phpstan-throws
instead of@throws