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

Getting an error if null condition is not explicit. #2302

Closed
davidkmenta opened this issue Jul 16, 2019 · 1 comment
Closed

Getting an error if null condition is not explicit. #2302

davidkmenta opened this issue Jul 16, 2019 · 1 comment

Comments

@davidkmenta
Copy link

Bug report

Getting an error if null condition is not explicit.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

class Bar {
	public function foo(): ?string
	{
		return null;
	}
}

class HelloWorld
{
	public function sayHello(Bar $object): string
	{
		if ($object->foo()) {
			return $object->foo();
		}
		
		return '';
	}
}

Code above generates an error: https://phpstan.org/r/bc5bd55d-64db-4f6f-a30a-e4fc367e81c7

This works: https://phpstan.org/r/296a2b2d-d3ce-42b7-b479-3293ba43c36a
The difference is in condition $object->foo() !== null.

Expected output

No errors expected. Or is it a feature? 😄

@ondrejmirtes
Copy link
Member

Yes, currently a feature. I'll do something about it. Duplicate of #2288 and others.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants