Skip to content

Commit

Permalink
YodaStyleFixer - fix for assignment operators
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed Jul 30, 2021
1 parent 5ad0853 commit ac14f61
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
58 changes: 36 additions & 22 deletions src/Fixer/ControlStructure/YodaStyleFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private function getCompareFixableInfo(Tokens $tokens, $index, $yoda)
$left = $this->getLeftSideCompareFixableInfo($tokens, $index);
$right = $this->getRightSideCompareFixableInfo($tokens, $index);

if (!$yoda && $tokens[$tokens->getNextMeaningfulToken($right['end'])]->equals('=')) {
if (!$yoda && $this->isOfLowerPrecedenceAssignment($tokens[$tokens->getNextMeaningfulToken($right['end'])])) {
return null;
}

Expand Down Expand Up @@ -457,58 +457,72 @@ private function isOfLowerPrecedence(Token $token)

if (null === $tokens) {
$tokens = [
T_AND_EQUAL, // &=
T_BOOLEAN_AND, // &&
T_BOOLEAN_OR, // ||
T_CASE, // case
T_CONCAT_EQUAL, // .=
T_DIV_EQUAL, // /=
T_DOUBLE_ARROW, // =>
T_ECHO, // echo
T_GOTO, // goto
T_LOGICAL_AND, // and
T_LOGICAL_OR, // or
T_LOGICAL_XOR, // xor
T_MINUS_EQUAL, // -=
T_MOD_EQUAL, // %=
T_MUL_EQUAL, // *=
T_OPEN_TAG, // <?php
T_OPEN_TAG_WITH_ECHO,
T_OR_EQUAL, // |=
T_PLUS_EQUAL, // +=
T_POW_EQUAL, // **=
T_PRINT, // print
T_RETURN, // return
T_SL_EQUAL, // <<=
T_SR_EQUAL, // >>=
T_THROW, // throw
T_XOR_EQUAL, // ^=
T_YIELD,
T_YIELD, // yield
];

// @TODO: drop condition when PHP 7.0+ is required
if (\defined('T_COALESCE')) {
$tokens[] = T_COALESCE; // ??
}

// @TODO: drop condition when PHP 7.4+ is required
if (\defined('T_COALESCE_EQUAL')) {
$tokens[] = T_COALESCE_EQUAL; // ??=
}
}

static $otherTokens = [
// bitwise and, or, xor
'&', '|', '^',
// ternary operators
'?', ':',
// assignment
'=',
// end of PHP statement
',', ';',
];

return $token->isGivenKind($tokens) || $token->equalsAny($otherTokens);
return $this->isOfLowerPrecedenceAssignment($token) || $token->isGivenKind($tokens) || $token->equalsAny($otherTokens);
}

/**
* Checks whether the given assignment token has a lower precedence than `T_IS_EQUAL`
* or `T_IS_IDENTICAL`.
*/
private function isOfLowerPrecedenceAssignment(Token $token)
{
static $tokens;

if (null === $tokens) {
$tokens = [
T_AND_EQUAL, // &=
T_CONCAT_EQUAL, // .=
T_DIV_EQUAL, // /=
T_MINUS_EQUAL, // -=
T_MOD_EQUAL, // %=
T_MUL_EQUAL, // *=
T_OR_EQUAL, // |=
T_PLUS_EQUAL, // +=
T_POW_EQUAL, // **=
T_SL_EQUAL, // <<=
T_SR_EQUAL, // >>=
T_XOR_EQUAL, // ^=
];

// @TODO: drop condition when PHP 7.4+ is required
if (\defined('T_COALESCE_EQUAL')) {
$tokens[] = T_COALESCE_EQUAL; // ??=
}
}

return $token->equals('=') || $token->isGivenKind($tokens);
}

/**
Expand Down
24 changes: 18 additions & 6 deletions tests/Fixer/ControlStructure/YodaStyleFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,26 @@ function foo() {
];
}

$templateExpected = '<?php $a %s 4 === $b ? 2 : 3;';
$templateInput = '<?php $a %s $b === 4 ? 2 : 3;';
$operators = ['**=', '*=', '|=', '+=', '-=', '^=', 'xor', 'or', 'and', '<<=', '>>=', '&=', '.=', '/=', '-=', '||', '&&'];
$assignmentOperators = ['=', '**=', '*=', '|=', '+=', '-=', '^=', '<<=', '>>=', '&=', '.=', '/=', '%='];
if (\PHP_VERSION_ID >= 70400) {
$assignmentOperators[] = '??=';
}

foreach ($operators as $operator) {
$logicalOperators = ['xor', 'or', 'and', '||', '&&'];
if (\PHP_VERSION_ID >= 70400) {
$logicalOperators[] = '??';
}

foreach (array_merge($assignmentOperators, $logicalOperators) as $operator) {
yield [
sprintf('<?php $a %s 4 === $b ? 2 : 3;', $operator),
sprintf('<?php $a %s $b === 4 ? 2 : 3;', $operator),
];
}

foreach ($assignmentOperators as $operator) {
yield [
sprintf($templateExpected, $operator),
sprintf($templateInput, $operator),
sprintf('<?php 1 === $x %s 2;', $operator),
];
}
}
Expand Down

0 comments on commit ac14f61

Please sign in to comment.