phpstan: False positive on return null|void

Bug report

	/**
	 * Overview action
	 *
	 * @return \Cake\Http\Response|null|void
	 */
	public function index() { ... }

Framework controller actions usually don’t have to return, the |void addition should suffice to silence PHPStan.

But it complains with

Method DatabaseLog\Controller\Admin\LogsController::index() should
return Cake\Http\Response|void|null but return statement is missing.

Expected output

No error here. Only with \Cake\Http\Response|null I would expect the above message.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 25 (14 by maintainers)

Most upvoted comments

I understand, I’ll think about it.

On Fri, 6 Dec 2019 at 18:08, Mark Sch. notifications@github.com wrote:

Note: If a project may decide to not allow this soft case (decide for hard typehints), they can simple add a cs sniffer or other tooling to do so, but for all others this is still not sth that should be disallowed by phpstan false-positive reporting these things.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/phpstan/phpstan/issues/2643?email_source=notifications&email_token=AAAZTODA6MF3AA2HOEFOT63QXKBILA5CNFSM4JVBE4CKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGEXQRI#issuecomment-562657349, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZTOENPGX7LWBK5HY5BD3QXKBILANCNFSM4JVBE4CA .

Ondřej Mirtes

Released as 0.12.5 😃

I was surprised that the other side was already fixed in PHPStan: https://github.com/phpstan/phpstan-src/commit/80afe105a5bc00b2f7ea06a876226fe6b9d59a2d

I now fixed the remaining case, e.g. return in a function isn’t required in case of void union: https://github.com/phpstan/phpstan-src/commit/e208c0599555cd6e54b9c38ff0412e89c2c2ac36

We might rethink this when PHP 8 is out. PHPStan typically wants the same constraints from phpDoc types as from the native types (so that they can be automatically converted by other tools).