Skip to content

Commit

Permalink
bug #4229 IsNullFixer - fix parenthesis not closed (guilliamxavier)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.12 branch.

Discussion
----------

IsNullFixer - fix parenthesis not closed

Includes and fixes #4225

Commits
-------

83944b8 IsNullFixer - fix parenthesis not closed
  • Loading branch information
keradus committed Jan 4, 2019
2 parents 8dee338 + 83944b8 commit 1afc836
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
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

0 comments on commit 1afc836

Please sign in to comment.