2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
2018-12-31 11:15:48 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Equal;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
2023-10-28 06:44:30 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotEqual;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector\SingleInArrayToCompareRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SingleInArrayToCompareRector extends AbstractRector
|
2018-12-31 11:15:48 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-31 11:15:48 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes in_array() with single element to ===', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-31 11:15:48 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if (in_array(strtolower($type), ['$this'], true)) {
|
|
|
|
return strtolower($type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-31 11:15:48 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if (strtolower($type) === '$this') {
|
|
|
|
return strtolower($type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-31 11:15:48 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-31 11:15:48 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-31 11:15:48 +00:00
|
|
|
{
|
2023-10-28 06:44:30 +00:00
|
|
|
return [BooleanNot::class, FuncCall::class];
|
2018-12-31 11:15:48 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-10-28 06:44:30 +00:00
|
|
|
* @param BooleanNot|FuncCall $node
|
2018-12-31 11:15:48 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-31 11:15:48 +00:00
|
|
|
{
|
2023-10-28 06:44:30 +00:00
|
|
|
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')) {
|
2018-12-31 11:15:48 +00:00
|
|
|
return null;
|
2023-06-02 12:32:12 +00:00
|
|
|
}
|
2023-10-28 06:44:30 +00:00
|
|
|
if ($funcCall->isFirstClassCallable()) {
|
2023-06-02 12:32:12 +00:00
|
|
|
return null;
|
2018-12-31 11:15:48 +00:00
|
|
|
}
|
2023-10-28 06:44:30 +00:00
|
|
|
if (!isset($funcCall->args[1])) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-10-28 06:44:30 +00:00
|
|
|
if (!$funcCall->args[1] instanceof Arg) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-10-28 06:44:30 +00:00
|
|
|
if (!$funcCall->args[1]->value instanceof Array_) {
|
2018-12-31 11:15:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var Array_ $arrayNode */
|
2023-10-28 06:44:30 +00:00
|
|
|
$arrayNode = $funcCall->args[1]->value;
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($arrayNode->items) !== 1) {
|
2018-12-31 11:15:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-12-21 02:12:42 +00:00
|
|
|
$firstArrayItem = $arrayNode->items[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$firstArrayItem instanceof ArrayItem) {
|
2020-12-21 02:12:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-04-21 12:00:51 +00:00
|
|
|
if ($firstArrayItem->unpack) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-28 06:44:30 +00:00
|
|
|
if (!isset($funcCall->getArgs()[0])) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-10-28 06:44:30 +00:00
|
|
|
return $firstArrayItem;
|
|
|
|
}
|
|
|
|
private function processCompare(ArrayItem $firstArrayItem, FuncCall $funcCall, bool $isNegated = \false) : Node
|
|
|
|
{
|
2020-12-21 02:12:42 +00:00
|
|
|
$firstArrayItemValue = $firstArrayItem->value;
|
2023-10-28 06:44:30 +00:00
|
|
|
$firstArg = $funcCall->getArgs()[0];
|
2020-05-12 15:20:40 +00:00
|
|
|
// strict
|
2023-10-28 06:44:30 +00:00
|
|
|
if (isset($funcCall->getArgs()[2])) {
|
|
|
|
return $isNegated ? new NotIdentical($firstArg->value, $firstArrayItemValue) : new Identical($firstArg->value, $firstArrayItemValue);
|
2018-12-31 11:15:48 +00:00
|
|
|
}
|
2023-10-28 06:44:30 +00:00
|
|
|
return $isNegated ? new NotEqual($firstArg->value, $firstArrayItemValue) : new Equal($firstArg->value, $firstArrayItemValue);
|
2018-12-31 11:15:48 +00:00
|
|
|
}
|
|
|
|
}
|