Skip to content

False positive: Always throwing and returning never #15070

Description

@uuf6429

Bug report

Given the following...

<?php

final class Url
{
	public function __clone(): never // <<
	{
		throw new \BadMethodCallException('Always throw');
	}
}

final class Email
{
	/**
	 * @return never
	 */
	public function __clone(): never
	{
		throw new \BadMethodCallException('Always throw');
	}
}

...PHPStan warns (on line 5) that a method that throws unconditionally should have the never return type (which it already does).

Originally, I had code returning void, but I never saw that warning in our code, it only showed up in the playground when I happened to paste my code for trying something else. Interestingly, the error identifier is phpstanPlayground.never. Also interesting that the PHPDoc one works.

Code snippet that reproduces the problem

https://phpstan.org/r/4670249c-9af0-4d1a-bcc2-c583c449dd46

Expected output

I didn't expect the error.

Did PHPStan help you today? Did it make you happy in any way?

Always :)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions