2020-04-23 12:34:27 +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 12:34:27 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
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 Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
|
2023-06-08 18:28:01 +00:00
|
|
|
use Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor\StrncmpMatchAndRefactor;
|
|
|
|
use Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor\StrposMatchAndRefactor;
|
|
|
|
use Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor\SubstrMatchAndRefactor;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Php80\ValueObject\StrStartsWith;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
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 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
|
|
|
*
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/RQHB5 for weak compare
|
|
|
|
* @changelog https://3v4l.org/AmLja for weak compare
|
2020-04-23 12:34:27 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php80\Rector\Identical\StrStartsWithRector\StrStartsWithRectorTest
|
2020-04-23 12:34:27 +00:00
|
|
|
*/
|
2023-12-24 12:52:27 +00:00
|
|
|
final class StrStartsWithRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-30 16:07:15 +00:00
|
|
|
* @var StrStartWithMatchAndRefactorInterface[]
|
2020-04-23 12:34:27 +00:00
|
|
|
*/
|
2023-06-08 18:28:01 +00:00
|
|
|
private $strStartWithMatchAndRefactors = [];
|
|
|
|
public function __construct(StrncmpMatchAndRefactor $strncmpMatchAndRefactor, SubstrMatchAndRefactor $substrMatchAndRefactor, StrposMatchAndRefactor $strposMatchAndRefactor)
|
2020-04-23 21:49:56 +00:00
|
|
|
{
|
2023-06-08 18:28:01 +00:00
|
|
|
$this->strStartWithMatchAndRefactors = [$strncmpMatchAndRefactor, $substrMatchAndRefactor, $strposMatchAndRefactor];
|
2020-04-23 12:34:27 +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_STARTS_WITH;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change helper functions to str_starts_with()', [new 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;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-04-23 12:34:27 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$isMatch = str_starts_with($haystack, $needle);
|
|
|
|
|
2020-05-23 13:54:59 +00:00
|
|
|
$isNotMatch = ! str_starts_with($haystack, $needle);
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-04-23 12:34:27 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-04-23 12:34:27 +00:00
|
|
|
{
|
2022-12-29 13:26:54 +00:00
|
|
|
return [Identical::class, NotIdentical::class, Equal::class, NotEqual::class];
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-12-29 13:26:54 +00:00
|
|
|
* @param Identical|NotIdentical|Equal|NotEqual $node
|
2020-04-23 12:34:27 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?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);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$strStartsWithValueObject instanceof StrStartsWith) {
|
2020-04-23 21:49:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-01 17:56:30 +00:00
|
|
|
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
|
|
|
}
|
2023-12-24 12:52:27 +00:00
|
|
|
public function providePolyfillPackage() : string
|
|
|
|
{
|
|
|
|
return PolyfillPackage::PHP_80;
|
|
|
|
}
|
2020-04-23 12:34:27 +00:00
|
|
|
}
|