Skip to content

Commit

Permalink
[TypeDeclaration] Skip test methods with exception in ReturnNeverType…
Browse files Browse the repository at this point in the history
…Rector
  • Loading branch information
TomasVotruba committed Nov 13, 2024
1 parent db1a84c commit 7bb5384
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector\Fixture;

final class SkipChecked
{
private ?string $name;

public function __construct(?string $name)
{
$this->name = $name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

use Exception;
use PHPUnit\Framework\TestCase;

final class SkipTestMethod extends TestCase
{
public function testSomething(): void
{
$this->expectException(Exception::class);

throw new \InvalidArgumentException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -67,11 +69,33 @@ 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);
}

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;
}
}

0 comments on commit 7bb5384

Please sign in to comment.