Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(YodaStyleFixer): do not touch require(_once), include(_once) and yield from statements #7325

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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