-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RemoveTypedPropertyNonMockDocblockRector
- Loading branch information
1 parent
d553e84
commit c6f510e
Showing
12 changed files
with
300 additions
and
14 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
32 changes: 32 additions & 0 deletions
32
...eadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector/Fixture/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,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\Source\RealType; | ||
|
||
final class SomeTest extends TestCase | ||
{ | ||
/** | ||
* @var RealType|MockObject | ||
*/ | ||
private RealType $someProperty; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\Source\RealType; | ||
|
||
final class SomeTest extends TestCase | ||
{ | ||
private RealType $someProperty; | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...ssLike/RemoveTypedPropertyNonMockDocblockRector/Fixture/skip_missing_propert_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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\Source\RealType; | ||
|
||
final class SkipMissingPropertyType extends TestCase | ||
{ | ||
/** | ||
* @var RealType|MockObject | ||
*/ | ||
private $someProperty; | ||
} |
16 changes: 16 additions & 0 deletions
16
...or/ClassLike/RemoveTypedPropertyNonMockDocblockRector/Fixture/skip_non_mock_union.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 | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Rector\Tests\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\Source\stdClass; | ||
use Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\Source\RealType; | ||
|
||
final class SkipNonMockUnion extends TestCase | ||
{ | ||
/** | ||
* @var RealType|stdClass | ||
*/ | ||
private RealType $someProperty; | ||
} |
28 changes: 28 additions & 0 deletions
28
...RemoveTypedPropertyNonMockDocblockRector/RemoveTypedPropertyNonMockDocblockRectorTest.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\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveTypedPropertyNonMockDocblockRectorTest 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
...ts/DeadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector/Source/RealType.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\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\Source; | ||
|
||
final class RealType | ||
{ | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...Code/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector/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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveTypedPropertyNonMockDocblockRector::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
153 changes: 153 additions & 0 deletions
153
rules/DeadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector.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\DeadCode\Rector\ClassLike; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\Property; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\UnionType; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover; | ||
use Rector\Enum\ClassName; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\StaticTypeMapper\StaticTypeMapper; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\RemoveTypedPropertyNonMockDocblockRectorTest | ||
*/ | ||
final class RemoveTypedPropertyNonMockDocblockRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject'; | ||
|
||
public function __construct( | ||
private readonly VarTagRemover $varTagRemover, | ||
private readonly StaticTypeMapper $staticTypeMapper, | ||
private readonly PhpDocInfoFactory $phpDocInfoFactory | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Remove @var annotation for PHPUnit\Framework\MockObject\MockObject combined with native object type', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
final class SomeTest extends TestCase | ||
{ | ||
/** | ||
* @var SomeClass|MockObject | ||
*/ | ||
private SomeClass $someProperty; | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
final class SomeTest extends TestCase | ||
{ | ||
private SomeClass $someProperty; | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isObjectType($node, new ObjectType(ClassName::TEST_CASE_CLASS))) { | ||
return null; | ||
} | ||
|
||
$hasChanged = false; | ||
|
||
foreach ($node->getProperties() as $property) { | ||
// not yet typed | ||
if (! $property->type instanceof Node) { | ||
continue; | ||
} | ||
|
||
if (count($property->props) !== 1) { | ||
continue; | ||
} | ||
|
||
if ($this->isObjectType($property->type, new ObjectType(self::MOCK_OBJECT_CLASS))) { | ||
continue; | ||
} | ||
|
||
$propertyDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); | ||
|
||
if (! $this->isVarTagUnionTypeMockObject($propertyDocInfo, $property)) { | ||
continue; | ||
} | ||
|
||
// clear var docblock | ||
if ($this->varTagRemover->removeVarTag($property)) { | ||
$hasChanged = true; | ||
} | ||
} | ||
|
||
if (! $hasChanged) { | ||
return null; | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::TYPED_PROPERTIES; | ||
} | ||
|
||
private function isVarTagUnionTypeMockObject(PhpDocInfo $phpDocInfo, Property $property): bool | ||
{ | ||
$varTagValueNode = $phpDocInfo->getVarTagValueNode(); | ||
if (! $varTagValueNode instanceof VarTagValueNode) { | ||
return false; | ||
} | ||
|
||
if (! $varTagValueNode->type instanceof UnionTypeNode) { | ||
return false; | ||
} | ||
|
||
$varTagType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property); | ||
if (! $varTagType instanceof UnionType) { | ||
return false; | ||
} | ||
|
||
foreach ($varTagType->getTypes() as $unionedType) { | ||
if ($unionedType->isSuperTypeOf(new ObjectType(self::MOCK_OBJECT_CLASS))->yes()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Enum; | ||
|
||
final class ClassName | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const TEST_CASE_CLASS = 'PHPUnit\Framework\TestCase'; | ||
} |