Skip to content
Open
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,33 @@
<?php

namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixture;

function ternaryWithIsNull()
{
$x = is_null($value) ? 10 : $value;

$y = is_null($a) ? $b : $a;

$z = !is_null($value) ? $value : 10;

$w = !is_null($c) ? $c : $d;
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixture;

function ternaryWithIsNull()
{
$x = $value ?? 10;

$y = $a ?? $b;

$z = $value ?? 10;

$w = $c ?? $d;
}

?>
50 changes: 50 additions & 0 deletions rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Ternary;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -37,6 +39,7 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample('$value === null ? 10 : $value;', '$value ?? 10;'),
new CodeSample('isset($value) ? $value : 10;', '$value ?? 10;'),
new CodeSample('is_null($value) ? 10 : $value;', '$value ?? 10;'),
]
);
}
Expand All @@ -58,6 +61,17 @@ public function refactor(Node $node): ?Node
return $this->processTernaryWithIsset($node, $node->cond);
}

if ($node->cond instanceof FuncCall && $this->isName($node->cond, 'is_null')) {
return $this->processTernaryWithIsNull($node, $node->cond, false);
}

if (
$node->cond instanceof BooleanNot && $node->cond->expr instanceof FuncCall
&& $this->isName($node->cond->expr, 'is_null')
) {
return $this->processTernaryWithIsNull($node, $node->cond->expr, true);
}

if ($node->cond instanceof Identical) {
$checkedNode = $node->else;
$fallbackNode = $node->if;
Expand Down Expand Up @@ -95,6 +109,42 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::NULL_COALESCE;
}

private function processTernaryWithIsNull(Ternary $ternary, FuncCall $isNullFuncCall, bool $isNegated): ?Coalesce
{
if (count($isNullFuncCall->args) !== 1) {
return null;
}

$firstArg = $isNullFuncCall->args[0];
if (! $firstArg instanceof Node\Arg) {
return null;
}

$checkedExpr = $firstArg->value;

if ($isNegated) {
if (! $ternary->if instanceof Expr) {
return null;
}

if (! $this->nodeComparator->areNodesEqual($ternary->if, $checkedExpr)) {
return null;
}

return new Coalesce($ternary->if, $ternary->else);
}

if (! $this->nodeComparator->areNodesEqual($ternary->else, $checkedExpr)) {
return null;
}

if (! $ternary->if instanceof Expr) {
return null;
}

return new Coalesce($ternary->else, $ternary->if);
}

private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset): ?Coalesce
{
if (! $ternary->if instanceof Expr) {
Expand Down