2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2018-12-31 11:15:48 +00:00
namespace Rector\CodeQuality\Rector\FuncCall ;
use PhpParser\Node ;
use PhpParser\Node\Expr\Array_ ;
2020-12-21 02:12:42 +00:00
use PhpParser\Node\Expr\ArrayItem ;
2018-12-31 11:15:48 +00:00
use PhpParser\Node\Expr\BinaryOp\Equal ;
use PhpParser\Node\Expr\BinaryOp\Identical ;
use PhpParser\Node\Expr\FuncCall ;
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\FuncCall\SingleInArrayToCompareRector\SingleInArrayToCompareRectorTest
2019-09-03 09:11:45 +00:00
*/
2021-05-10 22:23:08 +00:00
final class SingleInArrayToCompareRector extends \Rector\Core\Rector\AbstractRector
2018-12-31 11:15:48 +00:00
{
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-12-31 11:15:48 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Changes in_array() with single element to ===' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2018-12-31 11:15:48 +00:00
class SomeClass
{
public function run ()
{
if ( in_array ( strtolower ( $type ), [ '$this' ], true )) {
return strtolower ( $type );
}
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2018-12-31 11:15:48 +00:00
class SomeClass
{
public function run ()
{
if ( strtolower ( $type ) === '$this' ) {
return strtolower ( $type );
}
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2018-12-31 11:15:48 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2018-12-31 11:15:48 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2018-12-31 11:15:48 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Expr\FuncCall :: class ];
2018-12-31 11:15:48 +00:00
}
/**
* @ param FuncCall $node
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2018-12-31 11:15:48 +00:00
{
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node , 'in_array' )) {
2018-12-31 11:15:48 +00:00
return null ;
}
2021-05-10 22:23:08 +00:00
if ( ! $node -> args [ 1 ] -> value instanceof \PhpParser\Node\Expr\Array_ ) {
2018-12-31 11:15:48 +00:00
return null ;
}
/** @var Array_ $arrayNode */
$arrayNode = $node -> args [ 1 ] -> value ;
2021-05-09 20:15:43 +00:00
if ( \count ( $arrayNode -> items ) !== 1 ) {
2018-12-31 11:15:48 +00:00
return null ;
}
2020-12-21 02:12:42 +00:00
$firstArrayItem = $arrayNode -> items [ 0 ];
2021-05-10 22:23:08 +00:00
if ( ! $firstArrayItem instanceof \PhpParser\Node\Expr\ArrayItem ) {
2020-12-21 02:12:42 +00:00
return null ;
}
$firstArrayItemValue = $firstArrayItem -> value ;
2020-05-12 15:20:40 +00:00
// strict
if ( isset ( $node -> args [ 2 ])) {
2021-05-10 22:23:08 +00:00
return new \PhpParser\Node\Expr\BinaryOp\Identical ( $node -> args [ 0 ] -> value , $firstArrayItemValue );
2018-12-31 11:15:48 +00:00
}
2021-05-10 22:23:08 +00:00
return new \PhpParser\Node\Expr\BinaryOp\Equal ( $node -> args [ 0 ] -> value , $firstArrayItemValue );
2018-12-31 11:15:48 +00:00
}
}