Skip to content

Commit

Permalink
Copy subtype phpdoc on ClassPropertyAssignToConstructorPromotionRecto…
Browse files Browse the repository at this point in the history
…r (#8438) (#5603)

* Copy subtype phpdoc on ClassPropertyAssignToConstructorPromotionRector (#8438)

* Apply suggestions from code review

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

---------

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
pkvach and samsonasik committed Feb 11, 2024
1 parent 6980dc6 commit df8d2b7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

class CopyIntegerRangeType
{
/** @var positive-int */
public int $count;

public function __construct(int $count)
{
$this->count = $count;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

class CopyIntegerRangeType
{
public function __construct(
/** @var positive-int */
public int $count
)
{
}
}

?>
13 changes: 13 additions & 0 deletions rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\DeadCode\PhpDoc\DeadVarTagValueNodeAnalyzer;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;

final readonly class VarTagRemover
Expand All @@ -25,6 +26,7 @@ public function __construct(
private DeadVarTagValueNodeAnalyzer $deadVarTagValueNodeAnalyzer,
private PhpDocTypeChanger $phpDocTypeChanger,
private DocBlockUpdater $docBlockUpdater,
private TypeComparator $typeComparator,
) {
}

Expand Down Expand Up @@ -78,7 +80,18 @@ public function removeVarPhpTagValueNodeIfNotComment(Expression | Property | Par
return;
}

// keep subtypes like positive-int
if ($this->shouldKeepSubtypes($type, $phpDocInfo->getVarType())) {
return;
}

$phpDocInfo->removeByType(VarTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
}

private function shouldKeepSubtypes(Type $type, Type $varType): bool
{
return ! $this->typeComparator->areTypesEqual($type, $varType)
&& $this->typeComparator->isSubtype($varType, $type);
}
}

0 comments on commit df8d2b7

Please sign in to comment.