rector/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrposMatchAndRefactor.php

94 lines
2.8 KiB
PHP
Raw Normal View History

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;
use PhpParser\Node\Expr\BooleanNot;
2020-04-23 12:34:27 +00:00
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\NodeNameResolver\NodeNameResolver;
2020-04-23 21:49:56 +00:00
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
use Rector\Php80\NodeFactory\StrStartsWithFuncCallFactory;
use Rector\Php80\ValueObject\StrStartsWith;
2020-04-23 12:34:27 +00:00
final class StrposMatchAndRefactor implements StrStartWithMatchAndRefactorInterface
2020-04-23 12:34:27 +00:00
{
/**
* @var NodeNameResolver
*/
private $nodeNameResolver;
/**
* @var ValueResolver
*/
private $valueResolver;
/**
* @var StrStartsWithFuncCallFactory
*/
private $strStartsWithFuncCallFactory;
public function __construct(
NodeNameResolver $nodeNameResolver,
ValueResolver $valueResolver,
StrStartsWithFuncCallFactory $strStartsWithFuncCallFactory
) {
$this->nodeNameResolver = $nodeNameResolver;
$this->valueResolver = $valueResolver;
$this->strStartsWithFuncCallFactory = $strStartsWithFuncCallFactory;
}
2020-04-23 12:34:27 +00:00
/**
* @param Identical|NotIdentical $binaryOp
*/
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;
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;
return new StrStartsWith($funcCall, $haystack, $needle, $isPositive);
2020-04-23 12:34:27 +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;
return new StrStartsWith($funcCall, $haystack, $needle, $isPositive);
2020-04-23 12:34:27 +00:00
}
return null;
}
/**
* @return FuncCall|BooleanNot
*/
public function refactorStrStartsWith(StrStartsWith $strStartsWith): Node
2020-04-23 12:34:27 +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
return $this->strStartsWithFuncCallFactory->createStrStartsWith($strStartsWith);
2020-04-23 12:34:27 +00:00
}
}