2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-10-13 09:35:23 +00:00
|
|
|
|
2018-10-17 12:27:22 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Identical;
|
2018-10-13 09:35:23 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2021-02-08 12:33:17 +00:00
|
|
|
use Rector\Core\NodeManipulator\BinaryOpManipulator;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-01-20 11:41:35 +00:00
|
|
|
use Rector\Php71\ValueObject\TwoNodeMatch;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-10-13 09:35:23 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Identical\SimplifyArraySearchRector\SimplifyArraySearchRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2018-10-13 09:35:23 +00:00
|
|
|
final class SimplifyArraySearchRector extends AbstractRector
|
|
|
|
{
|
2018-11-07 17:04:38 +00:00
|
|
|
/**
|
2019-02-27 21:54:39 +00:00
|
|
|
* @var BinaryOpManipulator
|
2018-11-07 17:04:38 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $binaryOpManipulator;
|
2018-11-07 17:04:38 +00:00
|
|
|
|
2019-02-27 21:54:39 +00:00
|
|
|
public function __construct(BinaryOpManipulator $binaryOpManipulator)
|
2018-11-07 17:04:38 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->binaryOpManipulator = $binaryOpManipulator;
|
2018-11-07 17:04:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-10-13 09:35:23 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
2018-10-13 09:35:23 +00:00
|
|
|
'Simplify array_search to in_array',
|
|
|
|
[
|
2020-05-24 10:21:45 +00:00
|
|
|
new CodeSample('array_search("searching", $array) !== false;', 'in_array("searching", $array);'),
|
2018-10-13 09:35:23 +00:00
|
|
|
new CodeSample(
|
2020-05-24 10:21:45 +00:00
|
|
|
'array_search("searching", $array, true) !== false;',
|
2018-10-13 09:35:23 +00:00
|
|
|
'in_array("searching", $array, true);'
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-13 09:35:23 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2020-05-24 10:21:45 +00:00
|
|
|
return [Identical::class, NotIdentical::class];
|
2018-10-13 09:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-24 10:21:45 +00:00
|
|
|
* @param Identical|NotIdentical $node
|
2018-10-13 09:35:23 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2020-08-23 09:39:09 +00:00
|
|
|
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode(
|
2018-11-07 17:04:38 +00:00
|
|
|
$node,
|
2019-05-09 13:30:39 +00:00
|
|
|
function (Node $node): bool {
|
2021-03-19 23:11:36 +00:00
|
|
|
if (! $node instanceof FuncCall) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->nodeNameResolver->isName($node, 'array_search');
|
2018-11-07 17:04:38 +00:00
|
|
|
},
|
2019-05-09 13:30:39 +00:00
|
|
|
function (Node $node): bool {
|
2021-01-30 23:20:05 +00:00
|
|
|
return $this->valueResolver->isFalse($node);
|
2018-11-07 17:04:38 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $twoNodeMatch instanceof TwoNodeMatch) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-13 09:35:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
/** @var FuncCall $arraySearchFuncCall */
|
2020-08-23 09:39:09 +00:00
|
|
|
$arraySearchFuncCall = $twoNodeMatch->getFirstExpr();
|
2018-10-13 09:35:23 +00:00
|
|
|
|
2021-01-30 21:41:25 +00:00
|
|
|
$inArrayFuncCall = $this->nodeFactory->createFuncCall('in_array', $arraySearchFuncCall->args);
|
2018-10-13 09:35:23 +00:00
|
|
|
|
2020-05-24 10:21:45 +00:00
|
|
|
if ($node instanceof Identical) {
|
2018-10-13 09:35:23 +00:00
|
|
|
return new BooleanNot($inArrayFuncCall);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $inArrayFuncCall;
|
|
|
|
}
|
|
|
|
}
|