Skip to content

Commit 13a02cd

Browse files
committed
[CodeQuality] Deprecate SetTypeToCastRector as niche rule, not part of PHP upgrade path
1 parent 3ddaa09 commit 13a02cd

9 files changed

Lines changed: 9 additions & 230 deletions

File tree

‎rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/fixture.php.inc‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_args.php.inc‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_array_items.php.inc‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_assign_and_return.php.inc‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_invalid_cast_type.php.inc‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/SetTypeToCastRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/config/configured_rule.php‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php‎

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,18 @@
55
namespace Rector\CodeQuality\Rector\FuncCall;
66

77
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;
178
use PhpParser\Node\Stmt\Expression;
18-
use Rector\PhpParser\Node\Value\ValueResolver;
9+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
10+
use Rector\Exception\ShouldNotHappenException;
1911
use Rector\Rector\AbstractRector;
2012
use Rector\RuleDoc\CodeSample\CodeSample;
2113
use Rector\RuleDoc\RuleDefinition;
2214

2315
/**
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.
2517
*/
26-
final class SetTypeToCastRector extends AbstractRector
18+
final class SetTypeToCastRector extends AbstractRector implements DeprecatedInterface
2719
{
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-
4820
public function getRuleDefinition(): RuleDefinition
4921
{
5022
return new RuleDefinition(
@@ -84,62 +56,11 @@ public function getNodeTypes(): array
8456
return [Expression::class];
8557
}
8658

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
10760
{
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+
));
14465
}
14566
}

‎src/Config/Level/CodeQualityLevel.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
use Rector\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector;
4141
use Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector;
4242
use Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector;
43-
use Rector\CodeQuality\Rector\FuncCall\SetTypeToCastRector;
4443
use Rector\CodeQuality\Rector\FuncCall\SimplifyFuncGetArgsCountRector;
4544
use Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector;
4645
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
@@ -152,7 +151,6 @@ final class CodeQualityLevel
152151
ChangeArrayPushToArrayAssignRector::class,
153152
ForRepeatedCountToOwnVariableRector::class,
154153
CommonNotEqualRector::class,
155-
SetTypeToCastRector::class,
156154
LogicalToBooleanRector::class,
157155
VarToPublicPropertyRector::class,
158156
IssetOnPropertyObjectToPropertyExistsRector::class,

0 commit comments

Comments
 (0)