mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-20 23:41:57 +02:00
[Php] Add NullCoalescingOperatorRector
This commit is contained in:
parent
3e089d8f55
commit
f8a6028cbc
@ -0,0 +1,60 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php\Rector\Assign;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://wiki.php.net/rfc/null_coalesce_equal_operator
|
||||
*/
|
||||
final class NullCoalescingOperatorRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Use null coalescing operator ??=', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
$array = [];
|
||||
$array['user_id'] = $array['user_id'] ?? 'value';
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
$array = [];
|
||||
$array['user_id'] ??= 'value';
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Assign::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Assign $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $node->expr instanceof Coalesce) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->areNodesEqual($node->var, $node->expr->left)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$coalesceNode = new Coalesce($node->var, $node->expr->right);
|
||||
$coalesceNode->setAttribute('null_coalesce', true);
|
||||
|
||||
return $coalesceNode;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
$array = [];
|
||||
$array['user_id'] = $array['user_id'] ?? 'value';
|
||||
$array['user_id'] = $array['user_id_'] ?? 'value';
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
$array = [];
|
||||
$array['user_id'] ??= 'value';
|
||||
$array['user_id'] = $array['user_id_'] ?? 'value';
|
||||
|
||||
?>
|
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php\Tests\Rector\Assign\NullCoalescingOperatorRector;
|
||||
|
||||
use Rector\Php\Rector\Assign\NullCoalescingOperatorRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class NullCoalescingOperatorRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return NullCoalescingOperatorRector::class;
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
@ -214,6 +215,18 @@ final class BetterStandardPrinter extends Standard
|
||||
. ($node->stmts !== null ? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}' : ';');
|
||||
}
|
||||
|
||||
/**
|
||||
* Print ??= since PHP 7.4
|
||||
*/
|
||||
protected function pExpr_BinaryOp_Coalesce(Coalesce $node): string
|
||||
{
|
||||
if (! $node->getAttribute('null_coalesce')) {
|
||||
return parent::pExpr_BinaryOp_Coalesce($node);
|
||||
}
|
||||
|
||||
return $this->pInfixOp(Coalesce::class, $node->left, ' ??= ', $node->right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows PHP 7.3 trailing comma in multiline args
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user