Skip to content

Commit 90fbaf5

Browse files
authored
[DowngradePhp80] Handle match as arg on DowngradeMatchToSwitchRector (#209)
1 parent 917085c commit 90fbaf5

File tree

6 files changed

+86
-49
lines changed

6 files changed

+86
-49
lines changed

rules-tests/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector/Fixture/call_anonymous_function.php.inc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@ $output = function($value) {
2222
echo $value;
2323
};
2424

25-
switch ($statusCode) {
26-
case 100:
27-
case 200:
28-
$output(null);
29-
break;
30-
case 300:
31-
$output('not found');
32-
break;
33-
default:
34-
$output('unknown status code');
35-
break;
36-
}
25+
$output((function () use ($statusCode) {
26+
switch ($statusCode) {
27+
case 100:
28+
case 200:
29+
return null;
30+
case 300:
31+
return 'not found';
32+
default:
33+
return 'unknown status code';
34+
}
35+
})());
3736

3837
?>

rules-tests/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector/Fixture/function_call.php.inc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,17 @@ class FunctionCall
3434
{
3535
public function run($statusCode)
3636
{
37-
switch ($statusCode) {
38-
case 200:
39-
case 300:
40-
output(null);
41-
break;
42-
case 400:
43-
output('not found');
44-
break;
45-
default:
46-
output('unknown status code');
47-
break;
48-
}
37+
output((function () use ($statusCode) {
38+
switch ($statusCode) {
39+
case 200:
40+
case 300:
41+
return null;
42+
case 400:
43+
return 'not found';
44+
default:
45+
return 'unknown status code';
46+
}
47+
})());
4948
}
5049
}
5150

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;
4+
5+
final class MatchAsArg
6+
{
7+
public function run($param)
8+
{
9+
return execute(
10+
match ($param) {
11+
true => 1,
12+
false => new \stdClass(),
13+
},
14+
);
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;
23+
24+
final class MatchAsArg
25+
{
26+
public function run($param)
27+
{
28+
return execute(
29+
(function () use ($param) {
30+
switch ($param) {
31+
case true:
32+
return 1;
33+
case false:
34+
return new \stdClass();
35+
}
36+
})(),
37+
);
38+
}
39+
}
40+
41+
?>

rules-tests/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector/Fixture/method_call.php.inc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,17 @@ class ClassFunctionCall
3434

3535
public function run($statusCode)
3636
{
37-
switch ($statusCode) {
38-
case 200:
39-
case 300:
40-
$this->output(null);
41-
break;
42-
case 400:
43-
$this->output('not found');
44-
break;
45-
default:
46-
$this->output('unknown status code');
47-
break;
48-
}
37+
$this->output((function () use ($statusCode) {
38+
switch ($statusCode) {
39+
case 200:
40+
case 300:
41+
return null;
42+
case 400:
43+
return 'not found';
44+
default:
45+
return 'unknown status code';
46+
}
47+
})());
4948
}
5049
}
5150

rules-tests/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector/Fixture/static_call.php.inc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,17 @@ class SomeStaticCall
3434

3535
public function run($statusCode)
3636
{
37-
switch ($statusCode) {
38-
case 200:
39-
case 300:
40-
self::output(null);
41-
break;
42-
case 400:
43-
self::output('not found');
44-
break;
45-
default:
46-
self::output('unknown status code');
47-
break;
48-
}
37+
self::output((function () use ($statusCode) {
38+
switch ($statusCode) {
39+
case 200:
40+
case 300:
41+
return null;
42+
case 400:
43+
return 'not found';
44+
default:
45+
return 'unknown status code';
46+
}
47+
})());
4948
}
5049
}
5150

rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
108108
$this->traverseNodesWithCallable(
109109
$node,
110110
function (Node $subNode) use ($node, &$match, &$hasChanged, $scope) {
111-
if ($subNode instanceof ArrayItem && $subNode->value instanceof Match_ && $this->isEqualScope(
111+
if (($subNode instanceof ArrayItem || $subNode instanceof Arg) && $subNode->value instanceof Match_ && $this->isEqualScope(
112112
$subNode->value,
113113
$scope
114114
)) {

0 commit comments

Comments
 (0)