Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False generic error: Method never returns null so .. #6158

Closed
Jeroeny opened this issue Dec 6, 2021 · 3 comments
Closed

False generic error: Method never returns null so .. #6158

Jeroeny opened this issue Dec 6, 2021 · 3 comments
Labels
Milestone

Comments

@Jeroeny
Copy link
Contributor

Jeroeny commented Dec 6, 2021

Bug report

Given the method

    /** @return T|null */
    final public function randValue(): mixed

In a generic Collection. Then using that method somewhere:

	private function rand(): ?User
	{
		return $this->collection->randValue();
	}

yields the error: Method User::rand() never returns null so it can be removed from the return type..

Code snippet that reproduces the problem

https://phpstan.org/r/a70699a3-6415-4bc6-8247-7b55a57040dc

Expected output

No errors.

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

PHPStan still helps a lot :)

@Jeroeny
Copy link
Contributor Author

Jeroeny commented Dec 6, 2021

Apparently PHPStan thinks mixed return type never returns null: https://phpstan.org/r/a4940517-dc71-4766-89c8-0bb774b95b63

But it can: https://3v4l.org/ANhir

@ondrejmirtes ondrejmirtes added this to the Generics milestone Dec 6, 2021
@phpstan-bot
Copy link
Contributor

@Jeroeny After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-73: Method User::rand() never returns null so it can be removed from the return type.
+No errors

@github-actions
Copy link

github-actions bot commented Feb 4, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants