-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Rector to commit d8f31e7559c9e3e288f2f58228120c71986a80e9
rectorphp/rector-src@d8f31e7 Add RemoveTypedPropertyNonMockDocblockRector (#6306)
- Loading branch information
1 parent
4dd8bdd
commit 7f51244
Showing
11 changed files
with
188 additions
and
23 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
140 changes: 140 additions & 0 deletions
140
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,140 @@ | ||
<?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 | ||
{ | ||
/** | ||
* @readonly | ||
* @var \Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover | ||
*/ | ||
private $varTagRemover; | ||
/** | ||
* @readonly | ||
* @var \Rector\StaticTypeMapper\StaticTypeMapper | ||
*/ | ||
private $staticTypeMapper; | ||
/** | ||
* @readonly | ||
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory | ||
*/ | ||
private $phpDocInfoFactory; | ||
/** | ||
* @var string | ||
*/ | ||
private const MOCK_OBJECT_CLASS = 'PHPUnit\\Framework\\MockObject\\MockObject'; | ||
public function __construct(VarTagRemover $varTagRemover, StaticTypeMapper $staticTypeMapper, PhpDocInfoFactory $phpDocInfoFactory) | ||
{ | ||
$this->varTagRemover = $varTagRemover; | ||
$this->staticTypeMapper = $staticTypeMapper; | ||
$this->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
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 | ||
|
||
declare (strict_types=1); | ||
namespace Rector\Enum; | ||
|
||
final class ClassName | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const TEST_CASE_CLASS = 'PHPUnit\\Framework\\TestCase'; | ||
} |
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
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