diff --git a/config/set/code-quality/code-quality.yaml b/config/set/code-quality/code-quality.yaml index a54aff2c3f1..732250cb5b2 100644 --- a/config/set/code-quality/code-quality.yaml +++ b/config/set/code-quality/code-quality.yaml @@ -53,4 +53,4 @@ services: Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector: null Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector: null Rector\CodeQuality\Rector\BinaryOp\InlineIfToExplicitIfRector: null - Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToIssetRector: null + Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector: null diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index cc31340119a..aed64cddf36 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -835,12 +835,12 @@ Change array_key_exists() ternary to coalesing
-### `ArrayKeysAndInArrayToIssetRector` +### `ArrayKeysAndInArrayToArrayKeyExistsRector` -- class: [`Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToIssetRector`](/../master/rules/code-quality/src/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector.php) +- class: [`Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector`](/../master/rules/code-quality/src/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php) - [test fixtures](/../master/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/Fixture) -Replace array_keys() and in_array() to isset +Replace array_keys() and in_array() to array_key_exists() ```diff class SomeClass @@ -849,7 +849,7 @@ Replace array_keys() and in_array() to isset { - $keys = array_keys($values); - return in_array($packageName, $keys, true); -+ return isset($values[$packageName])); ++ return array_keys_exists($values, $packageName)); } } ``` diff --git a/rules/code-quality/src/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector.php b/rules/code-quality/src/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php similarity index 79% rename from rules/code-quality/src/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector.php rename to rules/code-quality/src/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php index 07d71d51ce5..696b30b235d 100644 --- a/rules/code-quality/src/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector.php +++ b/rules/code-quality/src/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php @@ -5,8 +5,6 @@ declare(strict_types=1); namespace Rector\CodeQuality\Rector\FuncCall; use PhpParser\Node; -use PhpParser\Node\Arg; -use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\FunctionLike; @@ -16,13 +14,13 @@ use Rector\Core\RectorDefinition\CodeSample; use Rector\Core\RectorDefinition\RectorDefinition; /** - * @see \Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToIssetRector\ArrayKeysAndInArrayToIssetRectorTest + * @see \Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToIssetRector\ArrayKeysAndInArrayToArrayKeyExistsRectorTest */ -final class ArrayKeysAndInArrayToIssetRector extends AbstractRector +final class ArrayKeysAndInArrayToArrayKeyExistsRector extends AbstractRector { public function getDefinition(): RectorDefinition { - return new RectorDefinition('Replace array_keys() and in_array() to isset', [ + return new RectorDefinition('Replace array_keys() and in_array() to array_key_exists()', [ new CodeSample( <<<'PHP' class SomeClass @@ -40,7 +38,7 @@ class SomeClass { public function run($packageName, $values) { - return isset($values[$packageName])); + return array_keys_exists($values, $packageName)); } } PHP @@ -97,13 +95,13 @@ PHP $this->removeNode($previousAssignArraysKeysFuncCall); - return $this->createIsset($node, $arrayKeysFuncCall); + return $this->createArrayKeyExists($node, $arrayKeysFuncCall); } - private function createIsset(FuncCall $inArrayFuncCall, FuncCall $arrayKeysFuncCall): FuncCall + private function createArrayKeyExists(FuncCall $inArrayFuncCall, FuncCall $arrayKeysFuncCall): FuncCall { - $dimFetch = new ArrayDimFetch($arrayKeysFuncCall->args[0]->value, $inArrayFuncCall->args[0]->value); + $arguments = [$arrayKeysFuncCall->args[0], $inArrayFuncCall->args[0]]; - return new FuncCall(new Name('isset'), [new Arg($dimFetch)]); + return new FuncCall(new Name('array_key_exists'), $arguments); } } diff --git a/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/ArrayKeysAndInArrayToIssetRectorTest.php b/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/ArrayKeysAndInArrayToArrayKeyExistsRectorTest.php similarity index 68% rename from rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/ArrayKeysAndInArrayToIssetRectorTest.php rename to rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/ArrayKeysAndInArrayToArrayKeyExistsRectorTest.php index 8caca3b8fac..8301ca51ce2 100644 --- a/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/ArrayKeysAndInArrayToIssetRectorTest.php +++ b/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/ArrayKeysAndInArrayToArrayKeyExistsRectorTest.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace Rector\CodeQuality\Tests\Rector\FuncCall\ArrayKeysAndInArrayToIssetRector; use Iterator; -use Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToIssetRector; +use Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector; use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; -final class ArrayKeysAndInArrayToIssetRectorTest extends AbstractRectorTestCase +final class ArrayKeysAndInArrayToArrayKeyExistsRectorTest extends AbstractRectorTestCase { /** * @dataProvider provideData() @@ -25,6 +25,6 @@ final class ArrayKeysAndInArrayToIssetRectorTest extends AbstractRectorTestCase protected function getRectorClass(): string { - return ArrayKeysAndInArrayToIssetRector::class; + return ArrayKeysAndInArrayToArrayKeyExistsRector::class; } } diff --git a/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/Fixture/fixture.php.inc b/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/Fixture/fixture.php.inc index bf8f7f74b7c..e59980681c2 100644 --- a/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/Fixture/fixture.php.inc +++ b/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToIssetRector/Fixture/fixture.php.inc @@ -21,7 +21,7 @@ class SomeClass { public function run($packageName, $values) { - return isset($values[$packageName]); + return array_key_exists($values, $packageName); } }