Skip to content

Commit

Permalink
[TypeDeclaration] Include MockObject&ClassName docblock with Intersec…
Browse files Browse the repository at this point in the history
…tionTypeNode on TypedPropertyFromStrictSetUpRector (#5655)

* [TypeDeclaration] Include MockObject&ClassName docblock with IntersectionTypeNode on TypedPropertyFromStrictSetUpRector

* update

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 22, 2024
1 parent 8038ee1 commit f9b3312
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector\Fixture;

use PHPUnit\Framework\TestCase;

final class IncludeMockDoc extends TestCase
{
/** @var \DateTime */
private $value;

public function setUp(): void
{
parent::setUp();

$this->value = $this->createMock(\DateTime::class);
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector\Fixture;

use PHPUnit\Framework\TestCase;

final class IncludeMockDoc extends TestCase
{
/** @var (\PHPUnit\Framework\MockObject\MockObject & \DateTime) */
private \PHPUnit\Framework\MockObject\MockObject $value;

public function setUp(): void
{
parent::setUp();

$this->value = $this->createMock(\DateTime::class);
}
}

?>
Expand Up @@ -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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit f9b3312

Please sign in to comment.