Repository navigation
[Php80] Skip loose comparison change in ChangeSwitchToMatchRector - #9065
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
switchcompares with loose==,matchwith strict===. Two places where the conversion changed which arm matches:1.
(string)cast decided by the last arm onlyRector produced
match ($code) { '2' => 'two', ... }- no cast, because thebreakincastMatchCond()only left the inner loop and the next arm overwrote the decision.2 === '2'is false, so'two'is never returned. With the arms in the other order the cast is added, and then non-string arms break instead.Numeric strings that are not a canonical int had the same problem:
case '01'became(string) $code === '01', while1 == '01'is true.2. Untyped subject with string cases
became
match ($value) { 'a' => ..., '1' => ... }. With unknown$value,true == 'a'and1 == '1'match the switch, but not the match.Fix
isLooseComparisonChanged()replacesisStringCondWithIntArm()and skips the conversion when:castMatchCond()now only adds the(string)cast when every arm is a string.Fixtures
6 existing fixtures converted an untyped subject with string cases. They test other things (default position, comments, next stmt), so I typed their subject as
stringto keep that coverage, and addedskip_untyped_subject_with_string_cases.php.incfor the untyped case. Plusskip_cast_string_for_int_with_mixed_arms.php.incandskip_cast_string_for_int_with_numeric_string.php.inc. All 3 new fixtures fail on main.