mirror of https://github.com/rectorphp/rector.git
[DowngradePhp80] Allow Truthy with Ternary on DowngradeThrowExprRector (#539)
* [DowngradePhp80] Allow Truthy with Ternary on DowngradeThrowExprRector * Trigger notification * update test fixture to trigger test * update test fixture to trigger test
This commit is contained in:
parent
6e73a1f069
commit
4be1f2aee7
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\Fixture;
|
||||
|
||||
trait TraitA
|
||||
trait SomeTraitUsed
|
||||
{
|
||||
public function foo()
|
||||
{
|
||||
|
@ -10,9 +10,9 @@ trait TraitA
|
|||
}
|
||||
}
|
||||
|
||||
class SomeClass
|
||||
class SkipUsingTrait
|
||||
{
|
||||
use TraitA;
|
||||
use SomeTraitUsed;
|
||||
|
||||
public function __construct(
|
||||
private $usedDependency
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class TruthyTernary
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$var = 'test';
|
||||
$id = $var ?: throw new RuntimeException();
|
||||
}
|
||||
|
||||
public function run2()
|
||||
{
|
||||
$var = 'test';
|
||||
$id = $var ? 'value' : throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector\Fixture;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class TruthyTernary
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$var = 'test';
|
||||
if (!$var) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
$id = $var;
|
||||
}
|
||||
|
||||
public function run2()
|
||||
{
|
||||
$var = 'test';
|
||||
if (!$var) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
$id = 'value';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -5,14 +5,17 @@ declare(strict_types=1);
|
|||
namespace Rector\DowngradePhp80\Rector\Expression;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
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\Ternary;
|
||||
use PhpParser\Node\Expr\Throw_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use Rector\Core\NodeAnalyzer\CoalesceAnalyzer;
|
||||
use Rector\Core\NodeManipulator\BinaryOpManipulator;
|
||||
use Rector\Core\NodeManipulator\IfManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -27,7 +30,8 @@ final class DowngradeThrowExprRector extends AbstractRector
|
|||
{
|
||||
public function __construct(
|
||||
private IfManipulator $ifManipulator,
|
||||
private CoalesceAnalyzer $coalesceAnalyzer
|
||||
private CoalesceAnalyzer $coalesceAnalyzer,
|
||||
private BinaryOpManipulator $binaryOpManipulator
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -100,9 +104,33 @@ CODE_SAMPLE
|
|||
return new Expression(($assign->expr));
|
||||
}
|
||||
|
||||
if ($assign->expr instanceof Ternary) {
|
||||
return $this->processTernary($assign, $assign->expr);
|
||||
}
|
||||
|
||||
return $expression;
|
||||
}
|
||||
|
||||
private function processTernary(Assign $assign, Ternary $ternary): ?If_
|
||||
{
|
||||
if (! $ternary->else instanceof Throw_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$inversedTernaryCond = $this->binaryOpManipulator->inverseNode($ternary->cond);
|
||||
if (! $inversedTernaryCond instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$if = $this->ifManipulator->createIfExpr($inversedTernaryCond, new Expression($ternary->else));
|
||||
$assign->expr = $ternary->if === null
|
||||
? $ternary->cond
|
||||
: $ternary->if;
|
||||
|
||||
$this->addNodeAfterNode(new Expression($assign), $if);
|
||||
return $if;
|
||||
}
|
||||
|
||||
private function processCoalesce(Assign $assign, Coalesce $coalesce): ?If_
|
||||
{
|
||||
if (! $coalesce->right instanceof Throw_) {
|
||||
|
|
Loading…
Reference in New Issue