From 81725974a0e68b36804dea8b96aebd0fa6dd481f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 23 May 2024 13:35:45 +0000 Subject: [PATCH] Updated Rector to commit 22d6fc2789f575fff16e76e7c7f963793e11f53b https://github.com/rectorphp/rector-src/commit/22d6fc2789f575fff16e76e7c7f963793e11f53b [TypeDeclaration] Add ReturnTypeFromSymfonySerializerRector (#5908) --- docs/rector_rules_overview.md | 25 +++- .../ReturnTypeFromSymfonySerializerRector.php | 129 ++++++++++++++++++ src/Application/VersionResolver.php | 4 +- src/Config/Level/TypeDeclarationLevel.php | 2 + vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + 6 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 382d48352cd..3cae1f3a9f5 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 376 Rules Overview +# 377 Rules Overview
@@ -60,7 +60,7 @@ - [Transform](#transform) (25) -- [TypeDeclaration](#typedeclaration) (47) +- [TypeDeclaration](#typedeclaration) (48) - [Visibility](#visibility) (3) @@ -7337,6 +7337,27 @@ Add return method return type based on strict typed property
+### ReturnTypeFromSymfonySerializerRector + +Add return type from symfony serializer + +- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromSymfonySerializerRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php) + +```diff + final class SomeClass + { + private \Symfony\Component\Serializer\Serializer $serializer; + +- public function resolveEntity($data) ++ public function resolveEntity($data): SomeType + { + return $this->serializer->deserialize($data, SomeType::class, 'json'); + } + } +``` + +
+ ### ReturnUnionTypeRector Add union return type diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php new file mode 100644 index 00000000000..b66ceed7048 --- /dev/null +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php @@ -0,0 +1,129 @@ +classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard; + $this->valueResolver = $valueResolver; + $this->argsAnalyzer = $argsAnalyzer; + } + public function getRuleDefinition() : RuleDefinition + { + return new RuleDefinition('Add return type from symfony serializer', [new CodeSample(<<<'CODE_SAMPLE' +final class SomeClass +{ + private \Symfony\Component\Serializer\Serializer $serializer; + + public function resolveEntity($data) + { + return $this->serializer->deserialize($data, SomeType::class, 'json'); + } +} +CODE_SAMPLE +, <<<'CODE_SAMPLE' +final class SomeClass +{ + private \Symfony\Component\Serializer\Serializer $serializer; + + public function resolveEntity($data): SomeType + { + return $this->serializer->deserialize($data, SomeType::class, 'json'); + } +} +CODE_SAMPLE +)]); + } + /** + * @return array> + */ + public function getNodeTypes() : array + { + return [ClassMethod::class]; + } + public function provideMinPhpVersion() : int + { + return PhpVersionFeature::HAS_RETURN_TYPE; + } + /** + * @param ClassMethod $node + */ + public function refactorWithScope(Node $node, Scope $scope) : ?Node + { + if ($node->stmts === null) { + return null; + } + if ($node->returnType instanceof Node) { + return null; + } + if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { + return null; + } + if (\count($node->stmts) !== 1) { + return null; + } + if (!$node->stmts[0] instanceof Return_ || !$node->stmts[0]->expr instanceof MethodCall) { + return null; + } + /** @var MethodCall $returnExpr */ + $returnExpr = $node->stmts[0]->expr; + if (!$this->nodeNameResolver->isName($returnExpr->name, 'deserialize')) { + return null; + } + if ($returnExpr->isFirstClassCallable()) { + return null; + } + if (!$this->isObjectType($returnExpr->var, new ObjectType('Symfony\\Component\\Serializer\\Serializer'))) { + return null; + } + $args = $returnExpr->getArgs(); + if ($this->argsAnalyzer->hasNamedArg($args)) { + return null; + } + if (\count($args) !== 3) { + return null; + } + $type = $this->valueResolver->getValue($args[1]->value); + if (!\is_string($type)) { + return null; + } + $node->returnType = new FullyQualified($type); + return $node; + } +} diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index c7500ca2fbc..73b296e9cfb 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = 'b2c9e52bf0776d2475e4d0533267cd11299826f6'; + public const PACKAGE_VERSION = '22d6fc2789f575fff16e76e7c7f963793e11f53b'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-05-22 09:12:25'; + public const RELEASE_DATE = '2024-05-23 15:32:34'; /** * @var int */ diff --git a/src/Config/Level/TypeDeclarationLevel.php b/src/Config/Level/TypeDeclarationLevel.php index 489e3baa6a2..3867a01e693 100644 --- a/src/Config/Level/TypeDeclarationLevel.php +++ b/src/Config/Level/TypeDeclarationLevel.php @@ -32,6 +32,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector; +use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromSymfonySerializerRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector; use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector; use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector; @@ -68,6 +69,7 @@ final class TypeDeclarationLevel ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromReturnNewRector::class, ReturnTypeFromReturnCastRector::class, + ReturnTypeFromSymfonySerializerRector::class, AddVoidReturnTypeWhereNoReturnRector::class, // php 7.4 EmptyOnNullableObjectToInstanceOfRector::class, diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 508941f8afb..d7b6ecbe84e 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -2408,6 +2408,7 @@ return array( 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictScalarReturnExprRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php', + 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromSymfonySerializerRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnUnionTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictArrayParamDimFetchRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictStringParamConcatRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index cf60fee0371..872d9ed0724 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -2627,6 +2627,7 @@ class ComposerStaticInit8f3085135f9c0dec79e149b0c0400440 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictScalarReturnExprRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php', + 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromSymfonySerializerRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnUnionTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictArrayParamDimFetchRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictStringParamConcatRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php',