|
5 | 5 | namespace Rector\CodeQuality\Rector\FuncCall; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Expr\Assign; |
9 | | -use PhpParser\Node\Expr\Cast; |
10 | | -use PhpParser\Node\Expr\Cast\Array_; |
11 | | -use PhpParser\Node\Expr\Cast\Bool_; |
12 | | -use PhpParser\Node\Expr\Cast\Double; |
13 | | -use PhpParser\Node\Expr\Cast\Int_; |
14 | | -use PhpParser\Node\Expr\Cast\Object_; |
15 | | -use PhpParser\Node\Expr\Cast\String_; |
16 | | -use PhpParser\Node\Expr\FuncCall; |
17 | 8 | use PhpParser\Node\Stmt\Expression; |
18 | | -use Rector\PhpParser\Node\Value\ValueResolver; |
| 9 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 10 | +use Rector\Exception\ShouldNotHappenException; |
19 | 11 | use Rector\Rector\AbstractRector; |
20 | 12 | use Rector\RuleDoc\CodeSample\CodeSample; |
21 | 13 | use Rector\RuleDoc\RuleDefinition; |
22 | 14 |
|
23 | 15 | /** |
24 | | - * @see \Rector\Tests\CodeQuality\Rector\FuncCall\SetTypeToCastRector\SetTypeToCastRectorTest |
| 16 | + * @deprecated This rule is deprecated, as it handles a niche `settype()` use case that is not part of the PHP upgrade path. |
25 | 17 | */ |
26 | | -final class SetTypeToCastRector extends AbstractRector |
| 18 | +final class SetTypeToCastRector extends AbstractRector implements DeprecatedInterface |
27 | 19 | { |
28 | | - /** |
29 | | - * @var array<string, class-string<Cast>> |
30 | | - */ |
31 | | - private const array TYPE_TO_CAST = [ |
32 | | - 'array' => Array_::class, |
33 | | - 'bool' => Bool_::class, |
34 | | - 'boolean' => Bool_::class, |
35 | | - 'double' => Double::class, |
36 | | - 'float' => Double::class, |
37 | | - 'int' => Int_::class, |
38 | | - 'integer' => Int_::class, |
39 | | - 'object' => Object_::class, |
40 | | - 'string' => String_::class, |
41 | | - ]; |
42 | | - |
43 | | - public function __construct( |
44 | | - private readonly ValueResolver $valueResolver |
45 | | - ) { |
46 | | - } |
47 | | - |
48 | 20 | public function getRuleDefinition(): RuleDefinition |
49 | 21 | { |
50 | 22 | return new RuleDefinition( |
@@ -84,62 +56,11 @@ public function getNodeTypes(): array |
84 | 56 | return [Expression::class]; |
85 | 57 | } |
86 | 58 |
|
87 | | - /** |
88 | | - * @param Expression $node |
89 | | - */ |
90 | | - public function refactor(Node $node): null|Expression |
91 | | - { |
92 | | - // skip expr that are not standalone line, as settype() returns success bool value |
93 | | - // and cannot be casted |
94 | | - if (! $node->expr instanceof FuncCall) { |
95 | | - return null; |
96 | | - } |
97 | | - |
98 | | - $assign = $this->refactorFuncCall($node->expr); |
99 | | - if (! $assign instanceof Assign) { |
100 | | - return null; |
101 | | - } |
102 | | - |
103 | | - return new Expression($assign); |
104 | | - } |
105 | | - |
106 | | - private function refactorFuncCall(FuncCall $funcCall): Assign|null |
| 59 | + public function refactor(Node $node): ?Node |
107 | 60 | { |
108 | | - if (! $this->isName($funcCall, 'setType')) { |
109 | | - return null; |
110 | | - } |
111 | | - |
112 | | - if ($funcCall->isFirstClassCallable()) { |
113 | | - return null; |
114 | | - } |
115 | | - |
116 | | - if (count($funcCall->getArgs()) < 2) { |
117 | | - return null; |
118 | | - } |
119 | | - |
120 | | - $secondArg = $funcCall->getArgs()[1]; |
121 | | - |
122 | | - $typeValue = $this->valueResolver->getValue($secondArg->value); |
123 | | - if (! is_string($typeValue)) { |
124 | | - return null; |
125 | | - } |
126 | | - |
127 | | - $typeValue = strtolower($typeValue); |
128 | | - |
129 | | - $firstArg = $funcCall->getArgs()[0]; |
130 | | - $variable = $firstArg->value; |
131 | | - |
132 | | - if (isset(self::TYPE_TO_CAST[$typeValue])) { |
133 | | - $castClass = self::TYPE_TO_CAST[$typeValue]; |
134 | | - $castNode = new $castClass($variable); |
135 | | - |
136 | | - return new Assign($variable, $castNode); |
137 | | - } |
138 | | - |
139 | | - if ($typeValue === 'null') { |
140 | | - return new Assign($variable, $this->nodeFactory->createNull()); |
141 | | - } |
142 | | - |
143 | | - return null; |
| 61 | + throw new ShouldNotHappenException(sprintf( |
| 62 | + '"%s" rule is deprecated, as it handles a niche settype() use case that is not part of the PHP upgrade path', |
| 63 | + self::class |
| 64 | + )); |
144 | 65 | } |
145 | 66 | } |
0 commit comments