Skip to content

Commit

Permalink
Make BetterPhpDocParser parse node directly (#4981)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 11, 2023
1 parent 57ec646 commit 0bb2398
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function createFromNode(Node $node): ?PhpDocInfo
$tokens = $this->lexer->tokenize($docComment->getText());
$tokenIterator = new BetterTokenIterator($tokens);

$phpDocNode = $this->betterPhpDocParser->parse($tokenIterator);
$phpDocNode = $this->betterPhpDocParser->parseWithNode($tokenIterator, $node);
$this->setPositionOfLastToken($phpDocNode);
}

Expand Down
29 changes: 10 additions & 19 deletions packages/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Util\Reflection\PrivatesAccessor;

Expand All @@ -35,7 +34,6 @@ final class BetterPhpDocParser extends PhpDocParser
public function __construct(
TypeParser $typeParser,
ConstExprParser $constExprParser,
private readonly CurrentNodeProvider $currentNodeProvider,
private readonly TokenIteratorFactory $tokenIteratorFactory,
private readonly array $phpDocNodeDecorators,
private readonly PrivatesAccessor $privatesAccessor = new PrivatesAccessor(),
Expand All @@ -61,33 +59,26 @@ public function __construct(
);
}

public function parse(TokenIterator $tokenIterator): PhpDocNode
public function parseWithNode(BetterTokenIterator $betterTokenIterator, Node $node): PhpDocNode
{
$tokenIterator->consumeTokenType(Lexer::TOKEN_OPEN_PHPDOC);
$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$betterTokenIterator->consumeTokenType(Lexer::TOKEN_OPEN_PHPDOC);
$betterTokenIterator->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);

$children = [];
if (! $tokenIterator->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
$children[] = $this->parseChildAndStoreItsPositions($tokenIterator);
if (! $betterTokenIterator->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
$children[] = $this->parseChildAndStoreItsPositions($betterTokenIterator);

while ($tokenIterator->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL) && ! $tokenIterator->isCurrentTokenType(
Lexer::TOKEN_CLOSE_PHPDOC
)) {
$children[] = $this->parseChildAndStoreItsPositions($tokenIterator);
while ($betterTokenIterator->tryConsumeTokenType(
Lexer::TOKEN_PHPDOC_EOL
) && ! $betterTokenIterator->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
$children[] = $this->parseChildAndStoreItsPositions($betterTokenIterator);
}
}

// might be in the middle of annotations
$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);
$betterTokenIterator->tryConsumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);

$phpDocNode = new PhpDocNode($children);

// decorate FQN classes etc.
$node = $this->currentNodeProvider->getNode();
if (! $node instanceof Node) {
throw new ShouldNotHappenException();
}

foreach ($this->phpDocNodeDecorators as $phpDocNodeDecorator) {
$phpDocNodeDecorator->decorate($phpDocNode, $node);
}
Expand Down
2 changes: 0 additions & 2 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
use Rector\Core\Application\ChangedNodeScopeRefresher;
use Rector\Core\Application\FileProcessor;
use Rector\Core\Configuration\ConfigInitializer;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Console\Command\ListRulesCommand;
use Rector\Core\Console\Command\ProcessCommand;
Expand Down Expand Up @@ -506,7 +505,6 @@ static function (AbstractRector $rector, Container $container): void {
$container->make(PhpDocInfoFactory::class),
$container->make(StaticTypeMapper::class),
$container->make(CurrentRectorProvider::class),
$container->make(CurrentNodeProvider::class),
$container->make(Skipper::class),
$container->make(ValueResolver::class),
$container->make(BetterNodeFinder::class),
Expand Down
8 changes: 0 additions & 8 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Application\ChangedNodeScopeRefresher;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Logging\CurrentRectorProvider;
Expand Down Expand Up @@ -74,8 +73,6 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter

private CurrentRectorProvider $currentRectorProvider;

private CurrentNodeProvider $currentNodeProvider;

private Skipper $skipper;

private CurrentFileProvider $currentFileProvider;
Expand All @@ -97,7 +94,6 @@ public function autowire(
PhpDocInfoFactory $phpDocInfoFactory,
StaticTypeMapper $staticTypeMapper,
CurrentRectorProvider $currentRectorProvider,
CurrentNodeProvider $currentNodeProvider,
Skipper $skipper,
ValueResolver $valueResolver,
BetterNodeFinder $betterNodeFinder,
Expand All @@ -113,7 +109,6 @@ public function autowire(
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->staticTypeMapper = $staticTypeMapper;
$this->currentRectorProvider = $currentRectorProvider;
$this->currentNodeProvider = $currentNodeProvider;
$this->skipper = $skipper;
$this->valueResolver = $valueResolver;
$this->betterNodeFinder = $betterNodeFinder;
Expand Down Expand Up @@ -153,9 +148,6 @@ final public function enterNode(Node $node): int|Node|null
}

$this->currentRectorProvider->changeCurrentRector($this);
// for PHP doc info factory and change notifier
$this->currentNodeProvider->setNode($node);

$this->changedNodeScopeRefresher->reIndexNodeAttributes($node);

// ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
Expand Down

0 comments on commit 0bb2398

Please sign in to comment.