Skip to content

Commit

Permalink
[Php80] Mirror additional docblock on importNames() on ClassPropertyA…
Browse files Browse the repository at this point in the history
…ssignToConstructorPromotionRector (#2410)

* [Php80] Mirror additional docblock on importNames() on ClassPropertyAssignToConstructorPromotionRector

* update docblock var to param

* add test for non auto import as well

* Fixed 🎉
  • Loading branch information
samsonasik committed Jun 1, 2022
1 parent e6ebae3 commit ca6dfe2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

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

use Doctrine\ORM\Mapping\Id;

class MirrorAdditionalDocblock
{
/**
* @var string
* @Id
*/
public $name;

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

?>
-----
<?php

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

use Doctrine\ORM\Mapping\Id;

class MirrorAdditionalDocblock
{
/**
* @param string $name
*/
public function __construct(
/**
* @Id
*/
public $name
)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

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

use Doctrine\ORM\Mapping\Id;

class MirrorAdditionalDocblock
{
/**
* @var string
* @Id
*/
public $name;

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

?>
-----
<?php

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

use Doctrine\ORM\Mapping\Id;

class MirrorAdditionalDocblock
{
/**
* @param string $name
*/
public function __construct(
/**
* @Id
*/
public $name
)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
Expand Down Expand Up @@ -130,7 +131,7 @@ public function refactor(Node $node): ?Node
$paramTagValueNode = $classMethodPhpDocInfo->getParamTagValueNodeByName($paramName);

if (! $paramTagValueNode instanceof ParamTagValueNode) {
$this->decorateParamWithPropertyPhpDocInfo($property, $param);
$this->decorateParamWithPropertyPhpDocInfo($constructClassMethod, $property, $param, $paramName);
} elseif ($paramTagValueNode->parameterName !== '$' . $propertyName) {
$paramTagValueNode->parameterName = '$' . $propertyName;
$paramTagValueNode->setAttribute(PhpDocAttributeKey::ORIG_NODE, null);
Expand Down Expand Up @@ -163,19 +164,31 @@ private function processNullableType(Property $property, Param $param): void
}
}

private function decorateParamWithPropertyPhpDocInfo(Property $property, Param $param): void
{
private function decorateParamWithPropertyPhpDocInfo(
ClassMethod $classMethod,
Property $property,
Param $param,
string $paramName
): void {
$propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
$propertyPhpDocInfo->markAsChanged();

$param->setAttribute(AttributeKey::PHP_DOC_INFO, $propertyPhpDocInfo);

// make sure the docblock is useful
if ($param->type === null) {
return;
$varTagValueNode = $propertyPhpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
return;
}

$paramType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($varTagValueNode, $property);
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
$this->phpDocTypeChanger->changeParamType($classMethodPhpDocInfo, $paramType, $param, $paramName);
} else {
$paramType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
}

$paramType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
$this->varTagRemover->removeVarPhpTagValueNodeIfNotComment($param, $paramType);
}

Expand Down

0 comments on commit ca6dfe2

Please sign in to comment.