-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add SingleMockPropertyTypeRector (#401)
- Loading branch information
1 parent
e54d4ad
commit eb0b69a
Showing
9 changed files
with
213 additions
and
1 deletion.
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
12 changes: 12 additions & 0 deletions
12
...s/CodeQuality/Rector/Class_/SingleMockPropertyTypeRector/Fixture/skip_native_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,12 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Source\AnyBook; | ||
|
||
final class SkipNativeType extends TestCase | ||
{ | ||
private AnyBook $anyBook; | ||
} |
12 changes: 12 additions & 0 deletions
12
...sts/CodeQuality/Rector/Class_/SingleMockPropertyTypeRector/Fixture/skip_sole_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,12 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Source\AnyBook; | ||
|
||
class SkipSoleType extends TestCase | ||
{ | ||
private MockObject $anyBook; | ||
} |
29 changes: 29 additions & 0 deletions
29
...ts/CodeQuality/Rector/Class_/SingleMockPropertyTypeRector/Fixture/union_type_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,29 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Source\AnyBook; | ||
|
||
class UnionTypeMock extends TestCase | ||
{ | ||
private MockObject|AnyBook $anyBook; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Source\AnyBook; | ||
|
||
class UnionTypeMock extends TestCase | ||
{ | ||
private MockObject $anyBook; | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...deQuality/Rector/Class_/SingleMockPropertyTypeRector/SingleMockPropertyTypeRectorTest.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\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SingleMockPropertyTypeRectorTest 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'; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
rules-tests/CodeQuality/Rector/Class_/SingleMockPropertyTypeRector/Source/AnyBook.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,8 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Source; | ||
|
||
class AnyBook | ||
{ | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...s-tests/CodeQuality/Rector/Class_/SingleMockPropertyTypeRector/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\PHPUnit\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([SingleMockPropertyTypeRector::class]); |
112 changes: 112 additions & 0 deletions
112
rules/CodeQuality/Rector/Class_/SingleMockPropertyTypeRector.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,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\CodeQuality\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\IntersectionType; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\UnionType; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\SingleMockPropertyTypeRectorTest | ||
*/ | ||
final class SingleMockPropertyTypeRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Make properties in tests with intersection mock object either object type or mock type', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
final class MockingEntity extends TestCase | ||
{ | ||
private SimpleObject|MockObject $someEntityMock; | ||
protected function setUp(): void | ||
{ | ||
$this->someEntityMock = $this->createMock(SimpleObject::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
final class MockingEntity extends TestCase | ||
{ | ||
private MockObject $someEntityMock; | ||
protected function setUp(): void | ||
{ | ||
$this->someEntityMock = $this->createMock(SimpleObject::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Class_ | ||
{ | ||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||
return null; | ||
} | ||
|
||
$hasChanged = false; | ||
|
||
foreach ($node->getProperties() as $property) { | ||
if (! $property->type instanceof IntersectionType && ! $property->type instanceof UnionType) { | ||
continue; | ||
} | ||
|
||
$complexType = $property->type; | ||
if (count($complexType->types) !== 2) { | ||
continue; | ||
} | ||
|
||
foreach ($complexType->types as $intersectionType) { | ||
if ($this->isName($intersectionType, MockObject::class)) { | ||
$property->type = $intersectionType; | ||
$hasChanged = true; | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
if (! $hasChanged) { | ||
return null; | ||
} | ||
|
||
return $node; | ||
} | ||
} |