use array_key_exists instead of isset

This commit is contained in:
TomasVotruba 2020-03-28 14:29:19 +01:00
parent ac29fa42ed
commit 74416461bb
5 changed files with 17 additions and 19 deletions

View File

@ -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

View File

@ -835,12 +835,12 @@ Change array_key_exists() ternary to coalesing
<br>
### `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));
}
}
```

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -21,7 +21,7 @@ class SomeClass
{
public function run($packageName, $values)
{
return isset($values[$packageName]);
return array_key_exists($values, $packageName);
}
}