From 5bc58a84a1f686aeec95197a1d9f5fde1f0e4c10 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 15 Oct 2018 08:21:55 +0800 Subject: [PATCH 1/4] gitignore: add create-rector.yml --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 16b58529d12..3fb8043a64f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,7 @@ composer.lock /demo rector-symfony.yml -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache + +# often customized locally - example on Github is just fine +create-rector.yml From 0bda599a8b0d5984722b697d9dafb5d993a6daa6 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 15 Oct 2018 08:23:23 +0800 Subject: [PATCH 2/4] update example to real one --- create-rector.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/create-rector.yml b/create-rector.yml index 18d9817f122..c905d1421b6 100644 --- a/create-rector.yml +++ b/create-rector.yml @@ -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);" From deff6bb56ed65d48e868fa579d676da0316110f2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 15 Oct 2018 08:26:09 +0800 Subject: [PATCH 3/4] [ContributorTools] Fix Rector path in test config --- .../_Package_/tests/Rector/_Category_/_Name_/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ContributorTools/templates/packages/_Package_/tests/Rector/_Category_/_Name_/config.yml b/packages/ContributorTools/templates/packages/_Package_/tests/Rector/_Category_/_Name_/config.yml index 47d907f7b8a..ec42c3540d1 100644 --- a/packages/ContributorTools/templates/packages/_Package_/tests/Rector/_Category_/_Name_/config.yml +++ b/packages/ContributorTools/templates/packages/_Package_/tests/Rector/_Category_/_Name_/config.yml @@ -1,2 +1,2 @@ services: - Rector\_Package_\_Category_\_Name_: ~ + Rector\_Package_\Rector\_Category_\_Name_: ~ From b4b728dc2890f4b607564501b8b1a2a0827a2fd9 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 15 Oct 2018 08:30:41 +0800 Subject: [PATCH 4/4] [CodeQuality] Add SimplifyInArrayValuesRector --- composer.json | 2 + .../FuncCall/SimplifyInArrayValuesRector.php | 63 +++++++++++++++++++ .../Correct/correct.php.inc | 3 + .../SimplifyInArrayValuesRectorTest.php | 30 +++++++++ .../Wrong/wrong.php.inc | 3 + .../SimplifyInArrayValuesRector/config.yml | 2 + .../Rector/_Category_/_Name_/_Name_Test.php | 2 +- 7 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 packages/CodeQuality/src/Rector/FuncCall/SimplifyInArrayValuesRector.php create mode 100644 packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/Correct/correct.php.inc create mode 100644 packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/SimplifyInArrayValuesRectorTest.php create mode 100644 packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/Wrong/wrong.php.inc create mode 100644 packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/config.yml diff --git a/composer.json b/composer.json index 04cc8b1b427..c44dedee56b 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/packages/CodeQuality/src/Rector/FuncCall/SimplifyInArrayValuesRector.php b/packages/CodeQuality/src/Rector/FuncCall/SimplifyInArrayValuesRector.php new file mode 100644 index 00000000000..6e2ee6b2faf --- /dev/null +++ b/packages/CodeQuality/src/Rector/FuncCall/SimplifyInArrayValuesRector.php @@ -0,0 +1,63 @@ +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; + } +} diff --git a/packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/Correct/correct.php.inc b/packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/Correct/correct.php.inc new file mode 100644 index 00000000000..30f396f510f --- /dev/null +++ b/packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/Correct/correct.php.inc @@ -0,0 +1,3 @@ +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'; + } +} diff --git a/packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/Wrong/wrong.php.inc b/packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/Wrong/wrong.php.inc new file mode 100644 index 00000000000..d4d093471f4 --- /dev/null +++ b/packages/CodeQuality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/Wrong/wrong.php.inc @@ -0,0 +1,3 @@ +