2019-10-13 05:59:52 +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-01-02 16:02:47 +00:00
|
|
|
|
2023-05-22 10:21:31 +00:00
|
|
|
use PhpParser\Node;
|
2023-05-22 13:11:03 +00:00
|
|
|
use PhpParser\Node\Arg;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2023-05-22 13:11:03 +00:00
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\Cast;
|
|
|
|
use PhpParser\Node\Expr\Cast\Array_;
|
|
|
|
use PhpParser\Node\Expr\Cast\Bool_;
|
|
|
|
use PhpParser\Node\Expr\Cast\Double;
|
|
|
|
use PhpParser\Node\Expr\Cast\Int_;
|
|
|
|
use PhpParser\Node\Expr\Cast\Object_;
|
|
|
|
use PhpParser\Node\Expr\Cast\String_;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2023-05-22 10:21:31 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
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-01-02 16:02:47 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://stackoverflow.com/questions/5577003/using-settype-in-php-instead-of-typecasting-using-brackets-what-is-the-differen/5577068#5577068
|
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\SetTypeToCastRector\SetTypeToCastRectorTest
|
2019-01-02 16:02:47 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SetTypeToCastRector extends AbstractRector
|
2019-01-02 16:02:47 +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;
|
2019-01-02 16:02:47 +00:00
|
|
|
/**
|
2021-02-23 01:25:34 +00:00
|
|
|
* @var array<string, class-string<Cast>>
|
2019-01-02 16:02:47 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private const TYPE_TO_CAST = ['array' => Array_::class, 'bool' => Bool_::class, 'boolean' => Bool_::class, 'double' => Double::class, 'float' => Double::class, 'int' => Int_::class, 'integer' => Int_::class, 'object' => Object_::class, 'string' => String_::class];
|
2023-06-12 09:19:31 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const IS_ARG_VALUE_ITEM_SET_TYPE = 'is_arg_value_item_set_type';
|
2023-09-20 12:53:23 +00:00
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-01-02 16:02:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes settype() to (type) where possible', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-01-02 16:02:47 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($foo)
|
|
|
|
{
|
|
|
|
settype($foo, 'string');
|
|
|
|
|
|
|
|
return settype($foo, 'integer');
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-01-02 16:02:47 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2021-07-28 12:55:48 +00:00
|
|
|
public function run($foo)
|
2019-01-02 16:02:47 +00:00
|
|
|
{
|
|
|
|
$foo = (string) $foo;
|
|
|
|
|
|
|
|
return (int) $foo;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-01-02 16:02:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-01-02 16:02:47 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-01-02 16:02:47 +00:00
|
|
|
{
|
2023-05-22 10:21:31 +00:00
|
|
|
return [FuncCall::class, Expression::class, Assign::class, ArrayItem::class, Arg::class];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param FuncCall|Expression|Assign|Expr\ArrayItem|Node\Arg $node
|
2023-08-05 10:29:33 +00:00
|
|
|
* @return null|int|\PhpParser\Node\Stmt\Expression|\PhpParser\Node\Expr\Assign|\PhpParser\Node\Expr\Cast
|
2023-05-22 10:21:31 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node)
|
|
|
|
{
|
|
|
|
if ($node instanceof Arg || $node instanceof ArrayItem) {
|
|
|
|
if ($this->isSetTypeFuncCall($node->value)) {
|
2023-06-12 09:19:31 +00:00
|
|
|
$node->value->setAttribute(self::IS_ARG_VALUE_ITEM_SET_TYPE, \true);
|
2023-05-22 10:21:31 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($node instanceof Assign) {
|
|
|
|
if (!$this->isSetTypeFuncCall($node->expr)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
|
|
|
}
|
|
|
|
if ($node instanceof Expression) {
|
|
|
|
if (!$node->expr instanceof FuncCall) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-09 17:30:30 +00:00
|
|
|
$assignOrCast = $this->refactorFuncCall($node->expr, \true);
|
|
|
|
if (!$assignOrCast instanceof Expr) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new Expression($assignOrCast);
|
2023-05-22 10:21:31 +00:00
|
|
|
}
|
|
|
|
return $this->refactorFuncCall($node, \false);
|
2019-01-02 16:02:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-22 10:21:31 +00:00
|
|
|
* @return \PhpParser\Node\Expr\Assign|null|\PhpParser\Node\Expr\Cast
|
2019-01-02 16:02:47 +00:00
|
|
|
*/
|
2023-05-22 10:21:31 +00:00
|
|
|
private function refactorFuncCall(FuncCall $funcCall, bool $isStandaloneExpression)
|
2019-01-02 16:02:47 +00:00
|
|
|
{
|
2023-05-22 10:21:31 +00:00
|
|
|
if (!$this->isSetTypeFuncCall($funcCall)) {
|
2019-01-02 16:02:47 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-22 10:21:31 +00:00
|
|
|
if ($funcCall->isFirstClassCallable()) {
|
2019-01-02 16:02:47 +00:00
|
|
|
return null;
|
2023-06-12 09:19:31 +00:00
|
|
|
}
|
|
|
|
if ($funcCall->getAttribute(self::IS_ARG_VALUE_ITEM_SET_TYPE) === \true) {
|
|
|
|
return null;
|
2019-01-02 16:02:47 +00:00
|
|
|
}
|
2023-05-22 10:21:31 +00:00
|
|
|
$typeValue = $this->valueResolver->getValue($funcCall->getArgs()[1]->value);
|
2023-05-13 17:20:02 +00:00
|
|
|
if (!\is_string($typeValue)) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
$typeValue = \strtolower($typeValue);
|
2023-05-22 10:21:31 +00:00
|
|
|
$variable = $funcCall->getArgs()[0]->value;
|
2023-05-13 17:20:02 +00:00
|
|
|
if (isset(self::TYPE_TO_CAST[$typeValue])) {
|
|
|
|
$castClass = self::TYPE_TO_CAST[$typeValue];
|
|
|
|
$castNode = new $castClass($variable);
|
2023-05-22 10:21:31 +00:00
|
|
|
if (!$isStandaloneExpression) {
|
|
|
|
return $castNode;
|
2019-01-02 16:02:47 +00:00
|
|
|
}
|
2023-05-22 10:21:31 +00:00
|
|
|
// bare expression? → assign
|
|
|
|
return new Assign($variable, $castNode);
|
2019-01-02 16:02:47 +00:00
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
if ($typeValue === 'null') {
|
|
|
|
return new Assign($variable, $this->nodeFactory->createNull());
|
2019-01-02 16:02:47 +00:00
|
|
|
}
|
2023-05-22 10:21:31 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
private function isSetTypeFuncCall(Expr $expr) : bool
|
|
|
|
{
|
|
|
|
// skip assign of settype() calls
|
|
|
|
if (!$expr instanceof FuncCall) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return $this->isName($expr, 'settype');
|
2019-01-02 16:02:47 +00:00
|
|
|
}
|
|
|
|
}
|