2020-04-23 12:34:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor;
|
|
|
|
|
2020-04-23 21:49:56 +00:00
|
|
|
use PhpParser\Node;
|
2020-04-23 12:34:27 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
2021-02-28 12:53:28 +00:00
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2020-04-23 12:34:27 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Name;
|
2021-02-28 12:53:28 +00:00
|
|
|
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
2020-04-23 21:49:56 +00:00
|
|
|
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
|
2021-02-28 12:53:28 +00:00
|
|
|
use Rector\Php80\NodeFactory\StrStartsWithFuncCallFactory;
|
2020-09-01 17:56:30 +00:00
|
|
|
use Rector\Php80\ValueObject\StrStartsWith;
|
2020-04-23 12:34:27 +00:00
|
|
|
|
2021-02-28 12:53:28 +00:00
|
|
|
final class StrposMatchAndRefactor implements StrStartWithMatchAndRefactorInterface
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2021-02-28 12:53:28 +00:00
|
|
|
/**
|
|
|
|
* @var NodeNameResolver
|
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ValueResolver
|
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var StrStartsWithFuncCallFactory
|
|
|
|
*/
|
2021-03-02 13:43:36 +00:00
|
|
|
private $strStartsWithFuncCallFactory;
|
2021-02-28 12:53:28 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
NodeNameResolver $nodeNameResolver,
|
|
|
|
ValueResolver $valueResolver,
|
2021-03-02 13:43:36 +00:00
|
|
|
StrStartsWithFuncCallFactory $strStartsWithFuncCallFactory
|
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-02-28 12:53:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 12:34:27 +00:00
|
|
|
/**
|
|
|
|
* @param Identical|NotIdentical $binaryOp
|
|
|
|
*/
|
2020-09-01 17:56:30 +00:00
|
|
|
public function match(BinaryOp $binaryOp): ?StrStartsWith
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2020-04-23 21:49:56 +00:00
|
|
|
$isPositive = $binaryOp instanceof Identical;
|
|
|
|
|
2021-02-28 12:53:28 +00:00
|
|
|
if ($binaryOp->left instanceof FuncCall && $this->nodeNameResolver->isName($binaryOp->left, 'strpos')) {
|
2020-04-23 12:34:27 +00:00
|
|
|
if (! $this->valueResolver->isValue($binaryOp->right, 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var FuncCall $funcCall */
|
|
|
|
$funcCall = $binaryOp->left;
|
2020-04-23 21:49:56 +00:00
|
|
|
$haystack = $funcCall->args[0]->value;
|
|
|
|
$needle = $funcCall->args[1]->value;
|
|
|
|
|
2020-09-01 17:56:30 +00:00
|
|
|
return new StrStartsWith($funcCall, $haystack, $needle, $isPositive);
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 12:53:28 +00:00
|
|
|
if ($binaryOp->right instanceof FuncCall && $this->nodeNameResolver->isName($binaryOp->right, 'strpos')) {
|
2020-04-23 12:34:27 +00:00
|
|
|
if (! $this->valueResolver->isValue($binaryOp->left, 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var FuncCall $funcCall */
|
|
|
|
$funcCall = $binaryOp->right;
|
2020-04-23 21:49:56 +00:00
|
|
|
$haystack = $funcCall->args[0]->value;
|
|
|
|
$needle = $funcCall->args[1]->value;
|
|
|
|
|
2020-09-01 17:56:30 +00:00
|
|
|
return new StrStartsWith($funcCall, $haystack, $needle, $isPositive);
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-28 12:53:28 +00:00
|
|
|
/**
|
|
|
|
* @return FuncCall|BooleanNot
|
|
|
|
*/
|
|
|
|
public function refactorStrStartsWith(StrStartsWith $strStartsWith): Node
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2020-09-01 17:56:30 +00:00
|
|
|
$strposFuncCall = $strStartsWith->getFuncCall();
|
2020-04-23 12:34:27 +00:00
|
|
|
$strposFuncCall->name = new Name('str_starts_with');
|
2020-04-23 21:49:56 +00:00
|
|
|
|
2021-03-02 13:43:36 +00:00
|
|
|
return $this->strStartsWithFuncCallFactory->createStrStartsWith($strStartsWith);
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
|
|
|
}
|