2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-01-02 16:02:47 +00:00
|
|
|
|
2020-07-27 08:16:16 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
2019-01-02 16:02:47 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
2021-02-23 01:25:34 +00:00
|
|
|
use PhpParser\Node\Expr\Cast;
|
2019-01-02 16:02:47 +00:00
|
|
|
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;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-12-25 11:53:26 +00:00
|
|
|
use Rector\Core\Util\StaticInstanceOf;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-01-02 16:02:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://stackoverflow.com/questions/5577003/using-settype-in-php-instead-of-typecasting-using-brackets-what-is-the-differen/5577068#5577068
|
|
|
|
* @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/3709
|
2020-07-27 08:16:16 +00:00
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\SetTypeToCastRector\SetTypeToCastRectorTest
|
2019-01-02 16:02:47 +00:00
|
|
|
*/
|
|
|
|
final class SetTypeToCastRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
2021-02-23 01:25:34 +00:00
|
|
|
* @var array<string, class-string<Cast>>
|
2019-01-02 16:02:47 +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,
|
|
|
|
];
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-01-02 16:02:47 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Changes settype() to (type) where possible', [
|
2019-01-02 16:02:47 +00:00
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'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
|
2019-01-02 16:02:47 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-01-02 16:02:47 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run(array $items)
|
|
|
|
{
|
|
|
|
$foo = (string) $foo;
|
|
|
|
|
|
|
|
return (int) $foo;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-01-02 16:02:47 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isName($node, 'settype')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-30 23:20:05 +00:00
|
|
|
$typeNode = $this->valueResolver->getValue($node->args[1]->value);
|
2019-01-02 16:02:47 +00:00
|
|
|
if ($typeNode === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$typeNode = strtolower($typeNode);
|
|
|
|
|
|
|
|
$varNode = $node->args[0]->value;
|
2019-04-13 09:20:27 +00:00
|
|
|
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
2020-12-25 11:53:26 +00:00
|
|
|
|
2019-01-02 16:02:47 +00:00
|
|
|
// result of function or probably used
|
2020-12-25 11:53:26 +00:00
|
|
|
if (StaticInstanceOf::isOneOf($parentNode, [Expr::class, Arg::class])) {
|
2019-01-02 16:02:47 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset(self::TYPE_TO_CAST[$typeNode])) {
|
|
|
|
$castClass = self::TYPE_TO_CAST[$typeNode];
|
|
|
|
$castNode = new $castClass($varNode);
|
|
|
|
|
|
|
|
if ($parentNode instanceof Expression) {
|
|
|
|
// bare expression? → assign
|
|
|
|
return new Assign($varNode, $castNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $castNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($typeNode === 'null') {
|
2021-01-30 21:41:25 +00:00
|
|
|
return new Assign($varNode, $this->nodeFactory->createNull());
|
2019-01-02 16:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|