improve PhpDocInfoFqnTypeDecorator

This commit is contained in:
Tomas Votruba 2018-05-02 15:43:21 +02:00
parent 9aafcbedf1
commit bf0b07436f
2 changed files with 78 additions and 15 deletions

View File

@ -31,22 +31,28 @@ final class DocBlockAnalyzer
* @var TypeResolver
*/
private $typeResolver;
/**
* @var PhpDocInfoFqnTypeDecorator
*/
private $phpDocInfoFqnTypeDecorator;
public function __construct(
PhpDocInfoFactory $phpDocInfoFactory,
PhpDocInfoPrinter $phpDocInfoPrinter,
NamespaceAnalyzer $namespaceAnalyzer,
TypeResolver $typeResolver
TypeResolver $typeResolver,
PhpDocInfoFqnTypeDecorator $phpDocInfoFqnTypeDecorator
) {
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->phpDocInfoPrinter = $phpDocInfoPrinter;
$this->namespaceAnalyzer = $namespaceAnalyzer;
$this->typeResolver = $typeResolver;
$this->phpDocInfoFqnTypeDecorator = $phpDocInfoFqnTypeDecorator;
}
public function hasAnnotation(Node $node, string $annotation): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
return $phpDocInfo->hasTag($annotation);
}
@ -57,7 +63,7 @@ final class DocBlockAnalyzer
return;
}
$phpDocInfo = $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$phpDocInfo->removeParamTagByParameter($name);
$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
@ -69,7 +75,7 @@ final class DocBlockAnalyzer
return;
}
$phpDocInfo = $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
if ($content) {
$phpDocInfo->removeTagByNameAndContent($name, $content);
@ -91,7 +97,7 @@ final class DocBlockAnalyzer
return;
}
$phpDocInfo = $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$phpDocInfo->replacePhpDocTypeByAnother($oldType, $newType);
@ -104,7 +110,7 @@ final class DocBlockAnalyzer
return;
}
$phpDocInfo = $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$phpDocInfo->replaceTagByAnother($oldAnnotation, $newAnnotation);
@ -121,7 +127,7 @@ final class DocBlockAnalyzer
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$varType = $phpDocInfo->getVarTypeNode();
if ($varType === null) {
@ -147,7 +153,7 @@ final class DocBlockAnalyzer
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
return (string) $phpDocInfo->getParamTypeNode($paramName);
}
@ -162,7 +168,7 @@ final class DocBlockAnalyzer
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
return $phpDocInfo->getTagsByName($name);
}
@ -191,4 +197,11 @@ final class DocBlockAnalyzer
// no comments, null
$node->setAttribute('comments', null);
}
private function createPhpDocInfoFromNode(Node $node): PhpDocInfo
{
$this->phpDocInfoFqnTypeDecorator->setCurrentPhpParserNode($node);
return $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
}
}

View File

@ -2,16 +2,66 @@
namespace Rector\ReflectionDocBlock\NodeAnalyzer;
use Symplify\BetterPhpDocParser\Contract\PhpDocInfoDecoratorInterface;
use Symplify\BetterPhpDocParser\PhpDocParser\PhpDocInfo;
use PhpParser\Node as PhpParserNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Symplify\BetterPhpDocParser\PhpDocInfo\AbstractPhpDocInfoDecorator;
use Symplify\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
final class PhpDocInfoFqnTypeDecorator implements PhpDocInfoDecoratorInterface
final class PhpDocInfoFqnTypeDecorator extends AbstractPhpDocInfoDecorator
{
/**
* @var NamespaceAnalyzer
*/
private $namespaceAnalyzer;
/**
* @var PhpParserNode|null
*/
private $phpParserNode;
public function __construct(NamespaceAnalyzer $namespaceAnalyzer)
{
$this->namespaceAnalyzer = $namespaceAnalyzer;
}
public function setCurrentPhpParserNode(PhpParserNode $phpParserNode)
{
$this->phpParserNode = $phpParserNode;
}
public function decorate(PhpDocInfo $phpDocInfo): void
{
dump('todo');
die;
$this->traverseNodes($phpDocInfo->getPhpDocNode());
}
// iterate all the nodes and make types FQN
protected function traverseNode(Node $node): Node
{
if ($this->shouldSkip($node)) {
return $node;
}
/** @var IdentifierTypeNode $node */
$node->name = $this->namespaceAnalyzer->resolveTypeToFullyQualified($node->name, $this->phpParserNode);
return $node;
}
private function isClassyType(string $name): bool
{
return ctype_upper($name[0]);
}
private function shouldSkip(Node $node): bool
{
if (! $node instanceof IdentifierTypeNode) {
return true;
}
if (! $this->isClassyType($node->name)) {
return true;
}
return false;
}
}