Skip to content
Merged
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
1 change: 1 addition & 0 deletions config/set/code-quality/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Rector\ValueObject\PhpVersionFeature;

/**
* @see https://3v4l.org/bfsdY
*
* @see \Rector\CodeQuality\Tests\Rector\Foreach_\SimplifyForeachToCoalescingRector\SimplifyForeachToCoalescingRectorTest
*/
final class SimplifyForeachToCoalescingRector extends AbstractRector
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

?>
Original file line number Diff line number Diff line change
@@ -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;
}
}