From 7bb538470e1f15d4c8b0df70afe9771329737607 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 13 Nov 2024 15:52:17 +0100 Subject: [PATCH] [TypeDeclaration] Skip test methods with exception in ReturnNeverTypeRector --- .../Fixture/skip_checked.php.inc | 13 ++++++++++ .../Fixture/skip_test_method.php.inc | 16 ++++++++++++ .../ClassMethod/ReturnNeverTypeRector.php | 26 ++++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_checked.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector/Fixture/skip_test_method.php.inc diff --git a/rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_checked.php.inc b/rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_checked.php.inc new file mode 100644 index 00000000000..8e0634dc174 --- /dev/null +++ b/rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_checked.php.inc @@ -0,0 +1,13 @@ +name = $name; + } +} diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector/Fixture/skip_test_method.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector/Fixture/skip_test_method.php.inc new file mode 100644 index 00000000000..3ef0281fc02 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector/Fixture/skip_test_method.php.inc @@ -0,0 +1,16 @@ +expectException(Exception::class); + + throw new \InvalidArgumentException(); + } +} diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php index 5208c7e5fd3..676cfa4ef88 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php @@ -8,6 +8,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use Rector\PHPStan\ScopeFetcher; +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddNeverReturnType; use Rector\ValueObject\PhpVersionFeature; @@ -21,7 +22,8 @@ final class ReturnNeverTypeRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( - private readonly AddNeverReturnType $addNeverReturnType + private readonly AddNeverReturnType $addNeverReturnType, + private readonly TestsNodeAnalyzer $testsNodeAnalyzer ) { } @@ -67,6 +69,11 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { $scope = ScopeFetcher::fetch($node); + + if ($this->isTestClassMethodWithFilledReturnType($node)) { + return null; + } + return $this->addNeverReturnType->add($node, $scope); } @@ -74,4 +81,21 @@ public function provideMinPhpVersion(): int { return PhpVersionFeature::NEVER_TYPE; } + + private function isTestClassMethodWithFilledReturnType(ClassMethod|Function_ $callLike): bool + { + if (! $callLike instanceof ClassMethod) { + return false; + } + + if (! $callLike->isPublic()) { + return false; + } + + if (! $this->testsNodeAnalyzer->isInTestClass($callLike)) { + return false; + } + + return $callLike->returnType instanceof Node; + } }