[CodeQuality] Create InArrayAndArrayKeysToArrayKeyExistsRector

This commit is contained in:
Gabriel Caruso 2018-04-22 13:46:09 -03:00
parent d5a28a9da6
commit d2a06f2e06
No known key found for this signature in database
GPG Key ID: D93D6E258EFC438A
6 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,2 @@
services:
Rector\Rector\Contrib\CodeQuality\InArrayAndArrayKeysToArrayKeyExistsRector: ~

View File

@ -0,0 +1,73 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\CodeQuality;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class InArrayAndArrayKeysToArrayKeyExistsRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Simplify in_array and array_keys functions combination into array_key_exists when array_keys has one argument only',
[
new CodeSample('in_array("key", array_keys($array), true);', 'array_key_exists("key", $array);'),
]
);
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof FuncCall) {
return false;
}
/** @var Name $funcCallName */
$funcCallName = $node->name;
if ($funcCallName->toString() !== 'in_array') {
return false;
}
$secondArgument = $node->args[1]->value;
if (! $secondArgument instanceof FuncCall) {
return false;
}
/** @var Name $functionName */
$functionName = $secondArgument->name;
if ($functionName->toString() !== 'array_keys') {
return false;
}
if (count($secondArgument->args) > 1) {
return false;
}
return true;
}
/**
* @param FuncCall $funcCall
*/
public function refactor(Node $funcCall): ?Node
{
[$key, $array] = $funcCall->args;
$array = $array->value->args[0];
$funcCall->args = [
$key, $array,
];
$funcCall->name = new Name('array_key_exists');
return $funcCall;
}
}

View File

@ -0,0 +1,28 @@
<?php declare(strict_types=1);
final class MyClass
{
public function hasKey(): bool
{
return !array_key_exists('key', [
'key' => ',',
1 => ';',
]);
}
public function hasMoreThanOneArgument(): bool
{
return in_array('key', array_keys([
'key' => ',',
1 => ';',
], 'key'));
}
public function resultIntoAVariable(): void
{
$array = ['foo', 'bar'];
$key = 'key';
$result = array_key_exists($key, $array);
}
}

View File

@ -0,0 +1,30 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\CodeQuality\InArrayAndArrayKeysToArrayKeyExistsRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @covers \Rector\Rector\Contrib\CodeQuality\InArrayAndArrayKeysToArrayKeyExistsRector
*/
final class InArrayAndArrayKeysToArrayKeyExistsRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideWrongToFixedFiles()
*/
public function test(string $wrong, string $fixed): void
{
$this->doTestFileMatchesExpectedContent($wrong, $fixed);
}
public function provideWrongToFixedFiles(): Iterator
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
}
protected function provideConfig(): string
{
return __DIR__ . '/config.yml';
}
}

View File

@ -0,0 +1,28 @@
<?php declare(strict_types=1);
final class MyClass
{
public function hasKey(): bool
{
return !in_array('key', array_keys([
'key' => ',',
1 => ';',
]));
}
public function hasMoreThanOneArgument(): bool
{
return in_array('key', array_keys([
'key' => ',',
1 => ';',
], 'key'));
}
public function resultIntoAVariable(): void
{
$array = ['foo', 'bar'];
$key = 'key';
$result = in_array($key, array_keys($array), true);
}
}

View File

@ -0,0 +1,2 @@
services:
Rector\Rector\Contrib\CodeQuality\InArrayAndArrayKeysToArrayKeyExistsRector: ~