Skip to content

Commit

Permalink
Improve stmts and tokens context in Parser (#1061)
Browse files Browse the repository at this point in the history
* rename Parser to RectorParser to avoid confusion

* add StmtsAndTokens value object to keep context in one place

* typo

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Oct 25, 2021
1 parent 48cb8a2 commit 877cf17
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 21 deletions.
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
__DIR__ . '/../src/Bootstrap',
__DIR__ . '/../src/Enum',
__DIR__ . '/../src/PhpParser/Node/CustomNode',
__DIR__ . '/../src/PhpParser/ValueObject',
__DIR__ . '/../src/functions',
__DIR__ . '/../src/constants.php',
__DIR__ . '/../src/PhpParser/NodeVisitor/CreatedByRuleNodeVisitor.php',
Expand Down
8 changes: 4 additions & 4 deletions packages/FileSystemRector/Parser/FileInfoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\FileSystemRector\Parser;

use PhpParser\Node;
use Rector\Core\PhpParser\Parser\Parser;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Symplify\SmartFileSystem\SmartFileInfo;
Expand All @@ -14,7 +14,7 @@ final class FileInfoParser
{
public function __construct(
private NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
private Parser $parser
private RectorParser $rectorParser
) {
}

Expand All @@ -23,9 +23,9 @@ public function __construct(
*/
public function parseFileInfoToNodesAndDecorate(SmartFileInfo $smartFileInfo): array
{
$oldStmts = $this->parser->parseFileInfo($smartFileInfo);
$stmts = $this->rectorParser->parseFile($smartFileInfo);
$file = new File($smartFileInfo, $smartFileInfo->getContents());

return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $oldStmts);
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts);
}
}
6 changes: 3 additions & 3 deletions packages/Testing/TestingParser/TestingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node;
use Rector\Core\Configuration\Option;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Parser\Parser;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
Expand All @@ -17,7 +17,7 @@ final class TestingParser
{
public function __construct(
private ParameterProvider $parameterProvider,
private Parser $parser,
private RectorParser $rectorParser,
private NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
private BetterNodeFinder $betterNodeFinder
) {
Expand All @@ -34,7 +34,7 @@ public function parseFileToDecoratedNodes(string $file): array
$smartFileInfo = new SmartFileInfo($file);
$this->parameterProvider->changeParameter(Option::SOURCE, [$file]);

$nodes = $this->parser->parseFileInfo($smartFileInfo);
$nodes = $this->rectorParser->parseFile($smartFileInfo);

$file = new File($smartFileInfo, $smartFileInfo->getContents());
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $nodes);
Expand Down
2 changes: 1 addition & 1 deletion packages/VersionBonding/PhpVersionedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function filter(array $rectors): array
continue;
}

// does satify version? → include
// does satisfy version? → include
if ($rector->provideMinPhpVersion() <= $minProjectPhpVersion) {
$activeRectors[] = $rector;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Application/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
namespace Rector\Core\Application;

use Nette\Utils\Strings;
use PhpParser\Lexer;
use PhpParser\Node;
use Rector\ChangesReporting\Collector\AffectedFilesCollector;
use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser;
use Rector\Core\PhpParser\Parser\Parser;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
Expand All @@ -24,9 +23,8 @@ final class FileProcessor

public function __construct(
private AffectedFilesCollector $affectedFilesCollector,
private Lexer $lexer,
private NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
private Parser $parser,
private RectorParser $rectorParser,
private RectorNodeTraverser $rectorNodeTraverser,
private BetterStandardPrinter $betterStandardPrinter
) {
Expand All @@ -36,8 +34,10 @@ public function parseFileInfoToLocalCache(File $file): void
{
// store tokens by absolute path, so we don't have to print them right now
$smartFileInfo = $file->getSmartFileInfo();
$oldStmts = $this->parser->parseFileInfo($smartFileInfo);
$oldTokens = $this->lexer->getTokens();
$stmtsAndTokens = $this->rectorParser->parseFileToStmtsAndTokens($smartFileInfo);

$oldStmts = $stmtsAndTokens->getStmts();
$oldTokens = $stmtsAndTokens->getTokens();

/**
* Tweak PHPStan internal issue for has @template-extends that cause endless loop in the process
Expand Down
2 changes: 1 addition & 1 deletion src/PhpParser/Parser/NikicPhpParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
final class NikicPhpParserFactory
{
public function __construct(
private ParserFactory $parserFactory,
private Lexer $lexer,
private ParserFactory $parserFactory
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@

namespace Rector\Core\PhpParser\Parser;

use PhpParser\Lexer;
use PhpParser\Node\Stmt;
use PhpParser\Parser as NikicParser;
use PhpParser\Parser;
use Rector\Core\PhpParser\ValueObject\StmtsAndTokens;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

final class Parser
final class RectorParser
{
/**
* @var array<string, Stmt[]>
*/
private array $nodesByFile = [];

public function __construct(
private NikicParser $nikicParser,
private SmartFileSystem $smartFileSystem
private Parser $parser,
private SmartFileSystem $smartFileSystem,
private Lexer $lexer
) {
}

/**
* @return Stmt[]
*/
public function parseFileInfo(SmartFileInfo $smartFileInfo): array
public function parseFile(SmartFileInfo $smartFileInfo): array
{
$fileRealPath = $smartFileInfo->getRealPath();

Expand All @@ -35,12 +38,20 @@ public function parseFileInfo(SmartFileInfo $smartFileInfo): array

$fileContent = $this->smartFileSystem->readFile($fileRealPath);

$nodes = $this->nikicParser->parse($fileContent);
$nodes = $this->parser->parse($fileContent);
if ($nodes === null) {
$nodes = [];
}

$this->nodesByFile[$fileRealPath] = $nodes;
return $this->nodesByFile[$fileRealPath];
}

public function parseFileToStmtsAndTokens(SmartFileInfo $smartFileInfo): StmtsAndTokens
{
$stmts = $this->parseFile($smartFileInfo);
$tokens = $this->lexer->getTokens();

return new StmtsAndTokens($stmts, $tokens);
}
}
36 changes: 36 additions & 0 deletions src/PhpParser/ValueObject/StmtsAndTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Rector\Core\PhpParser\ValueObject;

use PhpParser\Node\Stmt;

final class StmtsAndTokens
{
/**
* @param Stmt[] $stmts
* @param mixed[] $tokens
*/
public function __construct(
private array $stmts,
private array $tokens
) {
}

/**
* @return Stmt[]
*/
public function getStmts(): array
{
return $this->stmts;
}

/**
* @return mixed[]
*/
public function getTokens(): array
{
return $this->tokens;
}
}

0 comments on commit 877cf17

Please sign in to comment.