2019-12-27 16:44:29 +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\CodeQuality\Rector\FuncCall ;
2019-12-27 16:44:29 +00:00
2022-06-06 17:12:56 +00:00
use PhpParser\Node ;
use PhpParser\Node\Arg ;
use PhpParser\Node\Expr\Cast\Int_ ;
use PhpParser\Node\Expr\FuncCall ;
use Rector\Core\Rector\AbstractRector ;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2019-12-27 16:44:29 +00:00
/**
2021-04-10 18:47:17 +00:00
* @ changelog https :// tonyshowoff . com / articles / casting - int - faster - than - intval - in - php /
2019-12-27 16:44:29 +00:00
*
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector\IntvalToTypeCastRectorTest
2019-12-27 16:44:29 +00:00
*/
2022-06-06 17:12:56 +00:00
final class IntvalToTypeCastRector extends \Rector\Core\Rector\AbstractRector
2019-12-27 16:44:29 +00:00
{
2022-06-06 17:12:56 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2019-12-27 16:44:29 +00:00
{
2022-06-06 17:12:56 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Change intval() to faster and readable (int) $value' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2019-12-27 16:44:29 +00:00
class SomeClass
{
public function run ( $value )
{
return intval ( $value );
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2019-12-27 16:44:29 +00:00
class SomeClass
{
public function run ( $value )
{
return ( int ) $value ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2019-12-27 16:44:29 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2019-12-27 16:44:29 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2019-12-27 16:44:29 +00:00
{
2022-06-06 17:12:56 +00:00
return [ \PhpParser\Node\Expr\FuncCall :: class ];
2019-12-27 16:44:29 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param FuncCall $node
2019-12-27 16:44:29 +00:00
*/
2022-06-06 17:12:56 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2019-12-27 16:44:29 +00:00
{
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node , 'intval' )) {
2019-12-27 16:44:29 +00:00
return null ;
}
2022-06-06 17:12:56 +00:00
if ( isset ( $node -> args [ 1 ]) && $node -> args [ 1 ] instanceof \PhpParser\Node\Arg ) {
2021-01-30 23:20:05 +00:00
$secondArgumentValue = $this -> valueResolver -> getValue ( $node -> args [ 1 ] -> value );
2019-12-27 16:44:29 +00:00
// default value
if ( $secondArgumentValue !== 10 ) {
return null ;
}
}
2021-09-27 15:43:15 +00:00
if ( ! isset ( $node -> args [ 0 ])) {
return null ;
}
2022-06-06 17:12:56 +00:00
if ( ! $node -> args [ 0 ] instanceof \PhpParser\Node\Arg ) {
2021-09-27 15:43:15 +00:00
return null ;
}
2022-06-06 17:12:56 +00:00
return new \PhpParser\Node\Expr\Cast\Int_ ( $node -> args [ 0 ] -> value );
2019-12-27 16:44:29 +00:00
}
}