diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index c31c7dfcfbf..0a871ff0f8e 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -42,7 +42,7 @@ - [Php73](#php73) (9) -- [Php74](#php74) (14) +- [Php74](#php74) (13) - [Php80](#php80) (20) @@ -5345,33 +5345,6 @@ Change `array_merge()` to spread operator
-### ChangeReflectionTypeToStringToGetNameRector - -Change string calls on ReflectionType - -- class: [`Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector`](../rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php) - -```diff - class SomeClass - { - public function go(ReflectionFunction $reflectionFunction) - { - $parameterReflection = $reflectionFunction->getParameters()[0]; - -- $paramType = (string) $parameterReflection->getType(); -+ $paramType = (string) ($parameterReflection->getType() ? $parameterReflection->getType()->getName() : null); - -- $stringValue = 'hey' . $reflectionFunction->getReturnType(); -+ $stringValue = 'hey' . ($reflectionFunction->getReturnType() ? $reflectionFunction->getReturnType()->getName() : null); - - // keep - return $reflectionFunction->getReturnType(); - } - } -``` - -
- ### ClosureToArrowFunctionRector Change closure to arrow function diff --git a/config/set/php74.php b/config/set/php74.php index 0e73f14746e..76a7b89d1b7 100644 --- a/config/set/php74.php +++ b/config/set/php74.php @@ -13,7 +13,6 @@ use Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector; use Rector\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector; use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector; -use Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector; use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector; use Rector\Php74\Rector\StaticCall\ExportToReflectionFunctionRector; use Rector\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector; @@ -42,7 +41,6 @@ ClosureToArrowFunctionRector::class, ArraySpreadInsteadOfArrayMergeRector::class, AddLiteralSeparatorToNumberRector::class, - ChangeReflectionTypeToStringToGetNameRector::class, RestoreDefaultNullToNullableTypePropertyRector::class, CurlyToSquareBracketArrayStringRector::class, MoneyFormatToNumberFormatRector::class, diff --git a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/ChangeReflectionTypeToStringToGetNameRectorTest.php b/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/ChangeReflectionTypeToStringToGetNameRectorTest.php deleted file mode 100644 index a8c03d1d552..00000000000 --- a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/ChangeReflectionTypeToStringToGetNameRectorTest.php +++ /dev/null @@ -1,28 +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/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/fixture.php.inc b/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/fixture.php.inc deleted file mode 100644 index 1e05913fa60..00000000000 --- a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,35 +0,0 @@ -getParameters()[0]; - - $paramType = (string) $parameterReflection->getType(); - - $stringValue = 'hey' . $reflectionFunction->getReturnType(); - } -} - -?> ------ -getParameters()[0]; - - $paramType = (string) ($parameterReflection->getType() ? $parameterReflection->getType()->getName() : null); - - $stringValue = 'hey' . ($reflectionFunction->getReturnType() ? $reflectionFunction->getReturnType()->getName() : null); - } -} - -?> diff --git a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/keep_returned_value.php.inc b/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/keep_returned_value.php.inc deleted file mode 100644 index bb595454fe8..00000000000 --- a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/keep_returned_value.php.inc +++ /dev/null @@ -1,11 +0,0 @@ -getReturnType(); - } -} diff --git a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/known_has_type.php.inc b/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/known_has_type.php.inc deleted file mode 100644 index bf7397a6028..00000000000 --- a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/known_has_type.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -hasReturnType()) { - return (string) $reflectionMethod->getReturnType(); - } - } -} - -?> ------ -hasReturnType()) { - return $reflectionMethod->getReturnType()->getName(); - } - } -} - -?> diff --git a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/parameter_type.php.inc b/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/parameter_type.php.inc deleted file mode 100644 index 1ff95af3ee6..00000000000 --- a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/parameter_type.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ -getName(); - } -} - -?> diff --git a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/skip_cast_string_on_reflection_type.php.inc b/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/skip_cast_string_on_reflection_type.php.inc deleted file mode 100644 index ce4ef023319..00000000000 --- a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/skip_cast_string_on_reflection_type.php.inc +++ /dev/null @@ -1,20 +0,0 @@ -getReturnType(); - - return $returnType instanceof ReflectionNamedType - ? $returnType->getName() - : (string) $returnType; - } -} - -?> diff --git a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/skip_non_to_string.php.inc b/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/skip_non_to_string.php.inc deleted file mode 100644 index fc555ff7f8a..00000000000 --- a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture/skip_non_to_string.php.inc +++ /dev/null @@ -1,18 +0,0 @@ -getType(); - if ($parameterType !== null) { - /** @var ReflectionType $parameterType */ - $parameterType = $parameterType->getName(); - } - } -} diff --git a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/config/configured_rule.php b/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/config/configured_rule.php deleted file mode 100644 index fb6196fc48d..00000000000 --- a/rules-tests/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(ChangeReflectionTypeToStringToGetNameRector::class); -}; diff --git a/rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php b/rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php deleted file mode 100644 index e83583e855a..00000000000 --- a/rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php +++ /dev/null @@ -1,250 +0,0 @@ -getParameters()[0]; - - $paramType = (string) $parameterReflection->getType(); - - $stringValue = 'hey' . $reflectionFunction->getReturnType(); - - // keep - return $reflectionFunction->getReturnType(); - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -class SomeClass -{ - public function go(ReflectionFunction $reflectionFunction) - { - $parameterReflection = $reflectionFunction->getParameters()[0]; - - $paramType = (string) ($parameterReflection->getType() ? $parameterReflection->getType()->getName() : null); - - $stringValue = 'hey' . ($reflectionFunction->getReturnType() ? $reflectionFunction->getReturnType()->getName() : null); - - // keep - return $reflectionFunction->getReturnType(); - } -} -CODE_SAMPLE - ), - ]); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [MethodCall::class, String_::class]; - } - - /** - * @param MethodCall|String_ $node - */ - public function refactorWithScope(Node $node, Scope $scope): ?Node - { - if ($node instanceof MethodCall) { - return $this->refactorMethodCall($node); - } - - if ($node->expr instanceof MethodCall) { - return $this->refactorIfHasReturnTypeWasCalled($node->expr); - } - - if (! $node->expr instanceof Variable) { - return null; - } - - if (! $this->isObjectType($node->expr, new ObjectType('ReflectionType'))) { - return null; - } - - $type = $this->nodeTypeResolver->getType($node->expr); - if (! $type instanceof UnionType) { - return $this->nodeFactory->createMethodCall($node->expr, self::GET_NAME); - } - - if (! $this->isWithReflectionType($type)) { - return $this->nodeFactory->createMethodCall($node->expr, self::GET_NAME); - } - - return null; - } - - private function isWithReflectionType(UnionType $unionType): bool - { - foreach ($unionType->getTypes() as $type) { - if (! $type instanceof ObjectType) { - continue; - } - - if ($type->getClassName() !== 'ReflectionType') { - continue; - } - - return true; - } - - return false; - } - - private function refactorMethodCall(MethodCall $methodCall): ?Node - { - $this->collectCallByVariable($methodCall); - - if ($this->shouldSkipMethodCall($methodCall)) { - return null; - } - - if ($this->isReflectionParameterGetTypeMethodCall($methodCall)) { - return $this->refactorReflectionParameterGetName($methodCall); - } - - if ($this->isReflectionFunctionAbstractGetReturnTypeMethodCall($methodCall)) { - return $this->refactorReflectionFunctionGetReturnType($methodCall); - } - - return null; - } - - private function refactorIfHasReturnTypeWasCalled(MethodCall $methodCall): ?Node - { - if (! $methodCall->var instanceof Variable) { - return null; - } - - $variableName = $this->getName($methodCall->var); - - $callsByVariable = $this->callsByVariable[$variableName] ?? []; - - // we already know it has return type - if (in_array('hasReturnType', $callsByVariable, true)) { - return $this->nodeFactory->createMethodCall($methodCall, self::GET_NAME); - } - - return null; - } - - private function collectCallByVariable(MethodCall $methodCall): void - { - // bit workaround for now - if ($methodCall->var instanceof Variable) { - $variableName = $this->getName($methodCall->var); - $methodName = $this->getName($methodCall->name); - if (! is_string($variableName)) { - return; - } - - if (! is_string($methodName)) { - return; - } - - $this->callsByVariable[$variableName][] = $methodName; - } - } - - private function shouldSkipMethodCall(MethodCall $methodCall): bool - { - // is to string retype? - $parentNode = $methodCall->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof String_) { - return false; - } - - // probably already converted - return ! $parentNode instanceof Concat; - } - - private function isReflectionParameterGetTypeMethodCall(MethodCall $methodCall): bool - { - if (! $this->isName($methodCall->name, 'getType')) { - return false; - } - - return $this->isObjectType($methodCall->var, new ObjectType('ReflectionParameter')); - } - - private function refactorReflectionParameterGetName(MethodCall $methodCall): Ternary - { - $getNameMethodCall = $this->nodeFactory->createMethodCall($methodCall, self::GET_NAME); - - return new Ternary($methodCall, $getNameMethodCall, $this->nodeFactory->createNull()); - } - - private function isReflectionFunctionAbstractGetReturnTypeMethodCall(MethodCall $methodCall): bool - { - if (! $this->isName($methodCall->name, 'getReturnType')) { - return false; - } - - return $this->isObjectType($methodCall->var, new ObjectType('ReflectionFunctionAbstract')); - } - - private function refactorReflectionFunctionGetReturnType(MethodCall $methodCall): Node | Ternary - { - $refactoredMethodCall = $this->refactorIfHasReturnTypeWasCalled($methodCall); - if ($refactoredMethodCall instanceof Node) { - return $refactoredMethodCall; - } - - $getNameMethodCall = $this->nodeFactory->createMethodCall($methodCall, self::GET_NAME); - return new Ternary($methodCall, $getNameMethodCall, $this->nodeFactory->createNull()); - } -}