2020-04-23 12:34:27 +00:00
< ? php
2021-05-09 20:15:43 +00:00
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 ;
2021-01-20 11:41:35 +00:00
use Rector\Php80\ValueObject\StrStartsWith ;
2020-11-16 17:50:38 +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
*
* @ see https :// 3 v4l . org / RQHB5 for weak compare
* @ see https :// 3 v4l . org / AmLja for weak compare
*
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Php80\Rector\Identical\StrStartsWithRector\StrStartsWithRectorTest
2020-04-23 12:34:27 +00:00
*/
2021-05-10 22:23:08 +00:00
final class StrStartsWithRector extends \Rector\Core\Rector\AbstractRector
2020-04-23 12:34:27 +00:00
{
/**
2021-08-22 21:22:18 +00:00
* @ var \Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface []
2020-04-23 12:34:27 +00:00
*/
2021-05-10 23:39:21 +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
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-04-23 12:34:27 +00:00
{
2021-05-10 22:23:08 +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 ;
}
}
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
{
2021-05-10 22:23:08 +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
*/
2021-07-05 22:50:18 +00:00
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 );
2021-05-10 22:23:08 +00:00
if ( ! $strStartsWithValueObject instanceof \Rector\Php80\ValueObject\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
}
}