diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default_in_first_case_no_prev.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default_in_first_case_no_prev.php.inc index 8fc4d383da9..e0b2a6bad21 100644 --- a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default_in_first_case_no_prev.php.inc +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default_in_first_case_no_prev.php.inc @@ -4,7 +4,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; class CasesMixUpDefaultInFirstCaseNoPrev { - public function run($value) + public function run(string $value) { switch ($value) { default: @@ -25,7 +25,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; class CasesMixUpDefaultInFirstCaseNoPrev { - public function run($value) + public function run(string $value) { $string = match ($value) { 'other' => 'bar', diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default_in_middle_case.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default_in_middle_case.php.inc index 8bfbd8651cd..3e8e8d22b18 100644 --- a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default_in_middle_case.php.inc +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cases_mix_up_default_in_middle_case.php.inc @@ -4,7 +4,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; class CasesMixUpDefaultInMiddleCase { - public function run($value) + public function run(string $value) { switch ($value) { case 'a': @@ -29,7 +29,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; class CasesMixUpDefaultInMiddleCase { - public function run($value) + public function run(string $value) { $string = match ($value) { 'a' => 'A', diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mirror_assign_comment.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mirror_assign_comment.php.inc index 17246d1d6c1..4b35e50f6f6 100644 --- a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mirror_assign_comment.php.inc +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/mirror_assign_comment.php.inc @@ -4,9 +4,9 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; final class MirrorAssignComment { - public static function run() + public static function run(string $method) { - switch ($_SERVER['REQUEST_METHOD']) { + switch ($method) { case 'GET': // comment $response = 'get'; @@ -26,9 +26,9 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; final class MirrorAssignComment { - public static function run() + public static function run(string $method) { - $response = match ($_SERVER['REQUEST_METHOD']) { + $response = match ($method) { // comment 'GET' => 'get', default => 'error', diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/multi_next_stmt.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/multi_next_stmt.php.inc index df8ed7e779a..d8bf0a59307 100644 --- a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/multi_next_stmt.php.inc +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/multi_next_stmt.php.inc @@ -4,7 +4,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; final class MultiNextStmt { - public function run($value) + public function run(string $value) { switch ($value) { case 'number': @@ -33,7 +33,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; final class MultiNextStmt { - public function run($value) + public function run(string $value) { $sort = match ($value) { 'number', 'percentage', 'integer', 'price', 'decimal' => SORT_NUMERIC, diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/multiple_cases2.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/multiple_cases2.php.inc index 345ed2d13c6..8b72f8cb86e 100644 --- a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/multiple_cases2.php.inc +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/multiple_cases2.php.inc @@ -4,7 +4,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; final class MultipleCases2 { - public function run($value) + public function run(string $value) { switch ($value) { case 'v1': @@ -29,7 +29,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; final class MultipleCases2 { - public function run($value) + public function run(string $value) { return match ($value) { 'v1', 'v2', 'v3' => 100, diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/no_default_direct_next_node_return.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/no_default_direct_next_node_return.php.inc index c77c950f8cb..8c56738e58e 100644 --- a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/no_default_direct_next_node_return.php.inc +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/no_default_direct_next_node_return.php.inc @@ -4,7 +4,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; class NoDefaultDirectNextNodeReturn { - public function run($value) + public function run(string $value) { switch($value) { case 'A': @@ -26,7 +26,7 @@ namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture; class NoDefaultDirectNextNodeReturn { - public function run($value) + public function run(string $value) { return match ($value) { 'A', 'B' => 'AB', diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_cast_string_for_int_with_mixed_arms.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_cast_string_for_int_with_mixed_arms.php.inc new file mode 100644 index 00000000000..b06a7d03042 --- /dev/null +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_cast_string_for_int_with_mixed_arms.php.inc @@ -0,0 +1,21 @@ +getMatch(); - // (int) cast would turn loose "777abc" == 777 (false) into 777 === 777 (true) - if ($this->isStringCondWithIntArm($match)) { + // switch compares loosely, match strictly + if ($this->isLooseComparisonChanged($match)) { continue; } @@ -184,13 +183,31 @@ public function provideMinPhpVersion(): int private function castMatchCond(Match_ $match): void { - $type = $this->nodeTypeResolver->getNativeType($match->cond); - if (! $type->isInteger()->yes()) { + if (! $this->nodeTypeResolver->getNativeType($match->cond)->isInteger()->yes()) { return; } - $armCondType = []; - $newMatchCond = null; + // arms are either all strings or all non-strings, see isLooseComparisonChanged() + foreach ($match->arms as $arm) { + if ($arm->conds === null) { + continue; + } + + if ($this->nodeTypeResolver->getNativeType($arm->conds[0])->isString()->yes()) { + $match->cond = new String_($match->cond); + } + + return; + } + } + + private function isLooseComparisonChanged(Match_ $match): bool + { + $condType = $this->nodeTypeResolver->getNativeType($match->cond); + + $stringArmTypes = []; + $hasNonStringArm = false; + $hasIntArm = false; foreach ($match->arms as $arm) { if ($arm->conds === null) { @@ -200,32 +217,46 @@ private function castMatchCond(Match_ $match): void foreach ($arm->conds as $armCond) { $armCondType = $this->nodeTypeResolver->getNativeType($armCond); if ($armCondType->isString()->yes()) { - $newMatchCond = new String_($match->cond); - } else { - $newMatchCond = null; - break; + $stringArmTypes[] = $armCondType; + continue; + } + + $hasNonStringArm = true; + if ($armCondType->isInteger()->yes()) { + $hasIntArm = true; } } } - if ($newMatchCond instanceof Cast) { - $match->cond = $newMatchCond; + // (int) cast would turn loose "777abc" == 777 (false) into 777 === 777 (true) + if ($condType->isString()->yes()) { + return $hasIntArm; } - } - private function isStringCondWithIntArm(Match_ $match): bool - { - if (! $this->nodeTypeResolver->getNativeType($match->cond)->isString()->yes()) { + if ($stringArmTypes === []) { return false; } - foreach ($match->arms as $arm) { - if ($arm->conds === null) { - continue; + // unknown subject: true == 'a' or 1 == '1' would stop matching + if (! $condType->isInteger()->yes()) { + return true; + } + + // one (string) cast cannot fit both string and non-string arms + if ($hasNonStringArm) { + return true; + } + + // (string) cast mirrors loose == only for non-numeric or canonical int strings, 1 == '01' holds but '1' === '01' does not + foreach ($stringArmTypes as $stringArmType) { + $constantStringTypes = $stringArmType->getConstantStrings(); + if ($constantStringTypes === []) { + return true; } - foreach ($arm->conds as $armCond) { - if ($this->nodeTypeResolver->getNativeType($armCond)->isInteger()->yes()) { + foreach ($constantStringTypes as $constantStringType) { + $value = $constantStringType->getValue(); + if (is_numeric($value) && (string) (int) $value !== $value) { return true; } }