rector/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrposMatchAndRefactor.php
Tomas Votruba 7d36c3b0b9 Updated Rector to commit a80cf5292d
a80cf5292d revert to working scoper 0.14
2021-05-10 22:23:08 +00:00

76 lines
3.1 KiB
PHP

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