From 73e1f64054588ef7c982f6ea3c65c9d169acba5b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 24 Nov 2026 17:52:41 +0100 Subject: [PATCH] [Php80] Skip int cast of string subject in ChangeSwitchToMatchRector --- .../Fixture/cast_int_for_string.php.inc | 38 ------------------ .../Fixture/skip_cast_int_for_string.php.inc | 22 +++++++++++ .../Switch_/ChangeSwitchToMatchRector.php | 39 ++++++++++++++----- 3 files changed, 51 insertions(+), 48 deletions(-) delete mode 100644 rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cast_int_for_string.php.inc create mode 100644 rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_cast_int_for_string.php.inc diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cast_int_for_string.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cast_int_for_string.php.inc deleted file mode 100644 index 98ce58fca0e..00000000000 --- a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/cast_int_for_string.php.inc +++ /dev/null @@ -1,38 +0,0 @@ - ------ - 1, - 88 => 2, - default => 4, - };; - } -} - -?> diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_cast_int_for_string.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_cast_int_for_string.php.inc new file mode 100644 index 00000000000..0b4bf4fff74 --- /dev/null +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_cast_int_for_string.php.inc @@ -0,0 +1,22 @@ +getMatch(); + + // (int) cast would turn loose "777abc" == 777 (false) into 777 === 777 (true) + if ($this->isStringCondWithIntArm($match)) { + continue; + } + if ($matchResult->shouldRemoveNextStmt() && $isReturn) { unset($node->stmts[$key + 1]); } @@ -180,12 +185,7 @@ public function provideMinPhpVersion(): int private function castMatchCond(Match_ $match): void { $type = $this->nodeTypeResolver->getNativeType($match->cond); - $isNativeCondString = $type->isString() - ->yes(); - $isNativeCondInt = $type->isInteger() - ->yes(); - - if (! $isNativeCondString && ! $isNativeCondInt) { + if (! $type->isInteger()->yes()) { return; } @@ -199,9 +199,7 @@ private function castMatchCond(Match_ $match): void foreach ($arm->conds as $armCond) { $armCondType = $this->nodeTypeResolver->getNativeType($armCond); - if ($armCondType->isInteger()->yes() && $isNativeCondString) { - $newMatchCond = new Int_($match->cond); - } elseif ($armCondType->isString()->yes() && $isNativeCondInt) { + if ($armCondType->isString()->yes()) { $newMatchCond = new String_($match->cond); } else { $newMatchCond = null; @@ -215,6 +213,27 @@ private function castMatchCond(Match_ $match): void } } + private function isStringCondWithIntArm(Match_ $match): bool + { + if (! $this->nodeTypeResolver->getNativeType($match->cond)->isString()->yes()) { + return false; + } + + foreach ($match->arms as $arm) { + if ($arm->conds === null) { + continue; + } + + foreach ($arm->conds as $armCond) { + if ($this->nodeTypeResolver->getNativeType($armCond)->isInteger()->yes()) { + return true; + } + } + } + + return false; + } + private function mirrorDynamicBoolExpr(Match_ $match): void { // switch(true) already just use