Skip to content

Commit

Permalink
minor #4230 [7.3] IsNullFixer - fix trailing comma (guilliamxavier)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.12 branch.

Discussion
----------

[7.3] IsNullFixer - fix trailing comma

Includes and fixes #4226

I also had to include #4229 first

Commits
-------

31e0d12 [7.3] IsNullFixer - fix trailing comma
  • Loading branch information
keradus committed Jan 4, 2019
2 parents cb243a9 + 31e0d12 commit 75797f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/Fixer/LanguageConstruct/IsNullFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,14 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
$parentOperations = [T_IS_EQUAL, T_IS_NOT_EQUAL, T_IS_IDENTICAL, T_IS_NOT_IDENTICAL];
$wrapIntoParentheses = $parentLeftToken->isGivenKind($parentOperations) || $parentRightToken->isGivenKind($parentOperations);

// possible trailing comma removed
$prevIndex = $tokens->getPrevMeaningfulToken($referenceEnd);
if ($tokens[$prevIndex]->equals(',')) {
$tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
}

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

Expand Down
16 changes: 15 additions & 1 deletion tests/Fixer/LanguageConstruct/IsNullFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function provideFixCases()
'<?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
// edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
[
'<?php null === ($a ? $x : $y);',
'<?php is_null($a ? $x : $y);',
Expand Down Expand Up @@ -311,6 +311,20 @@ public function provideFix73Cases()
'<?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: $isContainingDangerousConstructs, $wrapIntoParentheses
[
'<?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, );',
],
];
}
}

0 comments on commit 75797f6

Please sign in to comment.