Skip to content

Commit

Permalink
[TypeDeclaration] Add missing usage of ClassMethodReturnTypeOverrideG…
Browse files Browse the repository at this point in the history
…uard on ReturnTypeFromMockObjectRector (#6169)

* [TypeDeclaration] Add missing usage of ClassMethodReturnTypeOverrideGuard on ReturnTypeFromMockObjectRector

* [TypeDeclaration] Add missing usage of ClassMethodReturnTypeOverrideGuard on ReturnTypeFromMockObjectRector

* Fix fixture

* test
  • Loading branch information
samsonasik committed Jul 21, 2024
1 parent 1f7cc28 commit 623bbf6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use PHPUnit\Framework\TestCase;

final class SkipFilledType extends TestCase
{
public function createMock(): MockObject
public function test(): MockObject
{
return $this->createMock('SomeType');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use PHPUnit\Framework\TestCase;

final class SomeTestWithMock extends TestCase
{
public function createMock()
public function test()
{
return $this->createMock('SomeType');
}
Expand All @@ -26,7 +26,7 @@ use PHPUnit\Framework\TestCase;

final class SomeTestWithMock extends TestCase
{
public function createMock(): \PHPUnit\Framework\MockObject\MockObject
public function test(): \PHPUnit\Framework\MockObject\MockObject
{
return $this->createMock('SomeType');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,32 @@
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPUnit\Framework\TestCase;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\ReturnTypeFromMockObjectRectorTest
*/
final class ReturnTypeFromMockObjectRector extends AbstractRector implements MinPhpVersionInterface
final class ReturnTypeFromMockObjectRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
/**
* @var string
*/
private const TESTCASE_CLASS = 'PHPUnit\Framework\TestCase';

/**
* @var string
*/
private const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject';

public function __construct(
private readonly BetterNodeFinder $betterNodeFinder
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard
) {
}

Expand Down Expand Up @@ -75,14 +80,18 @@ public function getNodeTypes(): array
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
// type is already known
if ($node->returnType instanceof Node) {
return null;
}

if (! $this->isInsideTestCaseClass($node)) {
if (! $this->isInsideTestCaseClass($scope)) {
return null;
}

if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
return null;
}

Expand Down Expand Up @@ -134,19 +143,14 @@ private function isMockObjectType(Type $returnType): bool
return $this->isIntersectionWithMockObjectType($returnType);
}

private function isInsideTestCaseClass(ClassMethod $classMethod): bool
private function isInsideTestCaseClass(Scope $scope): bool
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

// is phpunit test case?
return $classReflection->isSubclassOf(TestCase::class);
return $classReflection->isSubclassOf(self::TESTCASE_CLASS);
}
}

0 comments on commit 623bbf6

Please sign in to comment.