rector/rules/Php80/Rector/Identical/StrStartsWithRector.php

83 lines
2.7 KiB
PHP
Raw Normal View History

2020-04-23 12:34:27 +00:00
<?php
declare (strict_types=1);
2020-04-23 12:34:27 +00:00
namespace Rector\Php80\Rector\Identical;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use Rector\Core\Rector\AbstractRector;
2020-04-23 21:49:56 +00:00
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
use Rector\Php80\ValueObject\StrStartsWith;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2020-04-23 12:34:27 +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 12:34:27 +00:00
*
* @see https://3v4l.org/RQHB5 for weak compare
* @see https://3v4l.org/AmLja for weak compare
*
* @see \Rector\Tests\Php80\Rector\Identical\StrStartsWithRector\StrStartsWithRectorTest
2020-04-23 12:34:27 +00:00
*/
final class StrStartsWithRector extends \Rector\Core\Rector\AbstractRector
2020-04-23 12:34:27 +00:00
{
/**
* @var \Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface[]
2020-04-23 12:34:27 +00:00
*/
private $strStartWithMatchAndRefactors;
2020-04-23 12:34:27 +00:00
/**
2020-04-23 21:49:56 +00:00
* @param StrStartWithMatchAndRefactorInterface[] $strStartWithMatchAndRefactors
2020-04-23 12:34:27 +00:00
*/
2020-04-23 21:49:56 +00:00
public function __construct(array $strStartWithMatchAndRefactors)
{
$this->strStartWithMatchAndRefactors = $strStartWithMatchAndRefactors;
2020-04-23 12:34:27 +00:00
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-04-23 12:34:27 +00:00
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change helper functions to str_starts_with()', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
2020-04-23 12:34:27 +00:00
class SomeClass
{
public function run()
{
$isMatch = substr($haystack, 0, strlen($needle)) === $needle;
$isNotMatch = substr($haystack, 0, strlen($needle)) !== $needle;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2020-04-23 12:34:27 +00:00
class SomeClass
{
public function run()
{
$isMatch = str_starts_with($haystack, $needle);
$isNotMatch = ! str_starts_with($haystack, $needle);
2020-04-23 12:34:27 +00:00
}
}
CODE_SAMPLE
)]);
2020-04-23 12:34:27 +00:00
}
/**
* @return array<class-string<Node>>
2020-04-23 12:34:27 +00:00
*/
public function getNodeTypes() : array
2020-04-23 12:34:27 +00:00
{
return [\PhpParser\Node\Expr\BinaryOp\Identical::class, \PhpParser\Node\Expr\BinaryOp\NotIdentical::class];
2020-04-23 12:34:27 +00:00
}
/**
* @param Identical|NotIdentical $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
2020-04-23 12:34:27 +00:00
{
2020-04-23 21:49:56 +00:00
foreach ($this->strStartWithMatchAndRefactors as $strStartWithMatchAndRefactor) {
$strStartsWithValueObject = $strStartWithMatchAndRefactor->match($node);
if (!$strStartsWithValueObject instanceof \Rector\Php80\ValueObject\StrStartsWith) {
2020-04-23 21:49:56 +00:00
continue;
}
return $strStartWithMatchAndRefactor->refactorStrStartsWith($strStartsWithValueObject);
2020-04-23 12:34:27 +00:00
}
2020-07-01 21:35:40 +00:00
return null;
2020-04-23 12:34:27 +00:00
}
}