From 13a02cd1f00ae41ed451683a8ee9d1d392e10cd1 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 11 Oct 2026 00:53:17 +0200 Subject: [PATCH 1/2] [CodeQuality] Deprecate SetTypeToCastRector as niche rule, not part of PHP upgrade path --- .../Fixture/fixture.php.inc | 49 ---------- .../Fixture/skip_args.php.inc | 11 --- .../Fixture/skip_array_items.php.inc | 12 --- .../Fixture/skip_assign_and_return.php.inc | 20 ---- .../Fixture/skip_invalid_cast_type.php.inc | 11 --- .../SetTypeToCastRectorTest.php | 28 ------ .../config/configured_rule.php | 9 -- .../Rector/FuncCall/SetTypeToCastRector.php | 97 ++----------------- src/Config/Level/CodeQualityLevel.php | 2 - 9 files changed, 9 insertions(+), 230 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_args.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_array_items.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_assign_and_return.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_invalid_cast_type.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/SetTypeToCastRectorTest.php delete mode 100644 rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/config/configured_rule.php diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/fixture.php.inc deleted file mode 100644 index b783006b099..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,49 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_args.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_args.php.inc deleted file mode 100644 index 4f0bd9503c0..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_args.php.inc +++ /dev/null @@ -1,11 +0,0 @@ - settype($foo, 'string')]; - } -} diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_assign_and_return.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_assign_and_return.php.inc deleted file mode 100644 index 3257de09901..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/Fixture/skip_assign_and_return.php.inc +++ /dev/null @@ -1,20 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/config/configured_rule.php deleted file mode 100644 index af33d373c26..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SetTypeToCastRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([SetTypeToCastRector::class]); diff --git a/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php b/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php index e2c8215fe24..3a932239556 100644 --- a/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php @@ -5,46 +5,18 @@ namespace Rector\CodeQuality\Rector\FuncCall; use PhpParser\Node; -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; -use Rector\PhpParser\Node\Value\ValueResolver; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; /** - * @see \Rector\Tests\CodeQuality\Rector\FuncCall\SetTypeToCastRector\SetTypeToCastRectorTest + * @deprecated This rule is deprecated, as it handles a niche `settype()` use case that is not part of the PHP upgrade path. */ -final class SetTypeToCastRector extends AbstractRector +final class SetTypeToCastRector extends AbstractRector implements DeprecatedInterface { - /** - * @var array> - */ - private const array 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 __construct( - private readonly ValueResolver $valueResolver - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -84,62 +56,11 @@ public function getNodeTypes(): array return [Expression::class]; } - /** - * @param Expression $node - */ - public function refactor(Node $node): null|Expression - { - // skip expr that are not standalone line, as settype() returns success bool value - // and cannot be casted - if (! $node->expr instanceof FuncCall) { - return null; - } - - $assign = $this->refactorFuncCall($node->expr); - if (! $assign instanceof Assign) { - return null; - } - - return new Expression($assign); - } - - private function refactorFuncCall(FuncCall $funcCall): Assign|null + public function refactor(Node $node): ?Node { - if (! $this->isName($funcCall, 'setType')) { - return null; - } - - if ($funcCall->isFirstClassCallable()) { - return null; - } - - if (count($funcCall->getArgs()) < 2) { - return null; - } - - $secondArg = $funcCall->getArgs()[1]; - - $typeValue = $this->valueResolver->getValue($secondArg->value); - if (! is_string($typeValue)) { - return null; - } - - $typeValue = strtolower($typeValue); - - $firstArg = $funcCall->getArgs()[0]; - $variable = $firstArg->value; - - if (isset(self::TYPE_TO_CAST[$typeValue])) { - $castClass = self::TYPE_TO_CAST[$typeValue]; - $castNode = new $castClass($variable); - - return new Assign($variable, $castNode); - } - - if ($typeValue === 'null') { - return new Assign($variable, $this->nodeFactory->createNull()); - } - - return null; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as it handles a niche settype() use case that is not part of the PHP upgrade path', + self::class + )); } } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index 69f118a6056..79f3e963fe0 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -40,7 +40,6 @@ use Rector\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector; use Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector; use Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector; -use Rector\CodeQuality\Rector\FuncCall\SetTypeToCastRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyFuncGetArgsCountRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector; @@ -152,7 +151,6 @@ final class CodeQualityLevel ChangeArrayPushToArrayAssignRector::class, ForRepeatedCountToOwnVariableRector::class, CommonNotEqualRector::class, - SetTypeToCastRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class, IssetOnPropertyObjectToPropertyExistsRector::class, From 3b59a6a07ef186b1ca90e2b56aa502c5cf876814 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sat, 10 Oct 2026 22:54:39 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php b/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php index 3a932239556..c95361ba6dc 100644 --- a/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php @@ -56,6 +56,9 @@ public function getNodeTypes(): array return [Expression::class]; } + /** + * @param Expression $node + */ public function refactor(Node $node): ?Node { throw new ShouldNotHappenException(sprintf(