-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
SetTypeToCastRector.php
124 lines (107 loc) · 3.07 KB
/
SetTypeToCastRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php declare(strict_types=1);
namespace Rector\Celebrity\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
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;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @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
*/
final class SetTypeToCastRector extends AbstractRector
{
/**
* @var string[]
*/
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,
];
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Changes settype() to (type) where possible', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($foo)
{
settype($foo, 'string');
return settype($foo, 'integer');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(array $items)
{
$foo = (string) $foo;
return (int) $foo;
}
}
CODE_SAMPLE
),
]);
}
/**
* @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;
}
$typeNode = $this->getValue($node->args[1]->value);
if ($typeNode === null) {
return null;
}
$typeNode = strtolower($typeNode);
$varNode = $node->args[0]->value;
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
// result of function or probably used
if ($parentNode instanceof Expr || $parentNode instanceof Arg) {
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') {
return new Assign($varNode, $this->createNull());
}
return $node;
}
}