2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2019-02-28 21:50:53 +00:00
|
|
|
namespace Rector\BetterPhpDocParser\PhpDocParser;
|
|
|
|
|
2021-03-21 21:31:18 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
|
2019-02-28 21:50:53 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
|
2019-08-26 09:37:02 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
2019-02-28 21:50:53 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
|
2021-04-06 17:33:09 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
|
2019-02-28 21:50:53 +00:00
|
|
|
use PHPStan\PhpDocParser\Lexer\Lexer;
|
|
|
|
use PHPStan\PhpDocParser\Parser\ConstExprParser;
|
|
|
|
use PHPStan\PhpDocParser\Parser\PhpDocParser;
|
|
|
|
use PHPStan\PhpDocParser\Parser\TokenIterator;
|
|
|
|
use PHPStan\PhpDocParser\Parser\TypeParser;
|
2021-04-04 09:01:11 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
|
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
|
2021-04-06 17:33:09 +00:00
|
|
|
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
|
2020-09-01 17:56:30 +00:00
|
|
|
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
|
2021-08-08 00:26:40 +00:00
|
|
|
use RectorPrefix20210808\Symplify\PackageBuilder\Reflection\PrivatesCaller;
|
2019-09-26 14:26:44 +00:00
|
|
|
/**
|
2021-03-12 19:13:27 +00:00
|
|
|
* @see \Rector\Tests\BetterPhpDocParser\PhpDocParser\TagValueNodeReprint\TagValueNodeReprintTest
|
2019-09-26 14:26:44 +00:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
final class BetterPhpDocParser extends \PHPStan\PhpDocParser\Parser\PhpDocParser
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-05-12 13:15:45 +00:00
|
|
|
* @var \Symplify\PackageBuilder\Reflection\PrivatesCaller
|
2019-02-28 21:50:53 +00:00
|
|
|
*/
|
|
|
|
private $privatesCaller;
|
2020-02-29 18:57:31 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory
|
2020-02-29 18:57:31 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $tokenIteratorFactory;
|
2020-05-05 14:37:29 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator
|
2020-05-05 14:37:29 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $doctrineAnnotationDecorator;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\PHPStan\PhpDocParser\Parser\TypeParser $typeParser, \PHPStan\PhpDocParser\Parser\ConstExprParser $constExprParser, \Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory $tokenIteratorFactory, \Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator $doctrineAnnotationDecorator)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-05-10 23:39:21 +00:00
|
|
|
$this->tokenIteratorFactory = $tokenIteratorFactory;
|
|
|
|
$this->doctrineAnnotationDecorator = $doctrineAnnotationDecorator;
|
2019-02-28 21:50:53 +00:00
|
|
|
parent::__construct($typeParser, $constExprParser);
|
2021-08-08 00:26:40 +00:00
|
|
|
$this->privatesCaller = new \RectorPrefix20210808\Symplify\PackageBuilder\Reflection\PrivatesCaller();
|
2020-01-03 22:44:40 +00:00
|
|
|
}
|
2021-07-05 22:50:18 +00:00
|
|
|
/**
|
|
|
|
* @param \PHPStan\PhpDocParser\Parser\TokenIterator $tokenIterator
|
|
|
|
*/
|
|
|
|
public function parse($tokenIterator) : \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$tokenIterator->consumeTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_OPEN_PHPDOC);
|
|
|
|
$tokenIterator->tryConsumeTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_PHPDOC_EOL);
|
2019-02-28 21:50:53 +00:00
|
|
|
$children = [];
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$tokenIterator->isCurrentTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_CLOSE_PHPDOC)) {
|
2019-02-28 21:50:53 +00:00
|
|
|
$children[] = $this->parseChildAndStoreItsPositions($tokenIterator);
|
2021-05-10 22:23:08 +00:00
|
|
|
while ($tokenIterator->tryConsumeTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_PHPDOC_EOL) && !$tokenIterator->isCurrentTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_CLOSE_PHPDOC)) {
|
2019-02-28 21:50:53 +00:00
|
|
|
$children[] = $this->parseChildAndStoreItsPositions($tokenIterator);
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 22:17:31 +00:00
|
|
|
// might be in the middle of annotations
|
2021-05-10 22:23:08 +00:00
|
|
|
$tokenIterator->tryConsumeTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_CLOSE_PHPDOC);
|
|
|
|
$phpDocNode = new \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode($children);
|
2021-04-04 09:01:11 +00:00
|
|
|
// replace generic nodes with DoctrineAnnotations
|
|
|
|
$this->doctrineAnnotationDecorator->decorate($phpDocNode);
|
|
|
|
return $phpDocNode;
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|
2021-04-04 09:01:11 +00:00
|
|
|
/**
|
2021-07-05 22:50:18 +00:00
|
|
|
* @param \PHPStan\PhpDocParser\Parser\TokenIterator $tokenIterator
|
2021-04-04 09:01:11 +00:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function parseTag($tokenIterator) : \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode
|
2019-08-26 09:37:02 +00:00
|
|
|
{
|
2019-10-02 19:18:45 +00:00
|
|
|
$tag = $this->resolveTag($tokenIterator);
|
2021-01-30 15:06:43 +00:00
|
|
|
$phpDocTagValueNode = $this->parseTagValue($tokenIterator, $tag);
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode($tag, $phpDocTagValueNode);
|
2019-08-26 09:37:02 +00:00
|
|
|
}
|
2021-04-04 09:01:11 +00:00
|
|
|
/**
|
2021-07-05 22:50:18 +00:00
|
|
|
* @param \PHPStan\PhpDocParser\Parser\TokenIterator $tokenIterator
|
|
|
|
* @param string $tag
|
2021-04-04 09:01:11 +00:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function parseTagValue($tokenIterator, $tag) : \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
2021-04-06 17:33:09 +00:00
|
|
|
$startPosition = $tokenIterator->currentPosition();
|
2021-04-04 09:01:11 +00:00
|
|
|
$tagValueNode = parent::parseTagValue($tokenIterator, $tag);
|
2021-04-06 17:33:09 +00:00
|
|
|
$endPosition = $tokenIterator->currentPosition();
|
2021-05-10 22:23:08 +00:00
|
|
|
$startAndEnd = new \Rector\BetterPhpDocParser\ValueObject\StartAndEnd($startPosition, $endPosition);
|
|
|
|
$tagValueNode->setAttribute(\Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey::START_AND_END, $startAndEnd);
|
2021-04-06 17:33:09 +00:00
|
|
|
return $tagValueNode;
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2021-05-12 13:15:45 +00:00
|
|
|
/**
|
|
|
|
* @return PhpDocTextNode|PhpDocTagNode
|
|
|
|
*/
|
2021-07-05 23:06:30 +00:00
|
|
|
private function parseChildAndStoreItsPositions(\PHPStan\PhpDocParser\Parser\TokenIterator $tokenIterator) : \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
2021-04-06 17:33:09 +00:00
|
|
|
$betterTokenIterator = $this->tokenIteratorFactory->createFromTokenIterator($tokenIterator);
|
|
|
|
$startPosition = $betterTokenIterator->currentPosition();
|
2021-05-12 13:15:45 +00:00
|
|
|
/** @var PhpDocChildNode $phpDocNode */
|
2021-04-06 17:33:09 +00:00
|
|
|
$phpDocNode = $this->privatesCaller->callPrivateMethod($this, 'parseChild', [$betterTokenIterator]);
|
|
|
|
$endPosition = $betterTokenIterator->currentPosition();
|
2021-05-10 22:23:08 +00:00
|
|
|
$startAndEnd = new \Rector\BetterPhpDocParser\ValueObject\StartAndEnd($startPosition, $endPosition);
|
|
|
|
$phpDocNode->setAttribute(\Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey::START_AND_END, $startAndEnd);
|
2021-04-06 17:33:09 +00:00
|
|
|
return $phpDocNode;
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|
2021-07-05 23:06:30 +00:00
|
|
|
private function resolveTag(\Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator $tokenIterator) : string
|
2019-10-02 19:18:45 +00:00
|
|
|
{
|
|
|
|
$tag = $tokenIterator->currentTokenValue();
|
|
|
|
$tokenIterator->next();
|
2021-04-04 09:01:11 +00:00
|
|
|
// there is a space → stop
|
|
|
|
if ($tokenIterator->isPrecededByHorizontalWhitespace()) {
|
2019-10-04 12:55:26 +00:00
|
|
|
return $tag;
|
|
|
|
}
|
2019-10-02 19:18:45 +00:00
|
|
|
// is not e.g "@var "
|
|
|
|
// join tags like "@ORM\Column" etc.
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$tokenIterator->isCurrentTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_IDENTIFIER)) {
|
2019-10-02 19:18:45 +00:00
|
|
|
return $tag;
|
|
|
|
}
|
2021-04-04 09:01:11 +00:00
|
|
|
// @todo use joinUntil("(")?
|
2019-10-02 19:18:45 +00:00
|
|
|
$tag .= $tokenIterator->currentTokenValue();
|
|
|
|
$tokenIterator->next();
|
|
|
|
return $tag;
|
|
|
|
}
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|