From 428336c22c6de654b0b5fa471971ac3a56a4d8ac Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 5 Feb 2024 16:07:43 +0000 Subject: [PATCH] Updated Rector to commit 09398c3d4f07bb6ad664a55b87cab955278a467e https://github.com/rectorphp/rector-src/commit/09398c3d4f07bb6ad664a55b87cab955278a467e Decouple AddFunctionVoidReturnTypeWhereNoReturnRector to allow leveling by simple node first (#5563) --- config/set/type-declaration.php | 3 +- .../AddVoidReturnTypeWhereNoReturnRector.php | 30 +++------ ...ctionVoidReturnTypeWhereNoReturnRector.php | 67 +++++++++++++++++++ src/Application/VersionResolver.php | 4 +- .../Levels/TypeCoverageLevel.php | 3 +- vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + 7 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php diff --git a/config/set/type-declaration.php b/config/set/type-declaration.php index eff690557fc..cd5e211bbf0 100644 --- a/config/set/type-declaration.php +++ b/config/set/type-declaration.php @@ -34,11 +34,12 @@ use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector; use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector; use Rector\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector; use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector; +use Rector\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector; use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector; use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector; use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector; use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector; use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector; return static function (RectorConfig $rectorConfig) : void { - $rectorConfig->rules([AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, AddClosureVoidReturnTypeWhereNoReturnRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnTypeFromReturnNewRector::class, AddMethodCallBasedStrictParamTypeRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, ReturnTypeFromStrictParamRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class, ReturnTypeFromStrictTernaryRector::class, BoolReturnTypeFromStrictScalarReturnsRector::class, NumericReturnTypeFromStrictScalarReturnsRector::class, StrictArrayParamDimFetchRector::class, ReturnUnionTypeRector::class, StrictStringParamConcatRector::class, MergeDateTimePropertyTypeDeclarationRector::class]); + $rectorConfig->rules([AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, AddClosureVoidReturnTypeWhereNoReturnRector::class, AddFunctionVoidReturnTypeWhereNoReturnRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnTypeFromReturnNewRector::class, AddMethodCallBasedStrictParamTypeRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, ReturnTypeFromStrictParamRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class, ReturnTypeFromStrictTernaryRector::class, BoolReturnTypeFromStrictScalarReturnsRector::class, NumericReturnTypeFromStrictScalarReturnsRector::class, StrictArrayParamDimFetchRector::class, ReturnUnionTypeRector::class, StrictStringParamConcatRector::class, MergeDateTimePropertyTypeDeclarationRector::class]); }; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php index e77306ad171..f3590c95357 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php @@ -4,10 +4,8 @@ declare (strict_types=1); namespace Rector\TypeDeclaration\Rector\ClassMethod; use PhpParser\Node; -use PhpParser\Node\Expr\Closure; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Throw_; use Rector\NodeAnalyzer\MagicClassMethodAnalyzer; use Rector\Rector\AbstractRector; @@ -79,10 +77,10 @@ CODE_SAMPLE */ public function getNodeTypes() : array { - return [ClassMethod::class, Function_::class, Closure::class]; + return [ClassMethod::class]; } /** - * @param ClassMethod|Function_|Closure $node + * @param ClassMethod $node */ public function refactor(Node $node) : ?Node { @@ -96,7 +94,7 @@ CODE_SAMPLE if (!$this->silentVoidResolver->hasExclusiveVoid($node)) { return null; } - if ($node instanceof ClassMethod && $this->classMethodReturnVendorLockResolver->isVendorLocked($node)) { + if ($this->classMethodReturnVendorLockResolver->isVendorLocked($node)) { return null; } $node->returnType = new Identifier('void'); @@ -106,32 +104,26 @@ CODE_SAMPLE { return PhpVersionFeature::VOID_TYPE; } - /** - * @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike - */ - private function shouldSkipClassMethod($functionLike) : bool + private function shouldSkipClassMethod(ClassMethod $classMethod) : bool { - if (!$functionLike instanceof ClassMethod) { - return \false; - } - if ($this->magicClassMethodAnalyzer->isUnsafeOverridden($functionLike)) { + if ($this->magicClassMethodAnalyzer->isUnsafeOverridden($classMethod)) { return \true; } - if ($functionLike->isAbstract()) { + if ($classMethod->isAbstract()) { return \true; } // is not final and has only exception? possibly implemented by child - if ($this->isNotFinalAndHasExceptionOnly($functionLike)) { + if ($this->isNotFinalAndHasExceptionOnly($classMethod)) { return \true; } // possibly required by child implementation - if ($this->isNotFinalAndEmpty($functionLike)) { + if ($this->isNotFinalAndEmpty($classMethod)) { return \true; } - if ($functionLike->isProtected()) { - return !$this->classModifierChecker->isInsideFinalClass($functionLike); + if ($classMethod->isProtected()) { + return !$this->classModifierChecker->isInsideFinalClass($classMethod); } - return $this->classModifierChecker->isInsideAbstractClass($functionLike) && $functionLike->getStmts() === []; + return $this->classModifierChecker->isInsideAbstractClass($classMethod) && $classMethod->getStmts() === []; } private function isNotFinalAndHasExceptionOnly(ClassMethod $classMethod) : bool { diff --git a/rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php b/rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php new file mode 100644 index 00000000000..19b9eb85016 --- /dev/null +++ b/rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php @@ -0,0 +1,67 @@ +silentVoidResolver = $silentVoidResolver; + } + public function getRuleDefinition() : RuleDefinition + { + return new RuleDefinition('Add function return type void if there is no return', [new CodeSample(<<<'CODE_SAMPLE' +function restore() { +} +CODE_SAMPLE +, <<<'CODE_SAMPLE' +function restore(): void { +} +CODE_SAMPLE +)]); + } + /** + * @return array> + */ + public function getNodeTypes() : array + { + return [Function_::class]; + } + /** + * @param Function_ $node + */ + public function refactor(Node $node) : ?Node + { + // already has return type → skip + if ($node->returnType instanceof Node) { + return null; + } + if (!$this->silentVoidResolver->hasExclusiveVoid($node)) { + return null; + } + $node->returnType = new Identifier('void'); + return $node; + } + public function provideMinPhpVersion() : int + { + return PhpVersionFeature::VOID_TYPE; + } +} diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 5a593c1ec22..68e43cd185d 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 = '56c250771518c2aa2e17bc64c9986c2dbe9b6fd8'; + public const PACKAGE_VERSION = '09398c3d4f07bb6ad664a55b87cab955278a467e'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-02-05 16:54:04'; + public const RELEASE_DATE = '2024-02-05 17:05:25'; /** * @var int */ diff --git a/src/Configuration/Levels/TypeCoverageLevel.php b/src/Configuration/Levels/TypeCoverageLevel.php index ac961ca4974..aaa858b578c 100644 --- a/src/Configuration/Levels/TypeCoverageLevel.php +++ b/src/Configuration/Levels/TypeCoverageLevel.php @@ -28,6 +28,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector; use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector; use Rector\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector; use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector; +use Rector\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector; use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector; use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector; use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector; @@ -54,7 +55,7 @@ final class TypeCoverageLevel // php 7.0 // start with closure first, as safest AddClosureVoidReturnTypeWhereNoReturnRector::class, - // @todo continue with functions + AddFunctionVoidReturnTypeWhereNoReturnRector::class, AddVoidReturnTypeWhereNoReturnRector::class, // php 7.4 AddArrowFunctionReturnTypeRector::class, diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 0e530dca5f4..1eaca412c12 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -2360,6 +2360,7 @@ return array( 'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => $baseDir . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php', 'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php', 'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddReturnTypeDeclarationFromYieldsRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php', + 'Rector\\TypeDeclaration\\Rector\\Function_\\AddFunctionVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php', 'Rector\\TypeDeclaration\\Rector\\Property\\AddPropertyTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php', 'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromAssignsRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php', 'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 7f94a82e9f5..a02502e7c3c 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -2574,6 +2574,7 @@ class ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3 'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php', 'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php', 'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddReturnTypeDeclarationFromYieldsRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php', + 'Rector\\TypeDeclaration\\Rector\\Function_\\AddFunctionVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php', 'Rector\\TypeDeclaration\\Rector\\Property\\AddPropertyTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php', 'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromAssignsRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php', 'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php',