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\FuncCall;
|
|
|
|
use PhpParser\Node\Name;
|
2020-04-23 21:49:56 +00:00
|
|
|
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
|
|
|
|
use Rector\Php80\ValueObject\StrStartsWithValueObject;
|
2020-04-23 12:34:27 +00:00
|
|
|
|
2020-04-23 21:49:56 +00:00
|
|
|
final class StrposMatchAndRefactor extends AbstractMatchAndRefactor implements StrStartWithMatchAndRefactorInterface
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Identical|NotIdentical $binaryOp
|
|
|
|
*/
|
2020-04-23 21:49:56 +00:00
|
|
|
public function match(BinaryOp $binaryOp): ?StrStartsWithValueObject
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2020-04-23 21:49:56 +00:00
|
|
|
$isPositive = $binaryOp instanceof Identical;
|
|
|
|
|
2020-04-23 12:34:27 +00:00
|
|
|
if ($this->isFuncCallName($binaryOp->left, 'strpos')) {
|
|
|
|
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 StrStartsWithValueObject($funcCall, $haystack, $needle, $isPositive);
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isFuncCallName($binaryOp->right, 'strpos')) {
|
|
|
|
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 StrStartsWithValueObject($funcCall, $haystack, $needle, $isPositive);
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:49:56 +00:00
|
|
|
/**
|
|
|
|
* @return FuncCall
|
|
|
|
*/
|
2020-05-03 11:38:00 +00:00
|
|
|
public function refactor(StrStartsWithValueObject $strStartsWithValueObject): ?Node
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2020-05-03 11:38:00 +00:00
|
|
|
$strposFuncCall = $strStartsWithValueObject->getFuncCall();
|
2020-04-23 12:34:27 +00:00
|
|
|
$strposFuncCall->name = new Name('str_starts_with');
|
2020-04-23 21:49:56 +00:00
|
|
|
|
2020-04-23 12:34:27 +00:00
|
|
|
return $strposFuncCall;
|
|
|
|
}
|
|
|
|
}
|