From f863e8b003b4ac6feffc8a20387bb301f8a5a675 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 12 Feb 2024 22:55:41 +0000 Subject: [PATCH] Updated Rector to commit 7de77e518f7b47b7c7ba0d96152d4acdad958639 https://github.com/rectorphp/rector-src/commit/7de77e518f7b47b7c7ba0d96152d4acdad958639 [TypeDeclaration] Add AddTestsVoidReturnTypeWhereNoReturnRector (#5611) --- docs/rector_rules_overview.md | 24 ++++- ...TestsVoidReturnTypeWhereNoReturnRector.php | 102 ++++++++++++++++++ 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, 130 insertions(+), 4 deletions(-) create mode 100644 rules/TypeDeclaration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector.php diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 940a75a9a0d..86dffc62510 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 358 Rules Overview +# 359 Rules Overview
@@ -56,7 +56,7 @@ - [Transform](#transform) (23) -- [TypeDeclaration](#typedeclaration) (42) +- [TypeDeclaration](#typedeclaration) (43) - [Visibility](#visibility) (3) @@ -6495,6 +6495,26 @@ Changes defined return typehint of method and class.
+### AddTestsVoidReturnTypeWhereNoReturnRector + +Add void to PHPUnit test methods + +- class: [`Rector\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector`](../rules/TypeDeclaration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector.php) + +```diff + use PHPUnit\Framework\TestCase; + + class SomeClass extends TestCase + { +- public function testSomething() ++ public function testSomething(): void + { + } + } +``` + +
+ ### AddVoidReturnTypeWhereNoReturnRector Add return type void to function like without any return diff --git a/rules/TypeDeclaration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector.php b/rules/TypeDeclaration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector.php new file mode 100644 index 00000000000..a91ba1eefde --- /dev/null +++ b/rules/TypeDeclaration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector.php @@ -0,0 +1,102 @@ +testsNodeAnalyzer = $testsNodeAnalyzer; + $this->silentVoidResolver = $silentVoidResolver; + } + public function getRuleDefinition() : RuleDefinition + { + return new RuleDefinition('Add void to PHPUnit test methods', [new CodeSample(<<<'CODE_SAMPLE' +use PHPUnit\Framework\TestCase; + +class SomeClass extends TestCase +{ + public function testSomething() + { + } +} +CODE_SAMPLE +, <<<'CODE_SAMPLE' +use PHPUnit\Framework\TestCase; + +class SomeClass extends TestCase +{ + public function testSomething(): void + { + } +} +CODE_SAMPLE +)]); + } + /** + * @return array> + */ + public function getNodeTypes() : array + { + return [Class_::class]; + } + /** + * @param Class_ $node + */ + public function refactor(Node $node) : ?Node + { + if (!$this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + $hasChanged = \false; + foreach ($node->getMethods() as $classMethod) { + // has type already + if ($classMethod->returnType instanceof Node) { + continue; + } + if (!$this->testsNodeAnalyzer->isTestClassMethod($classMethod)) { + continue; + } + if ($classMethod->isAbstract()) { + continue; + } + if (!$this->silentVoidResolver->hasExclusiveVoid($classMethod)) { + continue; + } + $classMethod->returnType = new Identifier('void'); + $hasChanged = \true; + } + if ($hasChanged) { + return $node; + } + return null; + } + public function provideMinPhpVersion() : int + { + return PhpVersionFeature::VOID_TYPE; + } +} diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 73a120be335..8810d7072cb 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 = '48e8cfab19f5572f6419876a8f5cec0109966862'; + public const PACKAGE_VERSION = '7de77e518f7b47b7c7ba0d96152d4acdad958639'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-02-12 22:57:00'; + public const RELEASE_DATE = '2024-02-12 22:53:21'; /** * @var int */ diff --git a/src/Config/Level/TypeDeclarationLevel.php b/src/Config/Level/TypeDeclarationLevel.php index 732202e31a1..87172d6303b 100644 --- a/src/Config/Level/TypeDeclarationLevel.php +++ b/src/Config/Level/TypeDeclarationLevel.php @@ -5,6 +5,7 @@ namespace Rector\Config\Level; use Rector\Contract\Rector\RectorInterface; use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector; +use Rector\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector; use Rector\TypeDeclaration\Rector\Class_\MergeDateTimePropertyTypeDeclarationRector; use Rector\TypeDeclaration\Rector\Class_\PropertyTypeFromStrictSetterGetterRector; use Rector\TypeDeclaration\Rector\Class_\ReturnTypeFromStrictTernaryRector; @@ -53,6 +54,7 @@ final class TypeDeclarationLevel // start with closure first, as safest AddClosureVoidReturnTypeWhereNoReturnRector::class, AddFunctionVoidReturnTypeWhereNoReturnRector::class, + AddTestsVoidReturnTypeWhereNoReturnRector::class, AddVoidReturnTypeWhereNoReturnRector::class, // php 7.4 AddArrowFunctionReturnTypeRector::class, diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 607e680b78f..937c9305da2 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -2375,6 +2375,7 @@ return array( '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', + 'Rector\\TypeDeclaration\\Rector\\Class_\\AddTestsVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\MergeDateTimePropertyTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/Class_/MergeDateTimePropertyTypeDeclarationRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\PropertyTypeFromStrictSetterGetterRector' => $baseDir . '/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\ReturnTypeFromStrictTernaryRector' => $baseDir . '/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 021a4a0b274..5d04219898e 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -2594,6 +2594,7 @@ class ComposerStaticInit2d887a2f87c676eb32b3e04612865e54 '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', + 'Rector\\TypeDeclaration\\Rector\\Class_\\AddTestsVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\MergeDateTimePropertyTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Class_/MergeDateTimePropertyTypeDeclarationRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\PropertyTypeFromStrictSetterGetterRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\ReturnTypeFromStrictTernaryRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php',