2021-04-04 09:01:11 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-04-04 09:01:11 +00:00
|
|
|
namespace Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode;
|
|
|
|
use PHPStan\PhpDocParser\Lexer\Lexer;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser;
|
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
|
|
|
|
use Rector\Core\Configuration\CurrentNodeProvider;
|
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
final class PlainValueParser
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var StaticDoctrineAnnotationParser
|
|
|
|
*/
|
|
|
|
private $staticDoctrineAnnotationParser;
|
|
|
|
/**
|
|
|
|
* @var ClassAnnotationMatcher
|
|
|
|
*/
|
|
|
|
private $classAnnotationMatcher;
|
|
|
|
/**
|
|
|
|
* @var CurrentNodeProvider
|
|
|
|
*/
|
|
|
|
private $currentNodeProvider;
|
2021-04-13 18:51:41 +00:00
|
|
|
/**
|
|
|
|
* @var ArrayParser
|
|
|
|
*/
|
|
|
|
private $arrayParser;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher $classAnnotationMatcher, \Rector\Core\Configuration\CurrentNodeProvider $currentNodeProvider)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-04-04 09:01:11 +00:00
|
|
|
$this->classAnnotationMatcher = $classAnnotationMatcher;
|
|
|
|
$this->currentNodeProvider = $currentNodeProvider;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function autowirePlainValueParser(\Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser $staticDoctrineAnnotationParser, \Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\ArrayParser $arrayParser) : void
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-04-04 09:01:11 +00:00
|
|
|
$this->staticDoctrineAnnotationParser = $staticDoctrineAnnotationParser;
|
2021-04-13 18:51:41 +00:00
|
|
|
$this->arrayParser = $arrayParser;
|
2021-04-04 09:01:11 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return bool|int|mixed|string
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function parseValue(\Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator $tokenIterator)
|
2021-04-04 09:01:11 +00:00
|
|
|
{
|
|
|
|
$currentTokenValue = $tokenIterator->currentTokenValue();
|
|
|
|
// temporary hackaround multi-line doctrine annotations
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($tokenIterator->isCurrentTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_END)) {
|
2021-04-04 09:01:11 +00:00
|
|
|
return $currentTokenValue;
|
|
|
|
}
|
|
|
|
// consume the token
|
2021-05-10 22:23:08 +00:00
|
|
|
$isOpenCurlyArray = $tokenIterator->isCurrentTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_OPEN_CURLY_BRACKET);
|
2021-04-13 18:51:41 +00:00
|
|
|
if ($isOpenCurlyArray) {
|
|
|
|
return $this->arrayParser->parseCurlyArray($tokenIterator);
|
|
|
|
}
|
2021-04-04 09:01:11 +00:00
|
|
|
$tokenIterator->next();
|
|
|
|
// normalize value
|
|
|
|
if ($currentTokenValue === 'false') {
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode();
|
2021-04-04 09:01:11 +00:00
|
|
|
}
|
|
|
|
if ($currentTokenValue === 'true') {
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode();
|
2021-04-04 09:01:11 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\is_numeric($currentTokenValue) && (string) (int) $currentTokenValue === $currentTokenValue) {
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode($currentTokenValue);
|
2021-04-04 09:01:11 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
while ($tokenIterator->isCurrentTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_DOUBLE_COLON) || $tokenIterator->isCurrentTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_IDENTIFIER)) {
|
2021-04-04 09:01:11 +00:00
|
|
|
$currentTokenValue .= $tokenIterator->currentTokenValue();
|
|
|
|
$tokenIterator->next();
|
|
|
|
}
|
|
|
|
// nested entity!
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($tokenIterator->isCurrentTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_OPEN_PARENTHESES)) {
|
2021-04-13 18:51:41 +00:00
|
|
|
return $this->parseNestedDoctrineAnnotationTagValueNode($currentTokenValue, $tokenIterator);
|
|
|
|
}
|
|
|
|
return $currentTokenValue;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function parseNestedDoctrineAnnotationTagValueNode(string $currentTokenValue, \Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator $tokenIterator) : \Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-04-13 18:51:41 +00:00
|
|
|
// @todo
|
|
|
|
$annotationShortName = $currentTokenValue;
|
|
|
|
$values = $this->staticDoctrineAnnotationParser->resolveAnnotationMethodCall($tokenIterator);
|
|
|
|
$currentNode = $this->currentNodeProvider->getNode();
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$currentNode instanceof \PhpParser\Node) {
|
|
|
|
throw new \Rector\Core\Exception\ShouldNotHappenException();
|
2021-04-13 18:51:41 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$fullyQualifiedAnnotationClass = $this->classAnnotationMatcher->resolveTagFullyQualifiedName($annotationShortName, $currentNode);
|
2021-04-13 18:51:41 +00:00
|
|
|
// keep the last ")"
|
2021-05-10 22:23:08 +00:00
|
|
|
$tokenIterator->tryConsumeTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_PHPDOC_EOL);
|
|
|
|
$tokenIterator->consumeTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_CLOSE_PARENTHESES);
|
|
|
|
return new \Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode($fullyQualifiedAnnotationClass, $annotationShortName, $values);
|
2021-04-04 09:01:11 +00:00
|
|
|
}
|
|
|
|
}
|