[DowngradePhp80] Add DowngradeThrowExprRector (#533)

* [DowngradePhp80] Add DowngradeThrowExprRector

* add test

* move to use Expression node to check Throw inside Expression

* skip direct throw

* skip no throw in assign

* handle coalesce

* implemented for starter

* ensure left coalese is Variable, PropertyFetch, or StaticPropertyFetch, no other expr

* eol
This commit is contained in:
Abdul Malik Ikhsan 2021-07-28 23:42:54 +07:00 committed by GitHub
parent 32ef12c263
commit a467f764ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 271 additions and 0 deletions

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DowngradeThrowExprRectorTest extends AbstractRectorTestCase
{
/**
* @requires PHP 8.0
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
use RuntimeException;
class AssignDirectThrow
{
public function run()
{
$id = throw new RuntimeException();
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
use RuntimeException;
class AssignDirectThrow
{
public function run()
{
throw new RuntimeException();
}
}
?>

View File

@ -0,0 +1,34 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
use RuntimeException;
class Fixture
{
public function run()
{
$id = $somethingNonexistent ?? throw new RuntimeException();
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
use RuntimeException;
class Fixture
{
public function run()
{
if (!isset($somethingNonexistent)) {
throw new RuntimeException();
}
$id = $somethingNonexistent;
}
}
?>

View File

@ -0,0 +1,15 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
use RuntimeException;
class SkipDirectThrow
{
public function run()
{
throw new RuntimeException();
}
}
?>

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
class SkipNoThrowInAssign
{
public function run($b)
{
$a = $b;
}
}
?>

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
use Rector\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeThrowExprRector::class);
};

View File

@ -0,0 +1,133 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\Rector\Expression;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://wiki.php.net/rfc/throw_expression
*
* @see \Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\DowngradeThrowExprRectorTest
*/
final class DowngradeThrowExprRector extends AbstractRector
{
public function __construct(
private IfManipulator $ifManipulator
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Downgrade throw as expr', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$id = $somethingNonexistent ?? throw new RuntimeException();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
if (!isset($somethingNonexistent)) {
throw new RuntimeException();
}
$id = $somethingNonexistent;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}
/**
* @param Expression $node
*/
public function refactor(Node $node): ?Node
{
if ($node->expr instanceof Throw_) {
return null;
}
if ($node->expr instanceof Assign) {
return $this->processAssign($node, $node->expr);
}
return $node;
}
private function processAssign(Expression $expression, Assign $assign): If_ | Expression | null
{
if (! $this->hasThrowInAssignExpr($assign)) {
return null;
}
if ($assign->expr instanceof Coalesce) {
return $this->processCoalesce($assign, $assign->expr);
}
if ($assign->expr instanceof Throw_) {
return new Expression(($assign->expr));
}
return $expression;
}
private function processCoalesce(Assign $assign, Coalesce $coalesce): ?If_
{
if (! $coalesce->right instanceof Throw_) {
return null;
}
if (! $coalesce->left instanceof Variable && ! $coalesce->left instanceof PropertyFetch && ! $coalesce->left instanceof StaticPropertyFetch) {
return null;
}
$booleanNot = new BooleanNot(new Isset_([$coalesce->left]));
$assign->expr = $coalesce->left;
$if = $this->ifManipulator->createIfExpr($booleanNot, new Expression($coalesce->right));
$this->addNodeAfterNode(new Expression($assign), $if);
return $if;
}
private function hasThrowInAssignExpr(Assign $assign): bool
{
return (bool) $this->betterNodeFinder->findFirst(
$assign->expr,
fn (Node $node): bool => $node instanceof Throw_
);
}
}