-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add ReturnTypeFromMockObjectRector
- Loading branch information
1 parent
8b72bde
commit 48c3f4f
Showing
7 changed files
with
265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...ration/Rector/ClassMethod/ReturnTypeFromMockObjectRector/Fixture/skip_filled_type.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SkipFilledType extends TestCase | ||
{ | ||
public function createMock(): MockObject | ||
{ | ||
return $this->createMock('SomeType'); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ion/Rector/ClassMethod/ReturnTypeFromMockObjectRector/Fixture/some_test_with_mock.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeTestWithMock extends TestCase | ||
{ | ||
public function createMock() | ||
{ | ||
return $this->createMock('SomeType'); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeTestWithMock extends TestCase | ||
{ | ||
public function createMock(): \PHPUnit\Framework\MockObject\MockObject | ||
{ | ||
return $this->createMock('SomeType'); | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
.../Rector/ClassMethod/ReturnTypeFromMockObjectRector/ReturnTypeFromMockObjectRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ReturnTypeFromMockObjectRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...eDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([ReturnTypeFromMockObjectRector::class]); |
153 changes: 153 additions & 0 deletions
153
rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\PhpParser\Node\BetterNodeFinder; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
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 | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const MOCK_OBJECT_CLASS = '̈́PHPUnit\Framework\MockObject\MockObject'; | ||
|
||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Add known property and return MockObject types', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$someMock = $this->createMock(SomeClass::class); | ||
return $someMock; | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeTest extends TestCase | ||
{ | ||
public function test(): \PHPUnit\Framework\MockObject\MockObject | ||
{ | ||
$someMock = $this->createMock(SomeClass::class); | ||
return $someMock; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
|
||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
// type is already known | ||
if ($node->returnType instanceof Node) { | ||
return null; | ||
} | ||
|
||
if (! $this->isInsideTestCaseClass($node)) { | ||
return null; | ||
} | ||
|
||
// we need exactly 1 return | ||
$returns = $this->betterNodeFinder->findReturnsScoped($node); | ||
if (count($returns) !== 1) { | ||
return null; | ||
} | ||
|
||
$soleReturn = $returns[0]; | ||
if (! $soleReturn->expr instanceof Expr) { | ||
return null; | ||
} | ||
|
||
$returnType = $this->getType($soleReturn->expr); | ||
if (! $this->isMockObjectType($returnType)) { | ||
return null; | ||
} | ||
|
||
$node->returnType = new FullyQualified(self::MOCK_OBJECT_CLASS); | ||
|
||
return $node; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::SCALAR_TYPES; | ||
} | ||
|
||
private function isIntersectionWithMockObjectType(Type $type): bool | ||
{ | ||
if (! $type instanceof IntersectionType) { | ||
return false; | ||
} | ||
|
||
if (count($type->getTypes()) !== 2) { | ||
return false; | ||
} | ||
|
||
return in_array(MockObject::class, $type->getObjectClassNames()); | ||
} | ||
|
||
private function isMockObjectType(Type $returnType): bool | ||
{ | ||
if ($returnType instanceof ObjectType && $returnType->isInstanceOf(self::MOCK_OBJECT_CLASS)->yes()) { | ||
return true; | ||
} | ||
|
||
return $this->isIntersectionWithMockObjectType($returnType); | ||
} | ||
|
||
private function isInsideTestCaseClass(ClassMethod $classMethod): 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters