phptools-docs
phptools-docs copied to clipboard
Return type `never` is not recognized
public function actionDefault(): never # 'actionDefault': not all code paths return a value
{
$this->error();
}
Rule not all code paths return a value should not report functions with never return type
Thank you for reporting the issue.
It seems the editor needs to know, that $this->error(); does not return as well (returns never, or contains an infinite loop, or throws an exception).
You are right, it seems it does not recognize @return never from phpdoc – works with native type. Phpdoc should be prioritized when available.
Here is minimal example to reproduce:
class A {
/**
*
* @throws \LogicException
* @return never
*/
function error(): void {
throw new \LogicException('never');
}
}
class B extends A {
function actionDefault(): never {
$this->error();
}
}
Thanks; we handle this case rather vaguely.
Since function error() is overrideable (not final), we just can't be sure. On the other hand the return type hint : never can't be changed in a subclass so we count on it.
Many frameworks specify @return never in a base class but then they override it.