From f9b3312035a4e5baee7105b19715d057587e1f59 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 22 Feb 2024 15:43:48 +0700 Subject: [PATCH] [TypeDeclaration] Include MockObject&ClassName docblock with IntersectionTypeNode on TypedPropertyFromStrictSetUpRector (#5655) * [TypeDeclaration] Include MockObject&ClassName docblock with IntersectionTypeNode on TypedPropertyFromStrictSetUpRector * update * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action --- .../Fixture/include_mock_doc.php.inc | 41 +++++++++++++++++++ .../TypedPropertyFromStrictSetUpRector.php | 24 ++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector/Fixture/include_mock_doc.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector/Fixture/include_mock_doc.php.inc b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector/Fixture/include_mock_doc.php.inc new file mode 100644 index 00000000000..70ad936484c --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector/Fixture/include_mock_doc.php.inc @@ -0,0 +1,41 @@ +value = $this->createMock(\DateTime::class); + } +} + +?> +----- +value = $this->createMock(\DateTime::class); + } +} + +?> diff --git a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php index 786f5fe260b..0373663102f 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php @@ -4,9 +4,15 @@ namespace Rector\TypeDeclaration\Rector\Property; +use PHPStan\Type\ObjectType; use PhpParser\Node; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; +use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; +use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode; +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; +use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode; +use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -24,7 +30,9 @@ final class TypedPropertyFromStrictSetUpRector extends AbstractRector implements { public function __construct( private readonly TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer, - private readonly StaticTypeMapper $staticTypeMapper + private readonly StaticTypeMapper $staticTypeMapper, + private readonly PhpDocInfoFactory $phpDocInfoFactory, + private readonly DocBlockUpdater $docBlockUpdater ) { } @@ -109,6 +117,20 @@ public function refactor(Node $node): ?Node continue; } + if ($propertyType instanceof ObjectType && $propertyType->getClassName() === 'PHPUnit\Framework\MockObject\MockObject') { + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); + $varTag = $phpDocInfo->getVarTagValueNode(); + $varType = $phpDocInfo->getVarType(); + + if ($varTag instanceof VarTagValueNode && $varType instanceof ObjectType && $varType->getClassName() !== 'PHPUnit\Framework\MockObject\MockObject') { + $varTag->type = new IntersectionTypeNode([ + new FullyQualifiedIdentifierTypeNode($propertyType->getClassName()), + new FullyQualifiedIdentifierTypeNode($varType->getClassName()) + ]); + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property); + } + } + $property->type = $propertyTypeNode; $hasChanged = true; }