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

IsNullFixer - fix parenthesis not closed #4229

Merged
merged 1 commit into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/Fixer/LanguageConstruct/IsNullFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,13 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
$wrapIntoParentheses = $parentLeftToken->isGivenKind($parentOperations) || $parentRightToken->isGivenKind($parentOperations);

if (!$isContainingDangerousConstructs) {
if (!$wrapIntoParentheses) {
// closing parenthesis removed with leading spaces
$prevIndex = $tokens->getPrevMeaningfulToken($referenceEnd);
if ($tokens[$prevIndex]->equals(',')) {
$tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
}
$tokens->removeLeadingWhitespace($referenceEnd);
$tokens->clearAt($referenceEnd);
// closing parenthesis removed with leading spaces
$prevIndex = $tokens->getPrevMeaningfulToken($referenceEnd);
if ($tokens[$prevIndex]->equals(',')) {
$tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
}
$tokens->removeLeadingWhitespace($referenceEnd);
$tokens->clearAt($referenceEnd);

// opening parenthesis removed with trailing spaces
$tokens->removeLeadingWhitespace($matches[1]);
Expand All @@ -164,23 +162,20 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
if (true === $this->configuration['use_yoda_style']) {
if ($wrapIntoParentheses) {
array_unshift($replacement, new Token('('));
$tokens->insertAt($referenceEnd + 1, new Token(')'));
}

$tokens->overrideRange($isNullIndex, $isNullIndex, $replacement);
} else {
$replacement = array_reverse($replacement);
if ($isContainingDangerousConstructs) {
array_unshift($replacement, new Token(')'));
}

if ($wrapIntoParentheses) {
$replacement[] = new Token(')');
$tokens[$isNullIndex] = new Token('(');
} else {
$tokens->clearAt($isNullIndex);
}

$tokens->overrideRange($referenceEnd, $referenceEnd, $replacement);
$tokens->insertAt($referenceEnd + 1, $replacement);
}

// nested is_null calls support
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixer/LanguageConstruct/IsNullFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ public function provideFixCases()
'<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
'<?php if ((is_null($u) or $v) and ($w || is_null($x)) xor (!is_null($y) and $z)) echo "foo"; ?>',
],

// edge cases: contains dangerous constructs, wrapped into parentheses
[
'<?php null === ($a ? $x : $y);',
'<?php is_null($a ? $x : $y);',
],
[
'<?php $a === (null === $x);',
'<?php $a === is_null($x);',
],
[
'<?php $a === (null === ($a ? $x : $y));',
'<?php $a === is_null($a ? $x : $y);',
],
];
}

Expand Down