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 new file mode 100644 index 00000000000..8f5cbd03258 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector/Fixture/negated_in_array.php.inc @@ -0,0 +1,29 @@ + +----- + 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 @@ +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); } }