Merge pull request #691 from rectorphp/cq-array-values

[CodeQuality] Add SimplifyInArrayValuesRector
This commit is contained in:
Tomáš Votruba 2018-10-15 08:35:53 +08:00 committed by GitHub
commit a24351e14f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 113 additions and 7 deletions

5
.gitignore vendored
View File

@ -8,4 +8,7 @@ composer.lock
/demo
rector-symfony.yml
.phpunit.result.cache
.phpunit.result.cache
# often customized locally - example on Github is just fine
create-rector.yml

View File

@ -39,6 +39,7 @@
"Rector\\": "src",
"Rector\\ContributorTools\\": "packages/ContributorTools/src",
"Rector\\ConsoleDiffer\\": "packages/ConsoleDiffer/src",
"Rector\\CodeQuality\\": "packages/CodeQuality/src",
"Rector\\NodeTypeResolver\\": "packages/NodeTypeResolver/src",
"Rector\\Symfony\\": "packages/Symfony/src",
"Rector\\CakePHP\\": "packages/CakePHP/src",
@ -60,6 +61,7 @@
"Rector\\Tests\\": "tests",
"Rector\\NodeTypeResolver\\Tests\\": "packages/NodeTypeResolver/tests",
"Rector\\CakePHP\\Tests\\": "packages/CakePHP/tests",
"Rector\\CodeQuality\\Tests\\": "packages/CodeQuality/tests",
"Rector\\Php\\Tests\\": "packages/Php/tests",
"Rector\\Symfony\\Tests\\": "packages/Symfony/tests",
"Rector\\Silverstripe\\Tests\\": "packages/Silverstripe/tests",

View File

@ -1,8 +1,8 @@
package: 'CodeQuality'
name: 'SimplifyConfigRector' # w/wo suffix
name: 'SimplifyInArrayValuesRector' # w/wo suffix
node_types:
- 'FuncCall'
description: 'It adds man to hi'
code_before: 'hi'
code_after: 'hi man'
description: 'Removes unneeded array_values() in in_array() call'
code_before: "in_array('key', array_values($array), true);"
code_after: "in_array('key', $array, true);"

View File

@ -0,0 +1,63 @@
<?php declare(strict_types=1);
namespace Rector\CodeQuality\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use Rector\NodeAnalyzer\CallAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class SimplifyInArrayValuesRector extends AbstractRector
{
/**
* @var CallAnalyzer
*/
private $callAnalyzer;
public function __construct(CallAnalyzer $callAnalyzer)
{
$this->callAnalyzer = $callAnalyzer;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Removes unneeded array_values() in in_array() call', [
new CodeSample('in_array("key", array_values($array), true);', 'in_array("key", $array, true);'),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
// @todo shorten to "isName()" trait
if (! $this->callAnalyzer->isName($node, 'in_array')) {
return $node;
}
if (! $node->args[1]->value instanceof FuncCall) {
return $node;
}
/** @var FuncCall $innerFunCall */
$innerFunCall = $node->args[1]->value;
if (! $this->callAnalyzer->isName($innerFunCall, 'array_values')) {
return $node;
}
$node->args[1] = $innerFunCall->args[0];
return $node;
}
}

View File

@ -0,0 +1,3 @@
<?php
in_array('key', $array, true);

View File

@ -0,0 +1,30 @@
<?php declare(strict_types=1);
namespace Rector\CodeQuality\Tests\Rector\FuncCall\SimplifyInArrayValuesRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @covers \Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector
*/
final class SimplifyInArrayValuesRectorTest 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,3 @@
<?php
in_array('key', array_values($array), true);

View File

@ -0,0 +1,2 @@
services:
Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector: ~

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\_Package_\Tests\_Category_;
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

View File

@ -1,2 +1,2 @@
services:
Rector\_Package_\_Category_\_Name_: ~
Rector\_Package_\Rector\_Category_\_Name_: ~