Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ use JMS\Serializer\Annotation as Serializer;
class SomeFixture
{
/**
* @var \Ramsey\Uuid\UuidInterface
* @ORM\Id
* @ORM\Column(type="uuid_binary")
* @Serializer\Type("string")
* @var \Ramsey\Uuid\UuidInterface
*/
private $id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\Ast\PhpDocNodeTraverser;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\StaticTypeMapper;

final class DocBlockClassRenamer
Expand Down Expand Up @@ -54,14 +53,9 @@ function (PhpDocParserNode $node) use ($phpParserNode, $oldType, $newType): PhpD
return $node;
}

$newIdentifierType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType);
if ($newIdentifierType === null) {
throw new ShouldNotHappenException();
}

$this->hasNodeChanged = true;

return $newIdentifierType;
return $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType);
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
Expand Down Expand Up @@ -256,8 +257,22 @@ public function changeVarTag(Node $node, Type $newType): void
}
}

$this->removeTagFromNode($node, 'var', true);
$this->addTypeSpecificTag($node, 'var', $newType);
if ($this->hasTag($node, '@var')) {
// just change the type
$varTag = $this->getTagByName($node, '@var');

/** @var VarTagValueNode $varTagValueNode */
$varTagValueNode = $varTag->value;

$phpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType);
$varTagValueNode->type = $phpDocType;

// update doc :)
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
} else {
$this->addTypeSpecificTag($node, 'var', $newType);
}

// to invoke the node override
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
Expand Down
6 changes: 1 addition & 5 deletions packages/NodeTypeResolver/src/StaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function __construct(
$this->objectTypeSpecifier = $objectTypeSpecifier;
}

public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType): ?TypeNode
public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType): TypeNode
{
if ($phpStanType instanceof UnionType) {
$unionTypesNodes = [];
Expand All @@ -104,10 +104,6 @@ public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType): ?TypeN

if ($phpStanType instanceof ArrayType) {
$itemTypeNode = $this->mapPHPStanTypeToPHPStanPhpDocTypeNode($phpStanType->getItemType());
if ($itemTypeNode === null) {
throw new ShouldNotHappenException();
}

return new ArrayTypeNode($itemTypeNode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function test(string $file): void

public function provideDataForTest(): Iterator
{
yield [__DIR__ . '/Fixture/keep_comment.php.inc'];
yield [__DIR__ . '/Fixture/property_assign.php.inc'];
yield [__DIR__ . '/Fixture/default_value.php.inc'];
yield [__DIR__ . '/Fixture/assign_conflict.php.inc'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

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

class KeepComment
{
/**
* @var string e.g. 123
*/
private $ids;

public function setIds()
{
$this->ids[] = 'hello';
if (random_int(1, 100)) {
$this->ids = 'hey';
}
}
}

?>
-----
<?php

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

class KeepComment
{
/**
* @var string[]|string e.g. 123
*/
private $ids;

public function setIds()
{
$this->ids[] = 'hello';
if (random_int(1, 100)) {
$this->ids = 'hey';
}
}
}

?>