Skip to content

Commit

Permalink
[Php80] Copy array generic doc on ClassPropertyAssignToConstructorPro…
Browse files Browse the repository at this point in the history
…motionRector (#988)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 12, 2021
1 parent 90c1d09 commit 26ac103
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
namespace Rector\BetterPhpDocParser\PhpDocManipulator;

use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
Expand All @@ -24,7 +27,8 @@ final class PhpDocTypeChanger
public function __construct(
private StaticTypeMapper $staticTypeMapper,
private TypeComparator $typeComparator,
private ParamPhpDocNodeFactory $paramPhpDocNodeFactory
private ParamPhpDocNodeFactory $paramPhpDocNodeFactory,
private NodeNameResolver $nodeNameResolver
) {
}

Expand Down Expand Up @@ -144,5 +148,16 @@ public function copyPropertyDocToParam(Property $property, Param $param): void

$phpDocInfo->removeByType(VarTagValueNode::class);
$param->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);

$functionLike = $param->getAttribute(AttributeKey::PARENT_NODE);
$paramVarName = $this->nodeNameResolver->getName($param->var);
if ($functionLike instanceof ClassMethod && $varTag->type instanceof GenericTypeNode && is_string(
$paramVarName
)) {
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO);
$paramType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($varTag, $property);

$this->changeParamType($phpDocInfo, $paramType, $param, $paramVarName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

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

final class CopyArrayGenericDoc
{
/**
* Comment
* @var array<string, string>
*/
private array $map;

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

?>
-----
<?php declare(strict_types = 1);

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

final class CopyArrayGenericDoc
{
/**
* @param array<string, string> $map
*/
public function __construct(
/**
* Comment
*/
private array $map
)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PHPStan\Type\StringType;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -81,13 +80,13 @@ public function refactor(Node $node): ?Node
return null;
}

private function processIdentical(Expr $value, FuncCall $funcCall): ?Identical
private function processIdentical(Expr $expr, FuncCall $funcCall): ?Identical
{
if (! $this->isName($funcCall, 'strlen')) {
return null;
}

if (! $this->valueResolver->isValue($value, 0)) {
if (! $this->valueResolver->isValue($expr, 0)) {
return null;
}

Expand All @@ -103,7 +102,7 @@ private function processIdentical(Expr $value, FuncCall $funcCall): ?Identical
// Needs string cast if variable type is not string
// see https://github.com/rectorphp/rector/issues/6700
$isStringType = $this->nodeTypeResolver->getNativeType($variable) instanceof StringType;
if (!$isStringType) {
if (! $isStringType) {
return new Identical(new Expr\Cast\String_($variable), new String_(''));
}

Expand Down

0 comments on commit 26ac103

Please sign in to comment.