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

Null Coalescing Operator with objects' attributes #3283

Closed
ThibaudDauce opened this issue May 11, 2020 · 7 comments
Closed

Null Coalescing Operator with objects' attributes #3283

ThibaudDauce opened this issue May 11, 2020 · 7 comments
Labels
Milestone

Comments

@ThibaudDauce
Copy link

ThibaudDauce commented May 11, 2020

Bug report

https://phpstan.org/r/d7f7272b-92ac-4497-a6b2-07c98e3df70e

<?php declare(strict_types = 1);

$test = random_int(0, 10);

if ($test > 5) {
	$user = new stdClass;
	$user->name = 'Thibaud';
}

echo $user->name ?? 'Default'; // Variable $user might not be defined.

Expected output

There is no error in this code, PHP isset is working with nested attributes isset($abc->def->ghi) (https://www.php.net/manual/fr/function.isset.php#86313) and $var ?? $default is only a shortcut to isset($var) ? $var : $default.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone May 11, 2020
@EdgarSedov
Copy link

EdgarSedov commented Dec 18, 2020

No info about this yet?(

I'm not familiar with phpstan src at all, but if u'll point me in right direction, i can try to make PR

@ThibaudDauce
Copy link
Author

@ondrejmirtes Is it possible to point us in the right direction, I'm happy to make a PR too to fix this bug.

@ondrejmirtes
Copy link
Member

@ThibaudDauce I guess that $scope->isInExpressionAssign(new Variable('user')) also has to return true on line 10 which isn't the case right now.

It should be fixed somewhere around NodeScopeResolver::ensureShallowNonNullability / ensureNonNullability. And tested as a new testcase called testBug3283 in DefinedVariableRuleTest.

@ondrejmirtes
Copy link
Member

I figured out what I want to do here:

  1. Unify isset() and ?? behaviour, but on the strict side - unknown properties should be reported.
  2. Require adding classes with these properties to universalObjectCrateClasses, or require adding @property PHPDoc with those properties above the class.

Currently to suppress the error you can do 2) and should be fine.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@7ddfa17 (my previous comment is about something else which I want to change but it's unrelated to this bugfix)

@github-actions
Copy link

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 Oct 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants