2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-08-18 15:57:30 +00:00
namespace Rector\CodeQuality\Rector\Identical ;
2019-07-04 21:18:06 +00:00
use PhpParser\Node ;
2021-09-27 15:43:15 +00:00
use PhpParser\Node\Arg ;
2019-07-05 15:06:28 +00:00
use PhpParser\Node\Expr ;
2019-07-04 21:18:06 +00:00
use PhpParser\Node\Expr\BinaryOp\Identical ;
use PhpParser\Node\Expr\FuncCall ;
2019-07-05 15:06:28 +00:00
use PhpParser\Node\Scalar\String_ ;
2021-09-27 15:43:15 +00:00
use Rector\Core\NodeAnalyzer\ArgsAnalyzer ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2019-09-03 09:11:45 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\StrlenZeroToIdenticalEmptyStringRectorTest
2019-09-03 09:11:45 +00:00
*/
2021-05-10 22:23:08 +00:00
final class StrlenZeroToIdenticalEmptyStringRector extends \Rector\Core\Rector\AbstractRector
2019-07-04 21:18:06 +00:00
{
2021-09-27 15:43:15 +00:00
/**
* @ var \Rector\Core\NodeAnalyzer\ArgsAnalyzer
*/
private $argsAnalyzer ;
public function __construct ( \Rector\Core\NodeAnalyzer\ArgsAnalyzer $argsAnalyzer )
{
$this -> argsAnalyzer = $argsAnalyzer ;
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2019-07-04 21:18:06 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Changes strlen comparison to 0 to direct empty string compare' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2019-07-04 21:18:06 +00:00
class SomeClass
{
public function run ( $value )
{
$empty = strlen ( $value ) === 0 ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2019-07-04 21:18:06 +00:00
class SomeClass
{
public function run ( $value )
{
$empty = $value === '' ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2019-07-04 21:18:06 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2019-07-04 21:18:06 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2019-07-04 21:18:06 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Expr\BinaryOp\Identical :: class ];
2019-07-04 21:18:06 +00:00
}
/**
* @ param Identical $node
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2019-07-04 21:18:06 +00:00
{
2021-05-10 22:23:08 +00:00
if ( $node -> left instanceof \PhpParser\Node\Expr\FuncCall ) {
2021-09-27 15:43:15 +00:00
return $this -> processLeftIdentical ( $node , $node -> left );
}
if ( $node -> right instanceof \PhpParser\Node\Expr\FuncCall ) {
return $this -> processRightIdentical ( $node , $node -> right );
}
return null ;
}
private function processLeftIdentical ( \PhpParser\Node\Expr\BinaryOp\Identical $identical , \PhpParser\Node\Expr\FuncCall $funcCall ) : ? \PhpParser\Node\Expr\BinaryOp\Identical
{
if ( ! $this -> isName ( $funcCall , 'strlen' )) {
return null ;
}
if ( ! $this -> valueResolver -> isValue ( $identical -> right , 0 )) {
return null ;
}
if ( ! $this -> argsAnalyzer -> isArgInstanceInArgsPosition ( $funcCall -> args , 0 )) {
return null ;
}
/** @var Arg $firstArg */
$firstArg = $funcCall -> args [ 0 ];
/** @var Expr $variable */
$variable = $firstArg -> value ;
return new \PhpParser\Node\Expr\BinaryOp\Identical ( $variable , new \PhpParser\Node\Scalar\String_ ( '' ));
}
private function processRightIdentical ( \PhpParser\Node\Expr\BinaryOp\Identical $identical , \PhpParser\Node\Expr\FuncCall $funcCall ) : ? \PhpParser\Node\Expr\BinaryOp\Identical
{
if ( ! $this -> isName ( $funcCall , 'strlen' )) {
return null ;
}
if ( ! $this -> valueResolver -> isValue ( $identical -> left , 0 )) {
return null ;
}
if ( ! $this -> argsAnalyzer -> isArgInstanceInArgsPosition ( $funcCall -> args , 0 )) {
2019-07-09 15:06:58 +00:00
return null ;
2019-07-04 21:18:06 +00:00
}
2021-09-27 15:43:15 +00:00
/** @var Arg $firstArg */
$firstArg = $funcCall -> args [ 0 ];
2019-07-05 15:06:28 +00:00
/** @var Expr $variable */
2021-09-27 15:43:15 +00:00
$variable = $firstArg -> value ;
2021-05-10 22:23:08 +00:00
return new \PhpParser\Node\Expr\BinaryOp\Identical ( $variable , new \PhpParser\Node\Scalar\String_ ( '' ));
2019-07-04 21:18:06 +00:00
}
}