From 9a772fcb9d6812a7b5cf409d402911eb992e833c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Oct 2023 13:37:59 +0700 Subject: [PATCH 1/3] [CodeQuality] Add negated support on SingleInArrayToCompareRector --- .../FuncCall/SingleInArrayToCompareRector.php | 61 +++++++++++++++---- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php b/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php index 229cf7f579f..5e4142c627f 100644 --- a/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php @@ -10,6 +10,9 @@ use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; +use PhpParser\Node\Expr\BinaryOp\NotEqual; +use PhpParser\Node\Expr\BinaryOp\NotIdentical; +use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\FuncCall; use Rector\Core\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -59,36 +62,59 @@ public function run() */ public function getNodeTypes(): array { - return [FuncCall::class]; + return [BooleanNot::class, FuncCall::class]; } /** - * @param FuncCall $node + * @param BooleanNot|FuncCall $node */ public function refactor(Node $node): ?Node { - if (! $this->isName($node, 'in_array')) { + if ($node instanceof BooleanNot) { + if (! $node->expr instanceof FuncCall) { + return null; + } + + $firstArrayItem = $this->resolveArrayItem($node->expr); + if (! $firstArrayItem instanceof ArrayItem) { + return null; + } + + return $this->processCompare($firstArrayItem, $node->expr, true); + } + + $firstArrayItem = $this->resolveArrayItem($node); + if (! $firstArrayItem instanceof ArrayItem) { + return null; + } + + return $this->processCompare($firstArrayItem, $node); + } + + private function resolveArrayItem(FuncCall $funcCall): ?ArrayItem + { + if (! $this->isName($funcCall, 'in_array')) { return null; } - if ($node->isFirstClassCallable()) { + if ($funcCall->isFirstClassCallable()) { return null; } - if (! isset($node->args[1])) { + if (! isset($funcCall->args[1])) { return null; } - if (! $node->args[1] instanceof Arg) { + if (! $funcCall->args[1] instanceof Arg) { return null; } - if (! $node->args[1]->value instanceof Array_) { + if (! $funcCall->args[1]->value instanceof Array_) { return null; } /** @var Array_ $arrayNode */ - $arrayNode = $node->args[1]->value; + $arrayNode = $funcCall->args[1]->value; if (count($arrayNode->items) !== 1) { return null; } @@ -102,19 +128,28 @@ public function refactor(Node $node): ?Node return null; } - if (! isset($node->getArgs()[0])) { + if (! isset($funcCall->getArgs()[0])) { return null; } + return $firstArrayItem; + } + + private function processCompare(ArrayItem $firstArrayItem, FuncCall $funcCall, bool $isNegated = false): Node + { $firstArrayItemValue = $firstArrayItem->value; - $firstArg = $node->getArgs()[0]; + $firstArg = $funcCall->getArgs()[0]; // strict - if (isset($node->getArgs()[2])) { - return new Identical($firstArg->value, $firstArrayItemValue); + if (isset($funcCall->getArgs()[2])) { + return $isNegated + ? new NotIdentical($firstArg->value, $firstArrayItemValue) + : new Identical($firstArg->value, $firstArrayItemValue); } - return new Equal($firstArg->value, $firstArrayItemValue); + return $isNegated + ? new NotEqual($firstArg->value, $firstArrayItemValue) + : new Equal($firstArg->value, $firstArrayItemValue); } } From 0ae734767a1561c5e9708f6af537233f35e4da92 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Oct 2023 13:38:04 +0700 Subject: [PATCH 2/3] fixture --- .../Fixture/negated_in_array.php.inc | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/negated_in_array.php.inc diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/negated_in_array.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/negated_in_array.php.inc new file mode 100644 index 00000000000..25d3a78f9e0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/negated_in_array.php.inc @@ -0,0 +1,33 @@ + +----- + From 06110172c2b6fbe72c6bb92e9231593d1ff1bde4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Oct 2023 13:40:07 +0700 Subject: [PATCH 3/3] fixture extract --- .../Fixture/fixture.php.inc | 4 ---- .../Fixture/negated_in_array.php.inc | 4 ---- .../Fixture/skip_multi_data.php.inc | 11 +++++++++++ .../Fixture/skip_negated_multi_data.php.inc | 11 +++++++++++ 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/skip_multi_data.php.inc create mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/skip_negated_multi_data.php.inc diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/fixture.php.inc index 74b37c7d417..af488905505 100644 --- a/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/fixture.php.inc +++ b/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/fixture.php.inc @@ -8,8 +8,6 @@ class Fixture { $isIt = in_array(strtolower($type), ['$this'], true); $isIt = in_array(strtolower($type), ['$this']); - - $isIt = in_array(strtolower($type), ['$this', 'two']); } } @@ -25,8 +23,6 @@ class Fixture { $isIt = strtolower($type) === '$this'; $isIt = strtolower($type) == '$this'; - - $isIt = in_array(strtolower($type), ['$this', 'two']); } } diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/negated_in_array.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/negated_in_array.php.inc index 25d3a78f9e0..8f5cbd03258 100644 --- a/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/negated_in_array.php.inc +++ b/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/negated_in_array.php.inc @@ -8,8 +8,6 @@ class NegatedInArray { $isIt = ! in_array(strtolower($type), ['$this'], true); $isIt = ! in_array(strtolower($type), ['$this']); - - $isIt = ! in_array(strtolower($type), ['$this', 'two']); } } @@ -25,8 +23,6 @@ class NegatedInArray { $isIt = strtolower($type) !== '$this'; $isIt = strtolower($type) != '$this'; - - $isIt = ! in_array(strtolower($type), ['$this', 'two']); } } diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/skip_multi_data.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/skip_multi_data.php.inc new file mode 100644 index 00000000000..f75178d3b9a --- /dev/null +++ b/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/skip_multi_data.php.inc @@ -0,0 +1,11 @@ +