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

[Php80] Cast string/int for int vs string condition and case condition on ChangeSwitchToMatchRector #6359

Merged
merged 2 commits into from
Oct 6, 2024
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
@@ -0,0 +1,38 @@
<?php

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

final class CastIntForString
{
function test(string $code): int
{
switch($code) {
case 777:
return 1;
case 88:
return 2;
default:
return 4;
};
}
}

?>
-----
<?php

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

final class CastIntForString
{
function test(string $code): int
{
return match ((int) $code) {
777 => 1,
88 => 2,
default => 4,
};;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

final class CastStringForInt
{
function test(int $code): string
{
switch($code) {
case '777':
return '1';
case '88':
return '2';
default:
return '4';
};
}
}

?>
-----
<?php

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

final class CastStringForInt
{
function test(int $code): string
{
return match ((string) $code) {
'777' => '1',
'88' => '2',
default => '4',
};;
}
}

?>
42 changes: 42 additions & 0 deletions rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
Expand Down Expand Up @@ -117,6 +121,8 @@ public function refactor(Node $node): ?Node
$assignVar = $this->resolveAssignVar($condAndExprs);
$hasDefaultValue = $this->matchSwitchAnalyzer->hasDefaultValue($match);

$this->castMatchCond($match);

if ($assignVar instanceof Expr) {
if (! $hasDefaultValue) {
continue;
Expand Down Expand Up @@ -147,6 +153,42 @@ public function refactor(Node $node): ?Node
return null;
}

private function castMatchCond(Match_ $match): void
{
$type = $this->nodeTypeResolver->getNativeType($match->cond);
$isNativeCondString = $type->isString()->yes();
$isNativeCondInt = $type->isInteger()->yes();

if (! $isNativeCondString && ! $isNativeCondInt) {
return;
}

$armCondType = [];
$newMatchCond = null;

foreach ($match->arms as $arm) {
if ($arm->conds === null) {
continue;
}

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) {
$newMatchCond = new String_($match->cond);
} else {
$newMatchCond = null;
break;
}
}
}

if ($newMatchCond instanceof Cast) {
$match->cond = $newMatchCond;
}
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::MATCH_EXPRESSION;
Expand Down