diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index f9aeae86bfb..382d48352cd 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 374 Rules Overview +# 376 Rules Overview
@@ -6,7 +6,7 @@ - [Arguments](#arguments) (4) -- [Carbon](#carbon) (3) +- [Carbon](#carbon) (4) - [CodeQuality](#codequality) (75) @@ -60,7 +60,7 @@ - [Transform](#transform) (25) -- [TypeDeclaration](#typedeclaration) (46) +- [TypeDeclaration](#typedeclaration) (47) - [Visibility](#visibility) (3) @@ -148,7 +148,7 @@ Replaces defined map of arguments in defined methods and their calls. ### DateFuncCallToCarbonRector -Convert `date()` function call to Carbon::*() +Convert `date()` function call to `Carbon::now()->format(*)` - class: [`Rector\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector`](../rules/Carbon/Rector/FuncCall/DateFuncCallToCarbonRector.php) @@ -158,7 +158,7 @@ Convert `date()` function call to Carbon::*() public function run() { - $date = date('Y-m-d'); -+ $date = \Carbon\Carbon::now()->format('Y-m-d') ++ $date = \Carbon\Carbon::now()->format('Y-m-d'); } } ``` @@ -197,6 +197,25 @@ Convert new `DateTime()` with a method call to Carbon::*()
+### TimeFuncCallToCarbonRector + +Convert `time()` function call to `Carbon::now()->timestamp` + +- class: [`Rector\Carbon\Rector\FuncCall\TimeFuncCallToCarbonRector`](../rules/Carbon/Rector/FuncCall/TimeFuncCallToCarbonRector.php) + +```diff + class SomeClass + { + public function run() + { +- $time = time(); ++ $time = \Carbon\Carbon::now()->timestamp; + } + } +``` + +
+ ## CodeQuality ### AbsolutizeRequireAndIncludePathRector @@ -7043,6 +7062,30 @@ Add "never" return-type for methods that never return anything
+### ReturnTypeFromReturnCastRector + +Add return type to function like with return cast + +- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnCastRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php) + +```diff + final class SomeClass + { +- public function action($param) ++ public function action($param): array + { + try { + return (array) $param; + } catch (Exception $exception) { + // some logging + throw $exception; + } + } + } +``` + +
+ ### ReturnTypeFromReturnDirectArrayRector Add return type from return direct array diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php new file mode 100644 index 00000000000..714cba8ed20 --- /dev/null +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php @@ -0,0 +1,127 @@ +classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard; + $this->returnTypeInferer = $returnTypeInferer; + $this->betterNodeFinder = $betterNodeFinder; + $this->staticTypeMapper = $staticTypeMapper; + } + public function getRuleDefinition() : RuleDefinition + { + return new RuleDefinition('Add return type to function like with return cast', [new CodeSample(<<<'CODE_SAMPLE' +final class SomeClass +{ + public function action($param) + { + try { + return (array) $param; + } catch (Exception $exception) { + // some logging + throw $exception; + } + } +} +CODE_SAMPLE +, <<<'CODE_SAMPLE' +final class SomeClass +{ + public function action($param): array + { + try { + return (array) $param; + } catch (Exception $exception) { + // some logging + throw $exception; + } + } +} +CODE_SAMPLE +)]); + } + /** + * @return array> + */ + public function getNodeTypes() : array + { + return [ClassMethod::class, Function_::class, Closure::class]; + } + /** + * @param ClassMethod|Function_|Closure $node + */ + public function refactorWithScope(Node $node, Scope $scope) : ?Node + { + if ($node->returnType !== null) { + return null; + } + if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { + return null; + } + $hasNonCastReturn = (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped($node, static function (Node $subNode) : bool { + return $subNode instanceof Return_ && !$subNode->expr instanceof Cast; + }); + if ($hasNonCastReturn) { + return null; + } + $returnType = $this->returnTypeInferer->inferFunctionLike($node); + if ($returnType instanceof UnionType || $returnType->isVoid()->yes()) { + return null; + } + $returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN); + if (!$returnTypeNode instanceof Node) { + return null; + } + $node->returnType = $returnTypeNode; + return $node; + } + public function provideMinPhpVersion() : int + { + return PhpVersionFeature::SCALAR_TYPES; + } +} diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 0992757fcb1..86ff1b6c7de 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 = 'd1eb62e703651ec14712439700d6e9b2a60c0763'; + public const PACKAGE_VERSION = 'b8847be9d485ec1b3cde5ee5e099ab3626479eb7'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-05-20 08:46:14'; + public const RELEASE_DATE = '2024-05-20 20:32:55'; /** * @var int */ diff --git a/src/Config/Level/TypeDeclarationLevel.php b/src/Config/Level/TypeDeclarationLevel.php index 2354f05810b..489e3baa6a2 100644 --- a/src/Config/Level/TypeDeclarationLevel.php +++ b/src/Config/Level/TypeDeclarationLevel.php @@ -20,6 +20,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarR use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector; use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector; +use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnCastRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector; @@ -66,6 +67,7 @@ final class TypeDeclarationLevel ReturnTypeFromStrictScalarReturnExprRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromReturnNewRector::class, + ReturnTypeFromReturnCastRector::class, AddVoidReturnTypeWhereNoReturnRector::class, // php 7.4 EmptyOnNullableObjectToInstanceOfRector::class, diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 3fcc2a0ec7d..508941f8afb 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -2396,6 +2396,7 @@ return array( 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php', + 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnCastRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnDirectArrayRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictBoolReturnExprRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index b2fa423f107..cf60fee0371 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -2615,6 +2615,7 @@ class ComposerStaticInit8f3085135f9c0dec79e149b0c0400440 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php', + 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnCastRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnDirectArrayRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictBoolReturnExprRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php',