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

treatPhpDocTypesAsCertain has false positives with if (!empty()) #9126

Closed
klausi opened this issue Apr 1, 2023 · 3 comments
Closed

treatPhpDocTypesAsCertain has false positives with if (!empty()) #9126

klausi opened this issue Apr 1, 2023 · 3 comments

Comments

@klausi
Copy link

klausi commented Apr 1, 2023

Bug report

When setting treatPhpDocTypesAsCertain: false I would expect that PHPStan does not report errors when checking falsy values in if conditions.

The User and Resume class is third party code and not under my control to quickly fix(/update.

Code snippet that reproduces the problem

https://phpstan.org/r/c9e0fdba-86d0-4691-8415-09e32a11b31b

<?php declare(strict_types = 1);

class User {}

class Resume {

	/**
	 * @return User
	 */
	public function getOwner() {
		return $this->owner;
	}
}

$resume = new Resume();
$owner = $resume->getOwner();
if (!empty($owner)) {
	echo "not empty";
}

Expected output

Only one error that the property is undefined, but the empty() check is correct and PHPStan should not complain about it.

The error Variable $owner in empty() always exists and is not falsy. should not be reported.

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

Not super happy today, as this bug could have resulted in a fatal error on production, because I removed the empty() check. Luckily got caught by automated tests.

Otherwise PHPStan is great, sending love!

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@5b7a115

@klausi
Copy link
Author

klausi commented Apr 1, 2023

Speedy Ondrej is speedy, thanks a lot!

@github-actions
Copy link

github-actions bot commented May 3, 2023

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 May 3, 2023
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