-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
bug: Exclude $this
from TernaryToNullCoalescingFixer
#7052
bug: Exclude $this
from TernaryToNullCoalescingFixer
#7052
Conversation
$this
from TernaryToNullCoalescingFixer
@kylekatarnls please make CI green, no one is going to do code review without it 🙂. You should run |
I like Star Wars, but Yoda-style advantage is not worth the readability compromise for me 🤣 |
nice work @kylekatarnls 👍 suggestion: // search what is inside the isset()
$issetTokens = $this->getMeaningfulSequence($tokens, $startBraceIndex, $endBraceIndex);
if (\count($issetTokens) === 1) {
if ($issetTokens[0]->isGivenKind(T_VARIABLE) && '$this' === strtolower($issetTokens[0]->getContent())) {
return; // do not fix `isset($this) ? x : y` as it will break code
}
} elseif ($this->hasChangingContent($issetTokens)) {
return; // some weird stuff inside the isset
}
// search what is inside the middle argument of ternary operator and adding the test will maybe explain why my suggestion: ['<?php $x = isset($THIS) ? $this : null;'], |
OK for the case sensitivity. Done + test case added. I note that the case-sensitivity is not taken into account neither for issetCode !== ternaryFirstOperand check, is it intentional that:
I see you also propose to check token value rather than generated code, is it for performance reason? Because we run |
b99b41c
to
f278df5
Compare
f278df5
to
8dfdfd9
Compare
Thank you!
It is case sensitive now and should be I think. Example <?php
$var = 2;
$VAR = 1;
$a = isset($var) ? $VAR : 1;
var_dump($a);
$a = $var ?? 1;
var_dump($a);
so changing the code would yield different results at runtime so must be avoided.
Partly, but also it is more easy to understand/debug as we try to work with tokens where ever possible. However this is just a preference here of mine, so others may think different.
I see your point, I reason however that because it is so rare we should use as little cpu cycles as possible to detect it. I think this PR looks good 👍 Can a member kick of the CI for this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this requires integration test, so fixer can be run on actual code, not on tokens only. Also, we probably should cover more complex scenarios like isset($this->foo)
, isset($this['foo'])
, because in those cases $this
should be kept in isset()
too.
I don't think this is an issue. We do an exact check of what is inside the That's also why IMHO an integration test is overkill for this. But I ticked |
I still have ~100 issues to verify (along with other 100+ already verified, but not resolved), and ~60 PRs to handle, I would really love if you could cover mentioned scenarios on your own 😅. |
Same, spare time already full of OSS work. But first let's clarify as I think I should not waste more energy until the exact issue is clearly understood, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think it's more like a bug that $this
works differently in isset()
than regular variables, but considering current behavior this PR looks OK.
@kubawerlos will you take a look?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS. I've deleted @kubawerlos duplicated comment 🙂.
Thank you @kylekatarnls 🍻 |
Fix #7051