190 lines
5.5 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 PHPStan\PhpDocParser\Parser\PhpDocParser;
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
use Rector\BetterPhpDocParser\Contract\PhpDocNodeFactoryInterface;
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 PhpDocParser
*/
2020-10-11 12:40:45 +02:00
private $betterPhpDocParser;
2019-02-28 22:50:53 +01:00
/**
* @var Lexer
*/
private $lexer;
/**
* @var CurrentNodeProvider
*/
private $currentNodeProvider;
/**
* @var StaticTypeMapper
*/
private $staticTypeMapper;
2020-02-19 00:54:28 +01:00
/**
* @var PhpDocNodeMapper
2020-02-19 00:54:28 +01:00
*/
private $phpDocNodeMapper;
2020-02-19 00:54:28 +01:00
/**
* @var AnnotationNaming
*/
private $annotationNaming;
/**
* @var RectorChangeCollector
*/
private $rectorChangeCollector;
/**
* @var array<string, PhpDocInfo>
*/
private $phpDocInfosByObjectHash = [];
2019-02-28 22:50:53 +01:00
public function __construct(
PhpDocNodeMapper $phpDocNodeMapper,
CurrentNodeProvider $currentNodeProvider,
2020-07-26 09:49:22 +02:00
Lexer $lexer,
2020-10-11 12:40:45 +02:00
BetterPhpDocParser $betterPhpDocParser,
StaticTypeMapper $staticTypeMapper,
AnnotationNaming $annotationNaming,
RectorChangeCollector $rectorChangeCollector
2019-02-28 22:50:53 +01:00
) {
2020-10-11 12:40:45 +02:00
$this->betterPhpDocParser = $betterPhpDocParser;
2019-02-28 22:50:53 +01:00
$this->lexer = $lexer;
$this->currentNodeProvider = $currentNodeProvider;
$this->staticTypeMapper = $staticTypeMapper;
$this->phpDocNodeMapper = $phpDocNodeMapper;
$this->annotationNaming = $annotationNaming;
$this->rectorChangeCollector = $rectorChangeCollector;
2020-02-02 19:15:36 +01:00
}
public function createFromNodeOrEmpty(Node $node): PhpDocInfo
{
// already added
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo instanceof PhpDocInfo) {
return $phpDocInfo;
}
$phpDocInfo = $this->createFromNode($node);
if ($phpDocInfo instanceof PhpDocInfo) {
return $phpDocInfo;
}
return $this->createEmpty($node);
}
2020-02-16 22:29:32 +01:00
public function createFromNode(Node $node): ?PhpDocInfo
2019-02-28 22:50:53 +01:00
{
$objectHash = spl_object_hash($node);
if (isset($this->phpDocInfosByObjectHash[$objectHash])) {
return $this->phpDocInfosByObjectHash[$objectHash];
}
2020-01-30 22:11:29 +01:00
/** needed for @see PhpDocNodeFactoryInterface */
$this->currentNodeProvider->setNode($node);
$docComment = $node->getDocComment();
if (! $docComment instanceof 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
$content = '';
$tokenIterator = new BetterTokenIterator([]);
$phpDocNode = new PhpDocNode([]);
2020-01-30 22:11:29 +01:00
} else {
$content = $docComment->getText();
2020-01-30 22:11:29 +01:00
$tokens = $this->lexer->tokenize($content);
$tokenIterator = new 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
}
2020-02-19 00:54:28 +01:00
2020-05-04 22:06:33 +02:00
public function createEmpty(Node $node): PhpDocInfo
{
/** needed for @see PhpDocNodeFactoryInterface */
$this->currentNodeProvider->setNode($node);
2019-09-18 12:22:12 +02:00
$phpDocNode = new PhpDocNode([]);
$phpDocInfo = $this->createFromPhpDocNode($phpDocNode, new BetterTokenIterator([]), $node);
// multiline by default
$phpDocInfo->makeMultiLined();
return $phpDocInfo;
2019-02-28 22:50:53 +01:00
}
/**
* Needed for printing
*/
private function setPositionOfLastToken(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;
2019-02-28 22:50:53 +01:00
$lastChildNode = array_pop($phpDocChildNodes);
$startAndEnd = $lastChildNode->getAttribute(PhpDocAttributeKey::START_AND_END);
if ($startAndEnd instanceof StartAndEnd) {
$phpDocNode->setAttribute(PhpDocAttributeKey::LAST_PHP_DOC_TOKEN_POSITION, $startAndEnd->getEnd());
2019-02-28 22:50:53 +01:00
}
2020-02-02 19:15:36 +01:00
}
2020-05-04 22:06:33 +02:00
private function createFromPhpDocNode(
PhpDocNode $phpDocNode,
BetterTokenIterator $betterTokenIterator,
2020-05-04 22:06:33 +02:00
Node $node
): PhpDocInfo {
$this->phpDocNodeMapper->transform($phpDocNode, $betterTokenIterator);
2020-05-04 22:06:33 +02:00
$phpDocInfo = new PhpDocInfo(
$phpDocNode,
$betterTokenIterator,
2020-05-04 22:06:33 +02:00
$this->staticTypeMapper,
$node,
$this->annotationNaming,
$this->currentNodeProvider,
$this->rectorChangeCollector
2020-05-04 22:06:33 +02:00
);
2020-05-04 22:06:33 +02:00
$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
return $phpDocInfo;
}
2019-02-28 22:50:53 +01:00
}