Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;

final class SkipCastStringForIntWithMixedArms
{
/**
* @param string $other
*/
public function run(int $code, $other): string
{
switch ($code) {
case '2':
return 'two';
case $other:
return 'other';
default:
return 'default';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;

final class SkipCastStringForIntWithNumericString
{
public function run(int $code): string
{
switch ($code) {
case '01':
return 'one';
case '2':
return 'two';
default:
return 'other';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;

final class SkipUntypedSubjectWithStringCases
{
public function run($value): string
{
switch ($value) {
case 'a':
return 'first';
case '1':
return 'second';
default:
return 'other';
}
}
}
75 changes: 53 additions & 22 deletions rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Stmt\Expression;
Expand Down Expand Up @@ -124,8 +123,8 @@ public function refactor(Node $node): null|Node

$match = $matchResult->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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand Down
Loading