Skip to content

Commit

Permalink
[DowngradePhp80] Add Static Call Support on DowngradeMatchToSwitchRec…
Browse files Browse the repository at this point in the history
…tor (#2175)

* [DowngradePhp80] Add Static Call Support on DowngradeMatchToSwitchRector

* implemented

* [ci-review] Rector Rectify

* move to last for instanceof Expr

* back to CallLike

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 26, 2022
1 parent 6680bd8 commit c5c620a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

class SomeStaticCall
{
public static function output($value)
{
echo $value;
}

public function run($statusCode)
{
self::output(match ($statusCode) {
200, 300 => null,
400 => 'not found',
default => 'unknown status code',
});
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

class SomeStaticCall
{
public static function output($value)
{
echo $value;
}

public function run($statusCode)
{
switch ($statusCode) {
case 200:
case 300:
self::output(null);
break;
case 400:
self::output('not found');
break;
default:
self::output('unknown status code');
break;
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Stmt;
Expand Down Expand Up @@ -156,17 +159,18 @@ private function createSwitchStmts(Echo_ | Expression | Return_ $node, MatchArm
} elseif ($node instanceof Echo_) {
$stmts[] = new Echo_([$matchArm->body]);
$stmts[] = new Break_();
} else if ($node->expr instanceof MethodCall || $node->expr instanceof FuncCall) {
$call = clone $node->expr;
$call->args = [new Arg($matchArm->body)];
$stmts[] = new Expression($call);
$stmts[] = new Break_();
} elseif ($node->expr instanceof Assign) {
$stmts[] = new Expression(new Assign($node->expr->var, $matchArm->body));
$stmts[] = new Break_();
} elseif ($node->expr instanceof Match_) {
$stmts[] = new Expression($matchArm->body);
$stmts[] = new Break_();
} elseif ($node->expr instanceof CallLike) {
/** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $call */
$call = clone $node->expr;
$call->args = [new Arg($matchArm->body)];
$stmts[] = new Expression($call);
$stmts[] = new Break_();
}

return $stmts;
Expand Down

0 comments on commit c5c620a

Please sign in to comment.