mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
Merge pull request #691 from rectorphp/cq-array-values
[CodeQuality] Add SimplifyInArrayValuesRector
This commit is contained in:
commit
a24351e14f
5
.gitignore
vendored
5
.gitignore
vendored
@ -8,4 +8,7 @@ composer.lock
|
|||||||
|
|
||||||
/demo
|
/demo
|
||||||
rector-symfony.yml
|
rector-symfony.yml
|
||||||
.phpunit.result.cache
|
.phpunit.result.cache
|
||||||
|
|
||||||
|
# often customized locally - example on Github is just fine
|
||||||
|
create-rector.yml
|
||||||
|
@ -39,6 +39,7 @@
|
|||||||
"Rector\\": "src",
|
"Rector\\": "src",
|
||||||
"Rector\\ContributorTools\\": "packages/ContributorTools/src",
|
"Rector\\ContributorTools\\": "packages/ContributorTools/src",
|
||||||
"Rector\\ConsoleDiffer\\": "packages/ConsoleDiffer/src",
|
"Rector\\ConsoleDiffer\\": "packages/ConsoleDiffer/src",
|
||||||
|
"Rector\\CodeQuality\\": "packages/CodeQuality/src",
|
||||||
"Rector\\NodeTypeResolver\\": "packages/NodeTypeResolver/src",
|
"Rector\\NodeTypeResolver\\": "packages/NodeTypeResolver/src",
|
||||||
"Rector\\Symfony\\": "packages/Symfony/src",
|
"Rector\\Symfony\\": "packages/Symfony/src",
|
||||||
"Rector\\CakePHP\\": "packages/CakePHP/src",
|
"Rector\\CakePHP\\": "packages/CakePHP/src",
|
||||||
@ -60,6 +61,7 @@
|
|||||||
"Rector\\Tests\\": "tests",
|
"Rector\\Tests\\": "tests",
|
||||||
"Rector\\NodeTypeResolver\\Tests\\": "packages/NodeTypeResolver/tests",
|
"Rector\\NodeTypeResolver\\Tests\\": "packages/NodeTypeResolver/tests",
|
||||||
"Rector\\CakePHP\\Tests\\": "packages/CakePHP/tests",
|
"Rector\\CakePHP\\Tests\\": "packages/CakePHP/tests",
|
||||||
|
"Rector\\CodeQuality\\Tests\\": "packages/CodeQuality/tests",
|
||||||
"Rector\\Php\\Tests\\": "packages/Php/tests",
|
"Rector\\Php\\Tests\\": "packages/Php/tests",
|
||||||
"Rector\\Symfony\\Tests\\": "packages/Symfony/tests",
|
"Rector\\Symfony\\Tests\\": "packages/Symfony/tests",
|
||||||
"Rector\\Silverstripe\\Tests\\": "packages/Silverstripe/tests",
|
"Rector\\Silverstripe\\Tests\\": "packages/Silverstripe/tests",
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package: 'CodeQuality'
|
package: 'CodeQuality'
|
||||||
name: 'SimplifyConfigRector' # w/wo suffix
|
name: 'SimplifyInArrayValuesRector' # w/wo suffix
|
||||||
node_types:
|
node_types:
|
||||||
- 'FuncCall'
|
- 'FuncCall'
|
||||||
|
|
||||||
description: 'It adds man to hi'
|
description: 'Removes unneeded array_values() in in_array() call'
|
||||||
code_before: 'hi'
|
code_before: "in_array('key', array_values($array), true);"
|
||||||
code_after: 'hi man'
|
code_after: "in_array('key', $array, true);"
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
in_array('key', $array, true);
|
@ -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';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
in_array('key', array_values($array), true);
|
@ -0,0 +1,2 @@
|
|||||||
|
services:
|
||||||
|
Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector: ~
|
@ -1,6 +1,6 @@
|
|||||||
<?php declare(strict_types=1);
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rector\_Package_\Tests\_Category_;
|
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_;
|
||||||
|
|
||||||
use Iterator;
|
use Iterator;
|
||||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
services:
|
services:
|
||||||
Rector\_Package_\_Category_\_Name_: ~
|
Rector\_Package_\Rector\_Category_\_Name_: ~
|
||||||
|
Loading…
x
Reference in New Issue
Block a user