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

Problem assignin variable inside method is_null #2640

Closed
sdecandelario opened this issue Dec 3, 2019 · 7 comments
Closed

Problem assignin variable inside method is_null #2640

sdecandelario opened this issue Dec 3, 2019 · 7 comments
Labels
Milestone

Comments

@sdecandelario
Copy link

sdecandelario commented Dec 3, 2019

Bug report

When assign a variable inside a is_null function and then use this variable phpstan seems not to detect and considere that the variable can be null

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

class HelloWorld
{
	public function sayHello(): ?string
	{
		return 'Hello';
	}
	
	public function test(): string{
		if(!is_null($hello = $this->sayHello())){
			return $hello;	
		}
		
		return 'No hello';
	}
}

https://phpstan.org/r/33610217-6278-4a1c-b2cc-dd9f9f837fcd

Expected output

No errors

@mergeable
Copy link

mergeable bot commented Dec 3, 2019

This bug report is missing a link to reproduction on phpstan.org.
It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

Yes, this is a bug, but you can easily go around it :) https://phpstan.org/r/917106e6-839c-43a0-aeff-a0c9153af49f

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Dec 3, 2019
@b1rdex
Copy link
Contributor

b1rdex commented Dec 4, 2019

Alternatively, you could use null !== instead of !is_null: https://phpstan.org/r/a60c6dfc-901e-404d-a2f9-8c9dd5dcc30f. It fixes logic somehow.

@guilliamxavier
Copy link

Related to (maybe duplicate of) #2413

@phpstan-bot
Copy link
Contributor

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

@@ @@
-12: Method HelloWorld::test() should return string but returns string|null.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@b2bf703

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue May 11, 2021
@github-actions
Copy link

github-actions bot commented Jun 9, 2021

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 Jun 9, 2021
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

5 participants