-
-
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 TypedPropertyFromCreateMockAssignRector (#6177)
* [TypeDeclaration] Add TypedPropertyFromCreateMockAssignRector * add to type declaration set * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
b0c89a9
commit 417b208
Showing
17 changed files
with
342 additions
and
9 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
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
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
...ypeDeclaration/Rector/BooleanAnd/BinaryOpNullableToInstanceofRector/Fixture/binary_or.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector\Fixture; | ||
|
||
use Rector\Tests\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector\Source\SomeInstance; | ||
|
||
function binaryOr(?SomeInstance $someClass) | ||
{ | ||
if ($someClass || $someClass->someMethod()) { | ||
return 'yes'; | ||
} | ||
|
||
return 'no'; | ||
} | ||
|
||
?> |
37 changes: 37 additions & 0 deletions
37
...ration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/some_fixture.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,37 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source\SomeMockedClass; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
public $someMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->someMock = $this->createMock(SomeMockedClass::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source\SomeMockedClass; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
public \PHPUnit\Framework\MockObject\MockObject $someMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->someMock = $this->createMock(SomeMockedClass::class); | ||
} | ||
} | ||
|
||
?> |
39 changes: 39 additions & 0 deletions
39
...Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/with_finalized_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,39 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source\SomeFinalizedMockedClass; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source\SomeMockedClass; | ||
|
||
class WithFinalizedType extends TestCase | ||
{ | ||
public $someMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->someMock = $this->createMock(SomeFinalizedMockedClass::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source\SomeFinalizedMockedClass; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source\SomeMockedClass; | ||
|
||
class WithFinalizedType extends TestCase | ||
{ | ||
public \PHPUnit\Framework\MockObject\MockObject $someMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->someMock = $this->createMock(SomeFinalizedMockedClass::class); | ||
} | ||
} | ||
|
||
?> |
9 changes: 9 additions & 0 deletions
9
...Rector/Class_/TypedPropertyFromCreateMockAssignRector/Source/SomeFinalizedMockedClass.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); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source; | ||
|
||
final class SomeFinalizedMockedClass | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
...laration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Source/SomeMockedClass.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); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source; | ||
|
||
class SomeMockedClass | ||
{ | ||
} |
28 changes: 28 additions & 0 deletions
28
..._/TypedPropertyFromCreateMockAssignRector/TypedPropertyFromCreateMockAssignRectorTest.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\Class_\TypedPropertyFromCreateMockAssignRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class TypedPropertyFromCreateMockAssignRectorTest 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/rule_config.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...eDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/config/rule_config.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
use Rector\ValueObject\PhpVersionFeature; | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([TypedPropertyFromCreateMockAssignRector::class]) | ||
->withPhpVersion(PhpVersionFeature::TYPED_PROPERTIES); |
177 changes: 177 additions & 0 deletions
177
rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.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,177 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\NeverType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use Rector\NodeManipulator\ClassMethodPropertyFetchManipulator; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\MethodName; | ||
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\Class_\TypedPropertyFromCreateMockAssignRector\TypedPropertyFromCreateMockAssignRectorTest | ||
*/ | ||
final class TypedPropertyFromCreateMockAssignRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const TEST_CASE_CLASS = 'PHPUnit\Framework\TestCase'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject'; | ||
|
||
public function __construct( | ||
private readonly ClassMethodPropertyFetchManipulator $classMethodPropertyFetchManipulator | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Add typed property from assigned mock', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
private $someProperty; | ||
protected function setUp(): void | ||
{ | ||
$this->someProperty = $this->createMock(SomeMockedClass::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeTest extends TestCase | ||
{ | ||
private \PHPUnit\Framework\MockObject\MockObject $someProperty; | ||
protected function setUp(): void | ||
{ | ||
$this->someProperty = $this->createMock(SomeMockedClass::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isObjectType($node, new ObjectType(self::TEST_CASE_CLASS))) { | ||
return null; | ||
} | ||
|
||
$hasChanged = false; | ||
foreach ($node->getProperties() as $property) { | ||
// already typed | ||
if ($property->type instanceof Node) { | ||
continue; | ||
} | ||
|
||
$propertyName = $this->getName($property); | ||
|
||
$setUpClassMethod = $node->getMethod(MethodName::SET_UP); | ||
if (! $setUpClassMethod instanceof ClassMethod) { | ||
continue; | ||
} | ||
|
||
$assignedType = $this->resolveSingleAssignedExprType($setUpClassMethod, $propertyName); | ||
if (! $assignedType instanceof Type) { | ||
continue; | ||
} | ||
|
||
if (! $this->isMockObjectType($assignedType)) { | ||
continue; | ||
} | ||
|
||
$property->type = new FullyQualified(self::MOCK_OBJECT_CLASS); | ||
$hasChanged = true; | ||
} | ||
|
||
if (! $hasChanged) { | ||
return null; | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::TYPED_PROPERTIES; | ||
} | ||
|
||
private function isMockObjectType(Type $type): bool | ||
{ | ||
if ($type instanceof ObjectType && $type->isInstanceOf(self::MOCK_OBJECT_CLASS)->yes()) { | ||
return true; | ||
} | ||
|
||
return $this->isIntersectionWithMockObjectType($type); | ||
} | ||
|
||
private function isIntersectionWithMockObjectType(Type $type): bool | ||
{ | ||
if (! $type instanceof IntersectionType) { | ||
return false; | ||
} | ||
|
||
if (count($type->getTypes()) !== 2) { | ||
return false; | ||
} | ||
|
||
return in_array(self::MOCK_OBJECT_CLASS, $type->getObjectClassNames()); | ||
} | ||
|
||
private function resolveSingleAssignedExprType(ClassMethod $setUpClassMethod, string $propertyName): ?Type | ||
{ | ||
$assignedExprs = $this->classMethodPropertyFetchManipulator->findAssignsToPropertyName( | ||
$setUpClassMethod, | ||
$propertyName | ||
); | ||
if (count($assignedExprs) !== 1) { | ||
return null; | ||
} | ||
|
||
$assignedExpr = $assignedExprs[0]; | ||
$exprType = $this->getType($assignedExpr); | ||
|
||
// work around finalized class mock | ||
if ($exprType instanceof NeverType && $assignedExpr instanceof MethodCall && $this->isName( | ||
$assignedExpr->name, | ||
'createMock' | ||
)) { | ||
return new ObjectType(self::MOCK_OBJECT_CLASS); | ||
} | ||
|
||
return $exprType; | ||
} | ||
} |
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
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
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
Oops, something went wrong.