Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use FileInfo in Parser #3198

Merged
merged 1 commit into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/file-system-rector/src/Parser/FileInfoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(Parser $parser, NodeScopeAndMetadataDecorator $nodeS
*/
public function parseFileInfoToNodesAndDecorate(SmartFileInfo $fileInfo): array
{
$oldStmts = $this->parser->parseFile($fileInfo->getRealPath());
$oldStmts = $this->parser->parseFileInfo($fileInfo);

return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($oldStmts, $fileInfo);
}
Expand All @@ -42,7 +42,7 @@ public function parseFileInfoToNodesAndDecorate(SmartFileInfo $fileInfo): array
*/
public function parseFileInfoToNodesAndDecorateWithScope(SmartFileInfo $fileInfo): array
{
$oldStmts = $this->parser->parseFile($fileInfo->getRealPath());
$oldStmts = $this->parser->parseFileInfo($fileInfo);

return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($oldStmts, $fileInfo, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function autowireAbstractFileSystemRector(
*/
protected function parseFileInfoToNodes(SmartFileInfo $smartFileInfo): array
{
$oldStmts = $this->parser->parseFile($smartFileInfo->getRealPath());
$oldStmts = $this->parser->parseFileInfo($smartFileInfo);
// needed for format preserving
$this->oldStmts = $oldStmts;
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($oldStmts, $smartFileInfo);
Expand All @@ -119,7 +119,7 @@ protected function parseFileInfoToNodes(SmartFileInfo $smartFileInfo): array
*/
protected function parseFileInfoToNodesWithoutScope(SmartFileInfo $smartFileInfo): array
{
$oldStmts = $this->parser->parseFile($smartFileInfo->getRealPath());
$oldStmts = $this->parser->parseFileInfo($smartFileInfo);
$this->oldStmts = $oldStmts;

return $oldStmts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function getNodesForFile(string $file): array

$this->parameterProvider->changeParameter(Option::SOURCE, [$file]);

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

return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($nodes, $smartFileInfo);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function refactor(SmartFileInfo $smartFileInfo): void
*/
private function parseAndTraverseFileInfoToNodes(SmartFileInfo $smartFileInfo): array
{
$oldStmts = $this->parser->parseFile($smartFileInfo->getRealPath());
$oldStmts = $this->parser->parseFileInfo($smartFileInfo);
$oldTokens = $this->lexer->getTokens();

// needed for \Rector\NodeTypeResolver\PHPStan\Scope\NodeScopeResolver
Expand Down
15 changes: 9 additions & 6 deletions src/PhpParser/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Parser as NikicParser;
use Symplify\SmartFileSystem\SmartFileInfo;

final class Parser
{
Expand All @@ -29,15 +30,17 @@ public function __construct(NikicParser $nikicParser)
/**
* @return Node[]
*/
public function parseFile(string $filePath): array
public function parseFileInfo(SmartFileInfo $smartFileInfo): array
{
if (isset($this->nodesByFile[$filePath])) {
return $this->nodesByFile[$filePath];
$fileRealPath = $smartFileInfo->getRealPath();

if (isset($this->nodesByFile[$fileRealPath])) {
return $this->nodesByFile[$fileRealPath];
}

$fileContent = FileSystem::read($filePath);
$this->nodesByFile[$filePath] = (array) $this->nikicParser->parse($fileContent);
$fileContent = FileSystem::read($fileRealPath);
$this->nodesByFile[$fileRealPath] = (array) $this->nikicParser->parse($fileContent);

return $this->nodesByFile[$filePath];
return $this->nodesByFile[$fileRealPath];
}
}
3 changes: 2 additions & 1 deletion src/Testing/PHPUnit/AbstractNodeVisitorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ protected function doTestFile(string $file): void
);
$smartFileInfo2 = new SmartFileInfo($expectedNodesFile);

$nodes = $this->parser->parseFile($originalFile);
$originalFileInfo = new SmartFileInfo($originalFile);
$nodes = $this->parser->parseFileInfo($originalFileInfo);

$this->visitNodes($nodes);

Expand Down