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;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
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-07 08:22:29 +00:00
|
|
|
final class IntvalToTypeCastRector extends AbstractRector
|
2019-12-27 16:44:29 +00:00
|
|
|
{
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-12-27 16:44:29 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change intval() to faster and readable (int) $value', [new 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-07 08:22:29 +00:00
|
|
|
return [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-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?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-07 08:22:29 +00:00
|
|
|
if (isset($node->args[1]) && $node->args[1] instanceof 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;
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 22:33:20 +00:00
|
|
|
if ($node->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-08 07:37:37 +00:00
|
|
|
if (!isset($node->getArgs()[0])) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-08 07:37:37 +00:00
|
|
|
return new Int_($node->getArgs()[0]->value);
|
2019-12-27 16:44:29 +00:00
|
|
|
}
|
|
|
|
}
|