Skip to content

Commit

Permalink
TernaryToNullCoalescingFixer - concat precedence fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum authored and keradus committed Nov 20, 2020
1 parent 3a9af85 commit 7615750
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ parameters:
-
message: '/^Parameter #1 \$finder of method PhpCsFixer\\Config::setFinder\(\) expects iterable<string>, int given\.$/'
path: tests/ConfigTest.php

-
message: '/^Property .*::\$indicator .* does not accept null\.$/'
path: tests/Indicator/PhpUnitTestCaseIndicatorTest.php
tipsOfTheDay: false
23 changes: 17 additions & 6 deletions src/Fixer/Alias/PowToExponentiationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class PowToExponentiationFixer extends AbstractFunctionReferenceFixer
*/
public function isCandidate(Tokens $tokens)
{
// minimal candidate to fix is seven tokens: pow(x,x);
// minimal candidate to fix is seven tokens: pow(x,y);
return $tokens->count() > 7 && $tokens->isTokenKindFound(T_STRING);
}

Expand Down Expand Up @@ -68,9 +68,9 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
$candidates = $this->findPowCalls($tokens);
$argumentsAnalyzer = new ArgumentsAnalyzer();

$numberOfTokensAdded = 0;
$previousCloseParenthesisIndex = \count($tokens);

foreach (array_reverse($candidates) as $candidate) {
// if in the previous iteration(s) tokens were added to the collection and this is done within the tokens
// indexes of the current candidate than the index of the close ')' of the candidate has moved and so
Expand All @@ -84,10 +84,17 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
}

$arguments = $argumentsAnalyzer->getArguments($tokens, $candidate[1], $candidate[2]);

if (2 !== \count($arguments)) {
continue;
}

for ($i = $candidate[1]; $i < $candidate[2]; ++$i) {
if ($tokens[$i]->isGivenKind(T_ELLIPSIS)) {
continue 2;
}
}

$numberOfTokensAdded += $this->fixPowToExponentiation(
$tokens,
$candidate[0], // functionNameIndex,
Expand All @@ -105,12 +112,13 @@ private function findPowCalls(Tokens $tokens)
{
$candidates = [];

// Minimal candidate to fix is seven tokens: pow(x,x);
// Minimal candidate to fix is seven tokens: pow(x,y);
$end = \count($tokens) - 6;

// First possible location is after the open token: 1
for ($i = 1; $i < $end; ++$i) {
$candidate = $this->find('pow', $tokens, $i, $end);

if (null === $candidate) {
break;
}
Expand Down Expand Up @@ -139,11 +147,13 @@ private function fixPowToExponentiation(Tokens $tokens, $functionNameIndex, $ope
// clean up the function call tokens prt. I
$tokens->clearAt($closeParenthesisIndex);
$previousIndex = $tokens->getPrevMeaningfulToken($closeParenthesisIndex);

if ($tokens[$previousIndex]->equals(',')) {
$tokens->clearAt($previousIndex); // trailing ',' in function call (PHP 7.3)
}

$added = 0;

// check if the arguments need to be wrapped in parenthesis
foreach (array_reverse($arguments, true) as $argumentStartIndex => $argumentEndIndex) {
if ($this->isParenthesisNeeded($tokens, $argumentStartIndex, $argumentEndIndex)) {
Expand All @@ -157,9 +167,10 @@ private function fixPowToExponentiation(Tokens $tokens, $functionNameIndex, $ope
$tokens->clearAt($openParenthesisIndex);
$tokens->clearAt($functionNameIndex);

$prev = $tokens->getPrevMeaningfulToken($functionNameIndex);
if ($tokens[$prev]->isGivenKind(T_NS_SEPARATOR)) {
$tokens->clearAt($prev);
$prevMeaningfulTokenIndex = $tokens->getPrevMeaningfulToken($functionNameIndex);

if ($tokens[$prevMeaningfulTokenIndex]->isGivenKind(T_NS_SEPARATOR)) {
$tokens->clearAt($prevMeaningfulTokenIndex);
}

return $added;
Expand Down
1 change: 1 addition & 0 deletions src/Fixer/Operator/TernaryToNullCoalescingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private function isHigherPrecedenceAssociativityOperator(Token $token)
'^',
'|',
'~',
'.',
];

return isset($operatorsPerId[$token->getId()]) || $token->equalsAny($operatorsPerContent);
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixer/Alias/PowToExponentiationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public function &pow($a, $b);
'<?php echo $a[1]** $b[2+5];',
'<?php echo pow($a[1], $b[2+5]);',
],
[
'<?php pow($b, ...$a);',
],
];

foreach ($tests as $index => $test) {
Expand Down
1 change: 1 addition & 0 deletions tests/Fixer/Operator/TernaryToNullCoalescingFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function provideFixCases()
['<?php $x = ! isset($a) ? $a : null;'],
['<?php $x = false === isset($a) ? $a : 2;'],
['<?php $x = 4 * isset($a) ? $a : 2;'],
['<?php $x = "4" . isset($a) ? $a : 2;'],
['<?php $x = 3 ** isset($a) ? $a : 2;'],
['<?php $x = 1 | isset($a) ? $a : 2;'],
['<?php $x = (array) isset($a) ? $a : 2;'],
Expand Down
9 changes: 9 additions & 0 deletions tests/Indicator/PhpUnitTestCaseIndicatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ final class PhpUnitTestCaseIndicatorTest extends TestCase
protected function setUp()
{
$this->indicator = new PhpUnitTestCaseIndicator();

parent::setUp();
}

protected function tearDown()
{
$this->indicator = null;

parent::tearDown();
}

/**
Expand Down

0 comments on commit 7615750

Please sign in to comment.