mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-19 07:40:49 +01:00
* make use of new simple-php-doc-parser * add ParamPhpDocNodeVisitor * remove uneeded contracts * skip bool instanceof * [ci-review] Rector Rectify * [ci-review] Rector Rectify Co-authored-by: kaizen-ci <info@kaizen-ci.org>
204 lines
5.7 KiB
PHP
204 lines
5.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Rector\BetterPhpDocParser\PhpDocInfo;
|
|
|
|
use PhpParser\Comment\Doc;
|
|
use PhpParser\Node;
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
|
|
use PHPStan\PhpDocParser\Lexer\Lexer;
|
|
use PHPStan\PhpDocParser\Parser\PhpDocParser;
|
|
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
|
|
use Rector\BetterPhpDocParser\Contract\PhpDocNodeFactoryInterface;
|
|
use Rector\BetterPhpDocParser\PhpDocNodeMapper;
|
|
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;
|
|
|
|
final class PhpDocInfoFactory
|
|
{
|
|
/**
|
|
* @var PhpDocParser
|
|
*/
|
|
private $betterPhpDocParser;
|
|
|
|
/**
|
|
* @var Lexer
|
|
*/
|
|
private $lexer;
|
|
|
|
/**
|
|
* @var CurrentNodeProvider
|
|
*/
|
|
private $currentNodeProvider;
|
|
|
|
/**
|
|
* @var StaticTypeMapper
|
|
*/
|
|
private $staticTypeMapper;
|
|
|
|
/**
|
|
* @var PhpDocNodeMapper
|
|
*/
|
|
private $phpDocNodeMapper;
|
|
|
|
/**
|
|
* @var AnnotationNaming
|
|
*/
|
|
private $annotationNaming;
|
|
|
|
/**
|
|
* @var RectorChangeCollector
|
|
*/
|
|
private $rectorChangeCollector;
|
|
|
|
/**
|
|
* @var array<string, PhpDocInfo>
|
|
*/
|
|
private $phpDocInfosByObjectHash = [];
|
|
|
|
public function __construct(
|
|
PhpDocNodeMapper $phpDocNodeMapper,
|
|
CurrentNodeProvider $currentNodeProvider,
|
|
Lexer $lexer,
|
|
BetterPhpDocParser $betterPhpDocParser,
|
|
StaticTypeMapper $staticTypeMapper,
|
|
AnnotationNaming $annotationNaming,
|
|
RectorChangeCollector $rectorChangeCollector
|
|
) {
|
|
$this->betterPhpDocParser = $betterPhpDocParser;
|
|
$this->lexer = $lexer;
|
|
$this->currentNodeProvider = $currentNodeProvider;
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
|
$this->phpDocNodeMapper = $phpDocNodeMapper;
|
|
$this->annotationNaming = $annotationNaming;
|
|
$this->rectorChangeCollector = $rectorChangeCollector;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public function createFromNode(Node $node): ?PhpDocInfo
|
|
{
|
|
$objectHash = spl_object_hash($node);
|
|
if (isset($this->phpDocInfosByObjectHash[$objectHash])) {
|
|
return $this->phpDocInfosByObjectHash[$objectHash];
|
|
}
|
|
|
|
/** needed for @see PhpDocNodeFactoryInterface */
|
|
$this->currentNodeProvider->setNode($node);
|
|
|
|
$docComment = $node->getDocComment();
|
|
if (! $docComment instanceof Doc) {
|
|
if ($node->getComments() !== []) {
|
|
return null;
|
|
}
|
|
|
|
// create empty node
|
|
$content = '';
|
|
$tokens = [];
|
|
$phpDocNode = new PhpDocNode([]);
|
|
} else {
|
|
$content = $docComment->getText();
|
|
$tokens = $this->lexer->tokenize($content);
|
|
|
|
$phpDocNode = $this->parseTokensToPhpDocNode($tokens);
|
|
$this->setPositionOfLastToken($phpDocNode);
|
|
}
|
|
|
|
$phpDocInfo = $this->createFromPhpDocNode($phpDocNode, $content, $tokens, $node);
|
|
$this->phpDocInfosByObjectHash[$objectHash] = $phpDocInfo;
|
|
|
|
return $phpDocInfo;
|
|
}
|
|
|
|
public function createEmpty(Node $node): PhpDocInfo
|
|
{
|
|
/** needed for @see PhpDocNodeFactoryInterface */
|
|
$this->currentNodeProvider->setNode($node);
|
|
|
|
$phpDocNode = new PhpDocNode([]);
|
|
$phpDocInfo = $this->createFromPhpDocNode($phpDocNode, '', [], $node);
|
|
|
|
// multiline by default
|
|
$phpDocInfo->makeMultiLined();
|
|
|
|
return $phpDocInfo;
|
|
}
|
|
|
|
/**
|
|
* @param mixed[][] $tokens
|
|
*/
|
|
private function parseTokensToPhpDocNode(array $tokens): PhpDocNode
|
|
{
|
|
$tokenIterator = new BetterTokenIterator($tokens);
|
|
return $this->betterPhpDocParser->parse($tokenIterator);
|
|
}
|
|
|
|
/**
|
|
* Needed for printing
|
|
*/
|
|
private function setPositionOfLastToken(PhpDocNode $phpDocNode): void
|
|
{
|
|
if ($phpDocNode->children === []) {
|
|
return;
|
|
}
|
|
|
|
$phpDocChildNodes = $phpDocNode->children;
|
|
$lastChildNode = array_pop($phpDocChildNodes);
|
|
|
|
/** @var StartAndEnd $startAndEnd */
|
|
$startAndEnd = $lastChildNode->getAttribute(PhpDocAttributeKey::START_AND_END);
|
|
|
|
if ($startAndEnd !== null) {
|
|
$phpDocNode->setAttribute(PhpDocAttributeKey::LAST_TOKEN_POSITION, $startAndEnd->getEnd());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param mixed[] $tokens
|
|
*/
|
|
private function createFromPhpDocNode(
|
|
PhpDocNode $phpDocNode,
|
|
string $content,
|
|
array $tokens,
|
|
Node $node
|
|
): PhpDocInfo {
|
|
$this->phpDocNodeMapper->transform($phpDocNode, new BetterTokenIterator($tokens));
|
|
|
|
$phpDocInfo = new PhpDocInfo(
|
|
$phpDocNode,
|
|
$tokens,
|
|
$content,
|
|
$this->staticTypeMapper,
|
|
$node,
|
|
$this->annotationNaming,
|
|
$this->currentNodeProvider,
|
|
$this->rectorChangeCollector
|
|
);
|
|
|
|
$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
|
|
|
|
return $phpDocInfo;
|
|
}
|
|
}
|