2020-04-23 12:34:27 +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\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor;
|
2020-04-23 12:34:27 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
2022-12-29 13:26:54 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Equal;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
2022-12-29 13:26:54 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotEqual;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
|
|
|
|
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
|
|
|
|
use Rector\Php80\NodeFactory\StrStartsWithFuncCallFactory;
|
|
|
|
use Rector\Php80\ValueObject\StrStartsWith;
|
2022-06-07 08:22:29 +00:00
|
|
|
final class StrposMatchAndRefactor implements StrStartWithMatchAndRefactorInterface
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2021-02-28 12:53:28 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2021-02-28 12:53:28 +00:00
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
|
2021-02-28 12:53:28 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Php80\NodeFactory\StrStartsWithFuncCallFactory
|
2021-02-28 12:53:28 +00:00
|
|
|
*/
|
2021-03-02 13:43:36 +00:00
|
|
|
private $strStartsWithFuncCallFactory;
|
2021-09-27 15:43:15 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-09-27 15:43:15 +00:00
|
|
|
* @var \Rector\Core\NodeAnalyzer\ArgsAnalyzer
|
|
|
|
*/
|
|
|
|
private $argsAnalyzer;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(NodeNameResolver $nodeNameResolver, ValueResolver $valueResolver, StrStartsWithFuncCallFactory $strStartsWithFuncCallFactory, ArgsAnalyzer $argsAnalyzer)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-02-28 12:53:28 +00:00
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
|
|
$this->valueResolver = $valueResolver;
|
2021-03-02 13:43:36 +00:00
|
|
|
$this->strStartsWithFuncCallFactory = $strStartsWithFuncCallFactory;
|
2021-09-27 15:43:15 +00:00
|
|
|
$this->argsAnalyzer = $argsAnalyzer;
|
2021-02-28 12:53:28 +00:00
|
|
|
}
|
2020-04-23 12:34:27 +00:00
|
|
|
/**
|
2022-12-29 13:26:54 +00:00
|
|
|
* @param \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical|\PhpParser\Node\Expr\BinaryOp\Equal|\PhpParser\Node\Expr\BinaryOp\NotEqual $binaryOp
|
2020-04-23 12:34:27 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function match($binaryOp) : ?StrStartsWith
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2022-12-29 13:26:54 +00:00
|
|
|
$isPositive = $binaryOp instanceof Identical || $binaryOp instanceof Equal;
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($binaryOp->left instanceof FuncCall && $this->nodeNameResolver->isName($binaryOp->left, 'strpos')) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return $this->processBinaryOpLeft($binaryOp, $isPositive);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$binaryOp->right instanceof FuncCall) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
2021-09-27 15:43:15 +00:00
|
|
|
if (!$this->nodeNameResolver->isName($binaryOp->right, 'strpos')) {
|
|
|
|
return null;
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
2021-09-27 15:43:15 +00:00
|
|
|
return $this->processBinaryOpRight($binaryOp, $isPositive);
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
2021-02-28 12:53:28 +00:00
|
|
|
/**
|
|
|
|
* @return FuncCall|BooleanNot
|
|
|
|
*/
|
2023-02-11 11:34:35 +00:00
|
|
|
public function refactorStrStartsWith(StrStartsWith $strStartsWith) : ?\PhpParser\Node
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2020-09-01 17:56:30 +00:00
|
|
|
$strposFuncCall = $strStartsWith->getFuncCall();
|
2022-06-07 08:22:29 +00:00
|
|
|
$strposFuncCall->name = new Name('str_starts_with');
|
2021-03-02 13:43:36 +00:00
|
|
|
return $this->strStartsWithFuncCallFactory->createStrStartsWith($strStartsWith);
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processBinaryOpLeft(BinaryOp $binaryOp, bool $isPositive) : ?StrStartsWith
|
2021-09-27 15:43:15 +00:00
|
|
|
{
|
|
|
|
if (!$this->valueResolver->isValue($binaryOp->right, 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var FuncCall $funcCall */
|
|
|
|
$funcCall = $binaryOp->left;
|
|
|
|
if (!$this->argsAnalyzer->isArgsInstanceInArgsPositions($funcCall->args, [0, 1])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var Arg $firstArg */
|
|
|
|
$firstArg = $funcCall->args[0];
|
|
|
|
$haystack = $firstArg->value;
|
|
|
|
/** @var Arg $secondArg */
|
|
|
|
$secondArg = $funcCall->args[1];
|
|
|
|
$needle = $secondArg->value;
|
2022-06-07 08:22:29 +00:00
|
|
|
return new StrStartsWith($funcCall, $haystack, $needle, $isPositive);
|
2021-09-27 15:43:15 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processBinaryOpRight(BinaryOp $binaryOp, bool $isPositive) : ?StrStartsWith
|
2021-09-27 15:43:15 +00:00
|
|
|
{
|
|
|
|
if (!$this->valueResolver->isValue($binaryOp->left, 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var FuncCall $funcCall */
|
|
|
|
$funcCall = $binaryOp->right;
|
|
|
|
if (!$this->argsAnalyzer->isArgsInstanceInArgsPositions($funcCall->args, [0, 1])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var Arg $firstArg */
|
|
|
|
$firstArg = $funcCall->args[0];
|
|
|
|
$haystack = $firstArg->value;
|
|
|
|
/** @var Arg $secondArg */
|
|
|
|
$secondArg = $funcCall->args[1];
|
|
|
|
$needle = $secondArg->value;
|
2022-06-07 08:22:29 +00:00
|
|
|
return new StrStartsWith($funcCall, $haystack, $needle, $isPositive);
|
2021-09-27 15:43:15 +00:00
|
|
|
}
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|