mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-20 08:05:29 +01:00
[CodeQuality] Add ArrayKeyExistsTernaryThenValueToCoalescingRector
This commit is contained in:
parent
d4ba12f437
commit
48030e7257
@ -46,3 +46,4 @@ services:
|
||||
Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector: ~
|
||||
Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector: ~
|
||||
Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector: ~
|
||||
Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector: ~
|
||||
|
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\Ternary;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://3v4l.org/f7itn
|
||||
*
|
||||
* @see \Rector\CodeQuality\Tests\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector\ArrayKeyExistsTernaryThenValueToCoalescingRectorTest
|
||||
*/
|
||||
final class ArrayKeyExistsTernaryThenValueToCoalescingRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change array_key_exists() ternary to coalesing', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run($values, $keyToMatch)
|
||||
{
|
||||
$result = array_key_exists($keyToMatch, $values) ? $values[$keyToMatch] : null;
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run($values, $keyToMatch)
|
||||
{
|
||||
$result = $values[$keyToMatch] ?? null;
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Ternary::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Ternary $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $node->cond instanceof FuncCall) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isName($node->cond, 'array_key_exists')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $node->if instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->areArrayKeysExistsArgsMatchingDimFetch($node->cond, $node->if)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Coalesce($node->if, $node->else);
|
||||
}
|
||||
|
||||
/**
|
||||
* Equals if:
|
||||
*
|
||||
* array_key_exists($key, $values);
|
||||
* =
|
||||
* $values[$key]
|
||||
*/
|
||||
private function areArrayKeysExistsArgsMatchingDimFetch(FuncCall $funcCall, ArrayDimFetch $arrayDimFetch): bool
|
||||
{
|
||||
$keyExpr = $funcCall->args[0]->value;
|
||||
$valuesExpr = $funcCall->args[1]->value;
|
||||
|
||||
if (! $this->areNodesEqual($arrayDimFetch->var, $valuesExpr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->areNodesEqual($arrayDimFetch->dim, $keyExpr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class ArrayKeyExistsTernaryThenValueToCoalescingRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideDataForTest()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideDataForTest(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return ArrayKeyExistsTernaryThenValueToCoalescingRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run($values, $keyToMatch)
|
||||
{
|
||||
$result = array_key_exists($keyToMatch, $values) ? $values[$keyToMatch] : null;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run($values, $keyToMatch)
|
||||
{
|
||||
$result = $values[$keyToMatch] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector\Fixture;
|
||||
|
||||
class SkipMissMatch
|
||||
{
|
||||
public function run($values, $keyToMatch)
|
||||
{
|
||||
$result = array_key_exists($keyToMatch, $values) ? $values[$keyToMatch2] : null;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user