Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromMockObjectRector\Fixture;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromMockObjectRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromMockObjectRector\Fixture;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromMockObjectRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand All @@ -20,7 +20,7 @@ final class SomeTestWithMock extends TestCase

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromMockObjectRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromMockObjectRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector;
use Rector\TypeDeclaration\Rector\Class_\ReturnTypeFromMockObjectRector;

return RectorConfig::configure()
->withRules([ReturnTypeFromMockObjectRector::class]);
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

declare(strict_types=1);

namespace Rector\TypeDeclaration\Rector\ClassMethod;
namespace Rector\TypeDeclaration\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
Expand All @@ -25,7 +24,7 @@
use Rector\VersionBonding\Contract\MinPhpVersionInterface;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\ReturnTypeFromMockObjectRectorTest
* @see \Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromMockObjectRector\ReturnTypeFromMockObjectRectorTest
*/
final class ReturnTypeFromMockObjectRector extends AbstractRector implements MinPhpVersionInterface
{
Expand Down Expand Up @@ -68,49 +67,65 @@ public function createSomeMock(): \PHPUnit\Framework\MockObject\MockObject

public function getNodeTypes(): array
{
return [ClassMethod::class];
return [Class_::class];
}

/**
* @param ClassMethod $node
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
// type is already known
if ($node->returnType instanceof Node) {
$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}

// we need exactly 1 return
$returns = $this->betterNodeFinder->findReturnsScoped($node);
if (count($returns) !== 1) {
// is phpunit test case?
if (! $classReflection->is(ClassName::TEST_CASE_CLASS)) {
return null;
}

if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) {
return null;
}
$hasChanged = false;
$scope = null;

$scope = ScopeFetcher::fetch($node);
if (! $this->isInsideTestCaseClass($scope)) {
return null;
}
foreach ($node->getMethods() as $classMethod) {
// type is already known
if ($classMethod->returnType instanceof Node) {
continue;
}

if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
return null;
}
// we need exactly 1 return
$returns = $this->betterNodeFinder->findReturnsScoped($classMethod);
if (count($returns) !== 1) {
continue;
}

/** @var Expr $expr */
$expr = $returns[0]->expr;
$returnType = $this->nodeTypeResolver->getNativeType($expr);
if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($classMethod, $returns)) {
continue;
}

if (! $this->isMockObjectType($returnType)) {
return null;
$scope ??= ScopeFetcher::fetch($node);
if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($classMethod, $scope)) {
continue;
}

/** @var Expr $expr */
$expr = $returns[0]->expr;
$returnType = $this->nodeTypeResolver->getNativeType($expr);

if (! $this->isMockObjectType($returnType)) {
continue;
}

$classMethod->returnType = new FullyQualified(ClassName::MOCK_OBJECT);
$hasChanged = true;
}

$node->returnType = new FullyQualified(ClassName::MOCK_OBJECT);
if ($hasChanged) {
return $node;
}

return $node;
return null;
}

public function provideMinPhpVersion(): int
Expand Down Expand Up @@ -139,15 +154,4 @@ private function isMockObjectType(Type $returnType): bool

return $this->isIntersectionWithMockObjectType($returnType);
}

private function isInsideTestCaseClass(Scope $scope): bool
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

// is phpunit test case?
return $classReflection->is(ClassName::TEST_CASE_CLASS);
}
}
2 changes: 1 addition & 1 deletion src/Config/Level/TypeDeclarationLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\TypeDeclaration\Rector\Class_\MergeDateTimePropertyTypeDeclarationRector;
use Rector\TypeDeclaration\Rector\Class_\ObjectTypedPropertyFromJMSSerializerAttributeTypeRector;
use Rector\TypeDeclaration\Rector\Class_\PropertyTypeFromStrictSetterGetterRector;
use Rector\TypeDeclaration\Rector\Class_\ReturnTypeFromMockObjectRector;
use Rector\TypeDeclaration\Rector\Class_\ReturnTypeFromStrictTernaryRector;
use Rector\TypeDeclaration\Rector\Class_\ScalarTypedPropertyFromJMSSerializerAttributeTypeRector;
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromContainerGetSetUpRector;
Expand All @@ -38,7 +39,6 @@
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNullableTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromGetRepositoryDocblockRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnCastRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
Expand Down
Loading