phptools-docs icon indicating copy to clipboard operation
phptools-docs copied to clipboard

Return type `never` is not recognized

Open vitkutny opened this issue 1 year ago • 3 comments

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

vitkutny avatar Aug 16 '24 15:08 vitkutny

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).

jakubmisek avatar Aug 16 '24 17:08 jakubmisek

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();
	}
  }

vitkutny avatar Aug 17 '24 03:08 vitkutny

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.

jakubmisek avatar Aug 17 '24 13:08 jakubmisek