143 lines
6.8 KiB
PHP
Raw Normal View History

2019-10-13 07:59:52 +02:00
<?php
declare (strict_types=1);
2019-02-28 22:50:53 +01:00
namespace Rector\BetterPhpDocParser\PhpDocInfo;
use PhpParser\Comment\Doc;
2019-02-28 22:50:53 +01:00
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
2019-02-28 22:50:53 +01:00
use PHPStan\PhpDocParser\Lexer\Lexer;
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
use Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder;
use Rector\BetterPhpDocParser\PhpDocNodeMapper;
2020-10-11 12:40:45 +02:00
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\StaticTypeMapper;
2019-02-28 22:50:53 +01:00
final class PhpDocInfoFactory
{
/**
* @var array<string, PhpDocInfo>
2019-02-28 22:50:53 +01:00
*/
private $phpDocInfosByObjectHash = [];
2019-02-28 22:50:53 +01:00
/**
* @var \Rector\BetterPhpDocParser\PhpDocNodeMapper
2019-02-28 22:50:53 +01:00
*/
private $phpDocNodeMapper;
/**
* @var \Rector\Core\Configuration\CurrentNodeProvider
*/
private $currentNodeProvider;
/**
* @var \PHPStan\PhpDocParser\Lexer\Lexer
*/
private $lexer;
2020-02-19 00:54:28 +01:00
/**
* @var \Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser
2020-02-19 00:54:28 +01:00
*/
private $betterPhpDocParser;
/**
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
/**
* @var \Rector\BetterPhpDocParser\Annotation\AnnotationNaming
*/
private $annotationNaming;
/**
* @var \Rector\ChangesReporting\Collector\RectorChangeCollector
*/
private $rectorChangeCollector;
/**
* @var \Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder
*/
private $phpDocNodeByTypeFinder;
public function __construct(\Rector\BetterPhpDocParser\PhpDocNodeMapper $phpDocNodeMapper, \Rector\Core\Configuration\CurrentNodeProvider $currentNodeProvider, \PHPStan\PhpDocParser\Lexer\Lexer $lexer, \Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser $betterPhpDocParser, \Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper, \Rector\BetterPhpDocParser\Annotation\AnnotationNaming $annotationNaming, \Rector\ChangesReporting\Collector\RectorChangeCollector $rectorChangeCollector, \Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder $phpDocNodeByTypeFinder)
{
$this->phpDocNodeMapper = $phpDocNodeMapper;
$this->currentNodeProvider = $currentNodeProvider;
$this->lexer = $lexer;
$this->betterPhpDocParser = $betterPhpDocParser;
$this->staticTypeMapper = $staticTypeMapper;
$this->annotationNaming = $annotationNaming;
$this->rectorChangeCollector = $rectorChangeCollector;
$this->phpDocNodeByTypeFinder = $phpDocNodeByTypeFinder;
2020-02-02 19:15:36 +01:00
}
public function createFromNodeOrEmpty(\PhpParser\Node $node) : \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo
{
// already added
$phpDocInfo = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo instanceof \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo) {
return $phpDocInfo;
}
$phpDocInfo = $this->createFromNode($node);
if ($phpDocInfo instanceof \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo) {
return $phpDocInfo;
}
return $this->createEmpty($node);
}
public function createFromNode(\PhpParser\Node $node) : ?\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo
2019-02-28 22:50:53 +01:00
{
$objectHash = \spl_object_hash($node);
if (isset($this->phpDocInfosByObjectHash[$objectHash])) {
return $this->phpDocInfosByObjectHash[$objectHash];
}
/** @see \Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator::decorate() */
2020-01-30 22:11:29 +01:00
$this->currentNodeProvider->setNode($node);
$docComment = $node->getDocComment();
if (!$docComment instanceof \PhpParser\Comment\Doc) {
2020-02-02 19:15:36 +01:00
if ($node->getComments() !== []) {
2020-02-16 22:29:32 +01:00
return null;
2020-02-02 19:15:36 +01:00
}
2020-02-16 22:29:32 +01:00
// create empty node
$tokenIterator = new \Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator([]);
$phpDocNode = new \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode([]);
2020-01-30 22:11:29 +01:00
} else {
$text = $docComment->getText();
$tokens = $this->lexer->tokenize($text);
$tokenIterator = new \Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator($tokens);
$phpDocNode = $this->betterPhpDocParser->parse($tokenIterator);
2020-02-02 19:15:36 +01:00
$this->setPositionOfLastToken($phpDocNode);
2019-09-18 12:22:12 +02:00
}
$phpDocInfo = $this->createFromPhpDocNode($phpDocNode, $tokenIterator, $node);
$this->phpDocInfosByObjectHash[$objectHash] = $phpDocInfo;
return $phpDocInfo;
2020-05-04 22:06:33 +02:00
}
public function createEmpty(\PhpParser\Node $node) : \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo
2020-05-04 22:06:33 +02:00
{
/** @see \Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator::decorate() */
2020-05-04 22:06:33 +02:00
$this->currentNodeProvider->setNode($node);
$phpDocNode = new \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode([]);
$phpDocInfo = $this->createFromPhpDocNode($phpDocNode, new \Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator([]), $node);
// multiline by default
$phpDocInfo->makeMultiLined();
return $phpDocInfo;
2019-02-28 22:50:53 +01:00
}
/**
* Needed for printing
*/
private function setPositionOfLastToken(\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode $phpDocNode) : void
2020-02-02 19:15:36 +01:00
{
if ($phpDocNode->children === []) {
2020-02-02 19:15:36 +01:00
return;
2019-02-28 22:50:53 +01:00
}
$phpDocChildNodes = $phpDocNode->children;
$lastChildNode = \array_pop($phpDocChildNodes);
$startAndEnd = $lastChildNode->getAttribute(\Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey::START_AND_END);
if ($startAndEnd instanceof \Rector\BetterPhpDocParser\ValueObject\StartAndEnd) {
$phpDocNode->setAttribute(\Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey::LAST_PHP_DOC_TOKEN_POSITION, $startAndEnd->getEnd());
2019-02-28 22:50:53 +01:00
}
2020-02-02 19:15:36 +01:00
}
private function createFromPhpDocNode(\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode $phpDocNode, \Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator $betterTokenIterator, \PhpParser\Node $node) : \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo
{
$this->phpDocNodeMapper->transform($phpDocNode, $betterTokenIterator);
$phpDocInfo = new \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo($phpDocNode, $betterTokenIterator, $this->staticTypeMapper, $node, $this->annotationNaming, $this->currentNodeProvider, $this->rectorChangeCollector, $this->phpDocNodeByTypeFinder);
$node->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PHP_DOC_INFO, $phpDocInfo);
2020-05-04 22:06:33 +02:00
return $phpDocInfo;
}
2019-02-28 22:50:53 +01:00
}