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;
|
|
|
|
|
2021-01-20 18:41:35 +07:00
|
|
|
use PhpParser\Comment\Doc;
|
2019-02-28 22:50:53 +01:00
|
|
|
use PhpParser\Node;
|
2021-03-20 16:27:18 +01:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
|
2019-02-28 22:50:53 +01:00
|
|
|
use PHPStan\PhpDocParser\Lexer\Lexer;
|
|
|
|
use PHPStan\PhpDocParser\Parser\PhpDocParser;
|
2021-01-19 16:03:26 +01:00
|
|
|
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
|
2019-09-27 16:25:15 +02:00
|
|
|
use Rector\BetterPhpDocParser\Contract\PhpDocNodeFactoryInterface;
|
2021-03-20 16:27:18 +01:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocNodeMapper;
|
2020-10-11 12:40:45 +02:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
|
2021-04-04 11:01:11 +02:00
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
|
2021-04-06 19:33:09 +02:00
|
|
|
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
|
2020-09-01 19:56:30 +02:00
|
|
|
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
|
2021-01-19 20:45:30 +01:00
|
|
|
use Rector\ChangesReporting\Collector\RectorChangeCollector;
|
2020-02-06 22:48:18 +01:00
|
|
|
use Rector\Core\Configuration\CurrentNodeProvider;
|
2020-01-29 14:36:09 +01:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-02-10 10:17:05 +01:00
|
|
|
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;
|
|
|
|
|
2019-08-26 11:37:02 +02:00
|
|
|
/**
|
|
|
|
* @var CurrentNodeProvider
|
|
|
|
*/
|
|
|
|
private $currentNodeProvider;
|
|
|
|
|
2019-09-06 12:30:58 +02:00
|
|
|
/**
|
|
|
|
* @var StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
|
|
|
|
2020-02-19 00:54:28 +01:00
|
|
|
/**
|
2021-03-20 16:27:18 +01:00
|
|
|
* @var PhpDocNodeMapper
|
2020-02-19 00:54:28 +01:00
|
|
|
*/
|
2021-03-20 16:27:18 +01:00
|
|
|
private $phpDocNodeMapper;
|
2020-02-19 00:54:28 +01:00
|
|
|
|
2020-07-29 01:41:20 +02:00
|
|
|
/**
|
2021-01-19 20:45:30 +01:00
|
|
|
* @var AnnotationNaming
|
2020-07-29 01:41:20 +02:00
|
|
|
*/
|
2021-01-19 20:45:30 +01:00
|
|
|
private $annotationNaming;
|
2020-07-29 01:41:20 +02:00
|
|
|
|
2021-01-19 16:03:26 +01:00
|
|
|
/**
|
2021-01-19 20:45:30 +01:00
|
|
|
* @var RectorChangeCollector
|
2021-01-19 16:03:26 +01:00
|
|
|
*/
|
2021-01-19 20:45:30 +01:00
|
|
|
private $rectorChangeCollector;
|
2021-01-19 16:03:26 +01:00
|
|
|
|
2021-02-20 23:48:31 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, PhpDocInfo>
|
|
|
|
*/
|
|
|
|
private $phpDocInfosByObjectHash = [];
|
|
|
|
|
2019-02-28 22:50:53 +01:00
|
|
|
public function __construct(
|
2021-03-20 16:27:18 +01:00
|
|
|
PhpDocNodeMapper $phpDocNodeMapper,
|
2019-09-06 12:30:58 +02:00
|
|
|
CurrentNodeProvider $currentNodeProvider,
|
2020-07-26 09:49:22 +02:00
|
|
|
Lexer $lexer,
|
2020-10-11 12:40:45 +02:00
|
|
|
BetterPhpDocParser $betterPhpDocParser,
|
2021-01-19 16:03:26 +01:00
|
|
|
StaticTypeMapper $staticTypeMapper,
|
2021-01-19 20:45:30 +01:00
|
|
|
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;
|
2019-08-26 11:37:02 +02:00
|
|
|
$this->currentNodeProvider = $currentNodeProvider;
|
2019-09-06 12:30:58 +02:00
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
2021-03-20 16:27:18 +01:00
|
|
|
$this->phpDocNodeMapper = $phpDocNodeMapper;
|
2021-01-19 16:03:26 +01:00
|
|
|
$this->annotationNaming = $annotationNaming;
|
2021-01-19 20:45:30 +01:00
|
|
|
$this->rectorChangeCollector = $rectorChangeCollector;
|
2020-02-02 19:15:36 +01:00
|
|
|
}
|
|
|
|
|
2020-12-20 22:05:48 +01:00
|
|
|
public function createFromNodeOrEmpty(Node $node): PhpDocInfo
|
|
|
|
{
|
2021-01-18 21:06:02 +01:00
|
|
|
// already added
|
|
|
|
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
|
|
|
|
if ($phpDocInfo instanceof PhpDocInfo) {
|
|
|
|
return $phpDocInfo;
|
|
|
|
}
|
|
|
|
|
2020-12-20 22:05:48 +01:00
|
|
|
$phpDocInfo = $this->createFromNode($node);
|
2021-01-18 21:06:02 +01:00
|
|
|
if ($phpDocInfo instanceof PhpDocInfo) {
|
2020-12-20 22:05:48 +01:00
|
|
|
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
|
|
|
{
|
2021-02-20 23:48:31 +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);
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
$docComment = $node->getDocComment();
|
2021-01-20 18:41:35 +07:00
|
|
|
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 = '';
|
|
|
|
$tokens = [];
|
2021-03-20 16:27:18 +01:00
|
|
|
$phpDocNode = new PhpDocNode([]);
|
2020-01-30 22:11:29 +01:00
|
|
|
} else {
|
2020-11-16 17:50:38 +00:00
|
|
|
$content = $docComment->getText();
|
2020-01-30 22:11:29 +01:00
|
|
|
$tokens = $this->lexer->tokenize($content);
|
2021-02-20 23:48:31 +01:00
|
|
|
|
2021-04-04 11:01:11 +02:00
|
|
|
$phpDocNode = $this->parseTokensToPhpDocNode($tokens);
|
2020-02-02 19:15:36 +01:00
|
|
|
$this->setPositionOfLastToken($phpDocNode);
|
2019-09-18 12:22:12 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 23:48:31 +01:00
|
|
|
$phpDocInfo = $this->createFromPhpDocNode($phpDocNode, $content, $tokens, $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
|
|
|
|
2021-03-20 16:27:18 +01:00
|
|
|
$phpDocNode = new PhpDocNode([]);
|
2021-04-04 11:01:11 +02:00
|
|
|
$phpDocInfo = $this->createFromPhpDocNode($phpDocNode, '', [], $node);
|
|
|
|
|
|
|
|
// multiline by default
|
|
|
|
$phpDocInfo->makeMultiLined();
|
2021-04-06 19:33:09 +02:00
|
|
|
|
2021-04-04 11:01:11 +02:00
|
|
|
return $phpDocInfo;
|
2019-02-28 22:50:53 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 16:17:43 +02:00
|
|
|
/**
|
|
|
|
* @param mixed[][] $tokens
|
|
|
|
*/
|
2021-03-20 16:27:18 +01:00
|
|
|
private function parseTokensToPhpDocNode(array $tokens): PhpDocNode
|
2020-04-26 02:57:47 +02:00
|
|
|
{
|
2021-04-04 11:01:11 +02:00
|
|
|
$tokenIterator = new BetterTokenIterator($tokens);
|
2020-10-11 12:40:45 +02:00
|
|
|
return $this->betterPhpDocParser->parse($tokenIterator);
|
2020-04-26 02:57:47 +02:00
|
|
|
}
|
|
|
|
|
2019-02-28 22:50:53 +01:00
|
|
|
/**
|
|
|
|
* Needed for printing
|
|
|
|
*/
|
2021-03-20 16:27:18 +01:00
|
|
|
private function setPositionOfLastToken(PhpDocNode $phpDocNode): void
|
2020-02-02 19:15:36 +01:00
|
|
|
{
|
2021-03-20 16:27:18 +01:00
|
|
|
if ($phpDocNode->children === []) {
|
2020-02-02 19:15:36 +01:00
|
|
|
return;
|
2019-02-28 22:50:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 16:27:18 +01:00
|
|
|
$phpDocChildNodes = $phpDocNode->children;
|
2019-02-28 22:50:53 +01:00
|
|
|
$lastChildNode = array_pop($phpDocChildNodes);
|
|
|
|
|
2020-09-01 19:56:30 +02:00
|
|
|
/** @var StartAndEnd $startAndEnd */
|
2021-04-06 19:33:09 +02:00
|
|
|
$startAndEnd = $lastChildNode->getAttribute(PhpDocAttributeKey::START_AND_END);
|
2020-01-29 14:36:09 +01:00
|
|
|
|
2020-09-01 19:56:30 +02:00
|
|
|
if ($startAndEnd !== null) {
|
2021-04-06 19:33:09 +02:00
|
|
|
$phpDocNode->setAttribute(PhpDocAttributeKey::LAST_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
|
|
|
|
2020-10-11 16:17:43 +02:00
|
|
|
/**
|
|
|
|
* @param mixed[] $tokens
|
|
|
|
*/
|
2020-05-04 22:06:33 +02:00
|
|
|
private function createFromPhpDocNode(
|
2021-03-20 16:27:18 +01:00
|
|
|
PhpDocNode $phpDocNode,
|
2020-05-04 22:06:33 +02:00
|
|
|
string $content,
|
|
|
|
array $tokens,
|
|
|
|
Node $node
|
|
|
|
): PhpDocInfo {
|
2021-04-06 19:33:09 +02:00
|
|
|
$this->phpDocNodeMapper->transform($phpDocNode, new BetterTokenIterator($tokens));
|
2020-05-04 22:06:33 +02:00
|
|
|
|
|
|
|
$phpDocInfo = new PhpDocInfo(
|
2021-03-20 16:27:18 +01:00
|
|
|
$phpDocNode,
|
2020-05-04 22:06:33 +02:00
|
|
|
$tokens,
|
|
|
|
$content,
|
|
|
|
$this->staticTypeMapper,
|
|
|
|
$node,
|
2021-01-19 20:45:30 +01:00
|
|
|
$this->annotationNaming,
|
|
|
|
$this->currentNodeProvider,
|
|
|
|
$this->rectorChangeCollector
|
2020-05-04 22:06:33 +02:00
|
|
|
);
|
2020-10-11 16:17:43 +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
|
|
|
}
|