Skip to content

Commit

Permalink
fix(yoda_style): should not touch require(_once), include(_once) and …
Browse files Browse the repository at this point in the history
…yield from statements
  • Loading branch information
SpacePossum committed Sep 26, 2023
1 parent 573ab70 commit 0cbd77e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
8 changes: 7 additions & 1 deletion src/Fixer/ControlStructure/YodaStyleFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,14 @@ private function fixTokensComparePart(Tokens $tokens, int $start, int $end): Tok

private function getCompareFixableInfo(Tokens $tokens, int $index, bool $yoda): ?array
{
$left = $this->getLeftSideCompareFixableInfo($tokens, $index);
$right = $this->getRightSideCompareFixableInfo($tokens, $index);

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

$left = $this->getLeftSideCompareFixableInfo($tokens, $index);

if ($this->isListStatement($tokens, $left['start'], $left['end']) || $this->isListStatement($tokens, $right['start'], $right['end'])) {
return null; // do not fix lists assignment inside statements
}
Expand Down Expand Up @@ -440,6 +441,11 @@ private function isOfLowerPrecedence(Token $token): bool
T_THROW, // throw
T_COALESCE,
T_YIELD, // yield
T_YIELD_FROM,
T_REQUIRE,
T_REQUIRE_ONCE,
T_INCLUDE,
T_INCLUDE_ONCE,
];
}

Expand Down
45 changes: 25 additions & 20 deletions tests/Fixer/ControlStructure/YodaStyleFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,26 +222,6 @@ public static function provideFixCases(): iterable
'<?php while($a === 1 ? 1 : 2){};',
];

yield [
'<?php $a = 1 === include_once $a ? 1 : 2;',
'<?php $a = include_once $a === 1 ? 1 : 2;',
];

yield [
'<?php echo 1 === include $a ? 1 : 2;',
'<?php echo include $a === 1 ? 1 : 2;',
];

yield [
'<?php echo 1 === require_once $a ? 1 : 2;',
'<?php echo require_once $a === 1 ? 1 : 2;',
];

yield [
'<?php echo 1 === require $a ? 1 : 2;',
'<?php echo require $a === 1 ? 1 : 2;',
];

yield [
'<?php switch(1 === $a){
case true: echo 1;
Expand Down Expand Up @@ -867,6 +847,31 @@ function a() {
}
',
];

yield 'require' => [
'<?php require 1 === $var ? "A.php" : "B.php";',
'<?php require $var === 1 ? "A.php" : "B.php";',
];

yield 'require_once' => [
'<?php require_once 1 === $var ? "A.php" : "B.php";',
'<?php require_once $var === 1 ? "A.php" : "B.php";',
];

yield 'include' => [
'<?php include 1 === $var ? "A.php" : "B.php";',
'<?php include $var === 1 ? "A.php" : "B.php";',
];

yield 'include_once' => [
'<?php include_once 1 === $var ? "A.php" : "B.php";',
'<?php include_once $var === 1 ? "A.php" : "B.php";',
];

yield 'yield from' => [
'<?php function test() {return yield from 1 === $a ? $c : $d;};',
'<?php function test() {return yield from $a === 1 ? $c : $d;};',
];
}

/**
Expand Down

0 comments on commit 0cbd77e

Please sign in to comment.