Skip to content

Commit

Permalink
[TypeDeclaration] Handle DateTimeInterface from use statement on Merg…
Browse files Browse the repository at this point in the history
…eDateTimePropertyTypeDeclarationRector (#5330)

* [TypeDeclaration] Handle DateTimeInterface from use statement on MergeDateTimePropertyTypeDeclarationRector

* [TypeDeclaration] Handle DateTimeInterface from use statement on MergeDateTimePropertyTypeDeclarationRector

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 5, 2023
1 parent d22741e commit a92fade
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\MergeDateTimePropertyTypeDeclarationRector\Fixture;

use DateTimeInterface;
use DateTime;

final class TypeFromUse
{
/**
* @var DateTimeInterface
*/
private DateTime $dateTime;
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\MergeDateTimePropertyTypeDeclarationRector\Fixture;

use DateTimeInterface;
use DateTime;

final class TypeFromUse
{
private \DateTimeInterface $dateTime;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\TypeDeclaration\Rector\Class_;

use PHPStan\Type\TypeWithClassName;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
Expand All @@ -13,7 +14,6 @@
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -92,7 +92,10 @@ public function refactor(Node $node): ?Node
}

$varType = $phpDocInfo->getVarType();
if ($varType instanceof FullyQualifiedObjectType && $varType->getClassName() === 'DateTimeInterface') {
$className = $varType instanceof TypeWithClassName
? $this->nodeTypeResolver->getFullyQualifiedClassName($varType)
: null;
if ($className === 'DateTimeInterface') {
$phpDocInfo->removeByType(VarTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);

Expand Down

0 comments on commit a92fade

Please sign in to comment.