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..c95361ba6dc 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( @@ -87,59 +59,11 @@ public function getNodeTypes(): array /** * @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,