Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve param array type for change type #2987

Merged
merged 1 commit into from
Mar 2, 2020
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
55 changes: 45 additions & 10 deletions packages/better-php-doc-parser/src/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Param;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
Expand All @@ -29,10 +30,12 @@
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\ShortNameAwareTagInterface;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\TypeAwareTagValueNodeInterface;

use Rector\Core\Exception\NotImplementedException;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\PHPStan\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\PhpDocParser\ParamPhpDocNodeFactory;

/**
* @see \Rector\BetterPhpDocParser\Tests\PhpDocInfo\PhpDocInfo\PhpDocInfoTest
Expand Down Expand Up @@ -74,6 +77,11 @@ final class PhpDocInfo
*/
private $typeComparator;

/**
* @var ParamPhpDocNodeFactory
*/
private $paramPhpDocNodeFactory;

/**
* @param mixed[] $tokens
*/
Expand All @@ -83,7 +91,8 @@ public function __construct(
string $originalContent,
StaticTypeMapper $staticTypeMapper,
Node $node,
TypeComparator $typeComparator
TypeComparator $typeComparator,
ParamPhpDocNodeFactory $paramPhpDocNodeFactory
) {
$this->phpDocNode = $attributeAwarePhpDocNode;
$this->tokens = $tokens;
Expand All @@ -92,6 +101,7 @@ public function __construct(
$this->staticTypeMapper = $staticTypeMapper;
$this->node = $node;
$this->typeComparator = $typeComparator;
$this->paramPhpDocNodeFactory = $paramPhpDocNodeFactory;
}

public function getOriginalContent(): string
Expand Down Expand Up @@ -399,15 +409,7 @@ public function isEmpty(): bool

public function addTagValueNode(PhpDocTagValueNode $phpDocTagValueNode): void
{
if ($phpDocTagValueNode instanceof ReturnTagValueNode) {
$name = '@return';
} elseif ($phpDocTagValueNode instanceof ParamTagValueNode) {
$name = '@param';
} elseif ($phpDocTagValueNode instanceof VarTagValueNode) {
$name = '@var';
} else {
throw new NotImplementedException();
}
$name = $this->resolveNameForPhpDocTagValueNode($phpDocTagValueNode);

$phpDocTagNode = new AttributeAwarePhpDocTagNode($name, $phpDocTagValueNode);
$this->addPhpDocTagNode($phpDocTagNode);
Expand All @@ -422,6 +424,22 @@ public function isNewNode(): bool
return $this->tokens === [];
}

public function changeParamType(Type $type, Param $param, string $paramName): void
{
$paramTagValueNode = $this->getParamTagValueByName($paramName);

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

// override existing type
if ($paramTagValueNode !== null) {
$paramTagValueNode->type = $phpDocType;
return;
}

$paramTagValueNode = $this->paramPhpDocNodeFactory->create($type, $param);
$this->addTagValueNode($paramTagValueNode);
}

private function getParamTagValueByName(string $name): ?AttributeAwareParamTagValueNode
{
$phpDocNode = $this->getPhpDocNode();
Expand Down Expand Up @@ -461,4 +479,21 @@ private function areAnnotationNamesEqual(string $firstAnnotationName, string $se

return $firstAnnotationName === $secondAnnotationName;
}

private function resolveNameForPhpDocTagValueNode(PhpDocTagValueNode $phpDocTagValueNode): string
{
if ($phpDocTagValueNode instanceof ReturnTagValueNode) {
return '@return';
}

if ($phpDocTagValueNode instanceof ParamTagValueNode) {
return '@param';
}

if ($phpDocTagValueNode instanceof VarTagValueNode) {
return '@var';
}

throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\PhpDocParser\ParamPhpDocNodeFactory;

final class PhpDocInfoFactory
{
Expand Down Expand Up @@ -51,26 +52,35 @@ final class PhpDocInfoFactory
*/
private $attributeAwareNodeFactory;

/**
* @var ParamPhpDocNodeFactory
*/
private $paramPhpDocNodeFactory;

public function __construct(
PhpDocParser $phpDocParser,
Lexer $lexer,
CurrentNodeProvider $currentNodeProvider,
StaticTypeMapper $staticTypeMapper,
TypeComparator $typeComparator,
AttributeAwareNodeFactory $attributeAwareNodeFactory
AttributeAwareNodeFactory $attributeAwareNodeFactory,
ParamPhpDocNodeFactory $paramPhpDocNodeFactory
) {
$this->phpDocParser = $phpDocParser;
$this->lexer = $lexer;
$this->currentNodeProvider = $currentNodeProvider;
$this->staticTypeMapper = $staticTypeMapper;
$this->typeComparator = $typeComparator;
$this->attributeAwareNodeFactory = $attributeAwareNodeFactory;
$this->paramPhpDocNodeFactory = $paramPhpDocNodeFactory;
}

public function createFromString(Node $node, string $content): PhpDocInfo
{
$tokens = $this->lexer->tokenize($content);
$phpDocNode = $this->parseTokensToPhpDocNode($tokens);

/** @var AttributeAwarePhpDocNode $phpDocNode */
$phpDocNode = $this->attributeAwareNodeFactory->createFromNode($phpDocNode, $content);

$phpDocInfo = new PhpDocInfo(
Expand All @@ -79,7 +89,8 @@ public function createFromString(Node $node, string $content): PhpDocInfo
$content,
$this->staticTypeMapper,
$node,
$this->typeComparator
$this->typeComparator,
$this->paramPhpDocNodeFactory
);

$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
Expand Down Expand Up @@ -108,6 +119,7 @@ public function createFromNode(Node $node): ?PhpDocInfo
$this->setPositionOfLastToken($phpDocNode);
}

/** @var AttributeAwarePhpDocNode $phpDocNode */
$phpDocNode = $this->attributeAwareNodeFactory->createFromNode($phpDocNode, $content);

$phpDocInfo = new PhpDocInfo(
Expand All @@ -116,7 +128,8 @@ public function createFromNode(Node $node): ?PhpDocInfo
$content,
$this->staticTypeMapper,
$node,
$this->typeComparator
$this->typeComparator,
$this->paramPhpDocNodeFactory
);
$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\PhpDocParser\ParamPhpDocNodeFactory;
use Rector\TypeDeclaration\TypeInferer\ParamTypeInferer;

/**
Expand All @@ -25,21 +24,13 @@
*/
final class AddArrayParamDocTypeRector extends AbstractRector
{
/**
* @var ParamPhpDocNodeFactory
*/
private $paramPhpDocNodeFactory;

/**
* @var ParamTypeInferer
*/
private $paramTypeInferer;

public function __construct(
ParamPhpDocNodeFactory $paramPhpDocNodeFactory,
ParamTypeInferer $paramTypeInferer
) {
$this->paramPhpDocNodeFactory = $paramPhpDocNodeFactory;
public function __construct(ParamTypeInferer $paramTypeInferer)
{
$this->paramTypeInferer = $paramTypeInferer;
}

Expand Down Expand Up @@ -113,8 +104,8 @@ public function refactor(Node $node): ?Node
return null;
}

$paramTagValueNode = $this->paramPhpDocNodeFactory->create($type, $param);
$phpDocInfo->addTagValueNode($paramTagValueNode);
$paramName = $this->getName($param);
$phpDocInfo->changeParamType($type, $param, $paramName);
}

return $node;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayParamDocTypeRector\Fixture;

use Message;

class ParamDouble
{
/**
* @var array $meta
*/
private $meta = [];

/**
* @param array $meta
*
* @return Message
*/
public function setMeta(array $meta)
{
$this->meta = $meta;
}
}

?>
-----
<?php

namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayParamDocTypeRector\Fixture;

use Message;

class ParamDouble
{
/**
* @var array $meta
*/
private $meta = [];

/**
* @param mixed[] $meta
*
* @return Message
*/
public function setMeta(array $meta)
{
$this->meta = $meta;
}
}

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

namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayParamDocTypeRector\Fixture;

use Message;

class ParamNoDouble
{
/**
* @var array $meta
*/
private $meta = [];

/**
* @return Message
*/
public function setMeta(array $meta)
{
$this->meta = $meta;
}
}

?>
-----
<?php

namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayParamDocTypeRector\Fixture;

use Message;

class ParamNoDouble
{
/**
* @var array $meta
*/
private $meta = [];

/**
* @return Message
* @param mixed[] $meta
*/
public function setMeta(array $meta)
{
$this->meta = $meta;
}
}

?>