2020-04-23 22:28:33 +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\Rector\Identical;
|
2020-04-23 22:28:33 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
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\Expr\UnaryMinus;
|
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeAnalyzer\BinaryOpAnalyzer;
|
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\FuncCallAndExpr;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\ValueObject\PolyfillPackage;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2023-12-24 12:52:27 +00:00
|
|
|
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-04-23 22:28:33 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions
|
2020-04-23 22:28:33 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\StrEndsWithRectorTest
|
2020-04-23 22:28:33 +00:00
|
|
|
*/
|
2023-12-24 12:52:27 +00:00
|
|
|
final class StrEndsWithRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
|
2020-04-23 22:28:33 +00:00
|
|
|
{
|
2021-02-23 01:25:34 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\BinaryOpAnalyzer
|
2021-02-23 01:25:34 +00:00
|
|
|
*/
|
|
|
|
private $binaryOpAnalyzer;
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(BinaryOpAnalyzer $binaryOpAnalyzer, ValueResolver $valueResolver)
|
2021-02-23 01:25:34 +00:00
|
|
|
{
|
|
|
|
$this->binaryOpAnalyzer = $binaryOpAnalyzer;
|
2023-09-20 12:53:23 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2021-02-23 01:25:34 +00:00
|
|
|
}
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::STR_ENDS_WITH;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-04-23 22:28:33 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change helper functions to str_ends_with()', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-04-23 22:28:33 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$isMatch = substr($haystack, -strlen($needle)) === $needle;
|
2021-06-30 11:27:30 +00:00
|
|
|
|
|
|
|
$isNotMatch = substr($haystack, -strlen($needle)) !== $needle;
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-04-23 22:28:33 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$isMatch = str_ends_with($haystack, $needle);
|
2021-06-30 11:27:30 +00:00
|
|
|
|
|
|
|
$isNotMatch = !str_ends_with($haystack, $needle);
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2022-06-07 08:22:29 +00:00
|
|
|
), new CodeSample(<<<'CODE_SAMPLE'
|
2021-07-07 14:26:24 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$isMatch = substr($haystack, -9) === 'hardcoded;
|
|
|
|
|
|
|
|
$isNotMatch = substr($haystack, -9) !== 'hardcoded';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$isMatch = str_ends_with($haystack, 'hardcoded');
|
|
|
|
|
|
|
|
$isNotMatch = !str_ends_with($haystack, 'hardcoded');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-04-23 22:28:33 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-04-23 22:28:33 +00:00
|
|
|
{
|
2022-12-29 13:26:54 +00:00
|
|
|
return [Identical::class, NotIdentical::class, Equal::class, NotEqual::class];
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-12-29 13:26:54 +00:00
|
|
|
* @param Identical|NotIdentical|Equal|NotEqual $node
|
2020-04-23 22:28:33 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-04-23 22:28:33 +00:00
|
|
|
{
|
|
|
|
return $this->refactorSubstr($node) ?? $this->refactorSubstrCompare($node);
|
|
|
|
}
|
2023-12-24 12:52:27 +00:00
|
|
|
public function providePolyfillPackage() : string
|
|
|
|
{
|
|
|
|
return PolyfillPackage::PHP_80;
|
|
|
|
}
|
2020-04-23 22:28:33 +00:00
|
|
|
/**
|
|
|
|
* Covers:
|
|
|
|
* $isMatch = substr($haystack, -strlen($needle)) === $needle;
|
2021-07-07 14:26:24 +00:00
|
|
|
* $isMatch = 'needle' === substr($haystack, -6)
|
2022-08-11 14:46:08 +00:00
|
|
|
* @return \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\BooleanNot|null
|
2020-04-23 22:28:33 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorSubstr(BinaryOp $binaryOp)
|
2020-04-23 22:28:33 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($binaryOp->left instanceof FuncCall && $this->isName($binaryOp->left, 'substr')) {
|
2020-04-23 22:28:33 +00:00
|
|
|
$substrFuncCall = $binaryOp->left;
|
|
|
|
$comparedNeedleExpr = $binaryOp->right;
|
2022-06-07 08:22:29 +00:00
|
|
|
} elseif ($binaryOp->right instanceof FuncCall && $this->isName($binaryOp->right, 'substr')) {
|
2020-04-23 22:28:33 +00:00
|
|
|
$substrFuncCall = $binaryOp->right;
|
|
|
|
$comparedNeedleExpr = $binaryOp->left;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
if ($substrFuncCall->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (\count($substrFuncCall->getArgs()) < 2) {
|
2020-04-23 22:28:33 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
$needle = $substrFuncCall->getArgs()[1]->value;
|
|
|
|
if (!$this->isUnaryMinusStrlenFuncCallArgValue($needle, $comparedNeedleExpr) && !$this->isHardCodedLNumberAndString($needle, $comparedNeedleExpr)) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
$haystack = $substrFuncCall->getArgs()[0]->value;
|
2022-12-29 13:26:54 +00:00
|
|
|
$isPositive = $binaryOp instanceof Identical || $binaryOp instanceof Equal;
|
2021-07-07 14:26:24 +00:00
|
|
|
return $this->buildReturnNode($haystack, $comparedNeedleExpr, $isPositive);
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|
2022-08-11 14:46:08 +00:00
|
|
|
/**
|
|
|
|
* @return \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\BooleanNot|null
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorSubstrCompare(BinaryOp $binaryOp)
|
2020-04-23 22:28:33 +00:00
|
|
|
{
|
2021-02-23 01:25:34 +00:00
|
|
|
$funcCallAndExpr = $this->binaryOpAnalyzer->matchFuncCallAndOtherExpr($binaryOp, 'substr_compare');
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$funcCallAndExpr instanceof FuncCallAndExpr) {
|
2021-02-23 01:25:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$expr = $funcCallAndExpr->getExpr();
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isValue($expr, 0)) {
|
2020-04-23 22:28:33 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-02-23 01:25:34 +00:00
|
|
|
$substrCompareFuncCall = $funcCallAndExpr->getFuncCall();
|
2024-03-02 13:08:36 +00:00
|
|
|
$args = $substrCompareFuncCall->getArgs();
|
|
|
|
if (\count($args) < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$haystack = $args[0]->value;
|
|
|
|
$needle = $args[1]->value;
|
|
|
|
$thirdArgValue = $args[2]->value;
|
|
|
|
$isCaseInsensitiveValue = isset($args[4]) ? $this->valueResolver->getValue($args[4]->value) : null;
|
|
|
|
// is case insensitive → not valid replacement
|
|
|
|
if ($isCaseInsensitiveValue === \true) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$this->isUnaryMinusStrlenFuncCallArgValue($thirdArgValue, $needle) && !$this->isHardCodedLNumberAndString($thirdArgValue, $needle)) {
|
2020-04-23 22:28:33 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-29 13:26:54 +00:00
|
|
|
$isPositive = $binaryOp instanceof Identical || $binaryOp instanceof Equal;
|
2021-06-30 15:42:45 +00:00
|
|
|
return $this->buildReturnNode($haystack, $needle, $isPositive);
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isUnaryMinusStrlenFuncCallArgValue(Expr $substrOffset, Expr $needle) : bool
|
2020-04-23 22:28:33 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$substrOffset instanceof UnaryMinus) {
|
2021-07-07 14:26:24 +00:00
|
|
|
return \false;
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$substrOffset->expr instanceof FuncCall) {
|
2021-07-07 14:26:24 +00:00
|
|
|
return \false;
|
2021-03-19 23:11:36 +00:00
|
|
|
}
|
2021-07-07 14:26:24 +00:00
|
|
|
$funcCall = $substrOffset->expr;
|
|
|
|
if (!$this->nodeNameResolver->isName($funcCall, 'strlen')) {
|
|
|
|
return \false;
|
|
|
|
}
|
2023-05-08 07:49:33 +00:00
|
|
|
if (!isset($funcCall->getArgs()[0])) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$funcCall->args[0] instanceof Arg) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2021-07-07 14:26:24 +00:00
|
|
|
return $this->nodeComparator->areNodesEqual($funcCall->args[0]->value, $needle);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isHardCodedLNumberAndString(Expr $substrOffset, Expr $needle) : bool
|
2021-07-07 14:26:24 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$substrOffset instanceof UnaryMinus) {
|
2021-07-07 14:26:24 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$substrOffset->expr instanceof LNumber) {
|
2021-07-07 14:26:24 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
$lNumber = $substrOffset->expr;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$needle instanceof String_) {
|
2021-07-07 14:26:24 +00:00
|
|
|
return \false;
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|
2021-07-07 14:26:24 +00:00
|
|
|
return $lNumber->value === \strlen($needle->value);
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|
2021-06-30 15:42:45 +00:00
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @return \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\BooleanNot
|
2021-06-30 15:42:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function buildReturnNode(Expr $haystack, Expr $needle, bool $isPositive)
|
2021-06-30 15:42:45 +00:00
|
|
|
{
|
|
|
|
$funcCall = $this->nodeFactory->createFuncCall('str_ends_with', [$haystack, $needle]);
|
|
|
|
if (!$isPositive) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return new BooleanNot($funcCall);
|
2021-06-30 15:42:45 +00:00
|
|
|
}
|
|
|
|
return $funcCall;
|
|
|
|
}
|
2020-04-23 22:28:33 +00:00
|
|
|
}
|