mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-14 15:12:23 +00:00
7ba32aac1f
e12c73eb33
[psr-4] Move tests to main namespace, as part of /src and /packages merge - step 2 (#5407)
73 lines
2.8 KiB
PHP
73 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Testing\TestingParser;
|
|
|
|
use RectorPrefix202401\Nette\Utils\FileSystem;
|
|
use PhpParser\Node;
|
|
use Rector\Core\PhpParser\Parser\RectorParser;
|
|
use Rector\Core\Provider\CurrentFileProvider;
|
|
use Rector\Core\ValueObject\Application\File;
|
|
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
|
|
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
|
|
/**
|
|
* @api
|
|
*/
|
|
final class TestingParser
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Core\PhpParser\Parser\RectorParser
|
|
*/
|
|
private $rectorParser;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator
|
|
*/
|
|
private $nodeScopeAndMetadataDecorator;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Core\Provider\CurrentFileProvider
|
|
*/
|
|
private $currentFileProvider;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider
|
|
*/
|
|
private $dynamicSourceLocatorProvider;
|
|
public function __construct(RectorParser $rectorParser, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, CurrentFileProvider $currentFileProvider, DynamicSourceLocatorProvider $dynamicSourceLocatorProvider)
|
|
{
|
|
$this->rectorParser = $rectorParser;
|
|
$this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator;
|
|
$this->currentFileProvider = $currentFileProvider;
|
|
$this->dynamicSourceLocatorProvider = $dynamicSourceLocatorProvider;
|
|
}
|
|
public function parseFilePathToFile(string $filePath) : File
|
|
{
|
|
// needed for PHPStan reflection, as it caches the last processed file
|
|
$this->dynamicSourceLocatorProvider->setFilePath($filePath);
|
|
$fileContent = FileSystem::read($filePath);
|
|
$file = new File($filePath, $fileContent);
|
|
$stmts = $this->rectorParser->parseString($fileContent);
|
|
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($filePath, $stmts);
|
|
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
|
|
$this->currentFileProvider->setFile($file);
|
|
return $file;
|
|
}
|
|
/**
|
|
* @return Node[]
|
|
*/
|
|
public function parseFileToDecoratedNodes(string $filePath) : array
|
|
{
|
|
// needed for PHPStan reflection, as it caches the last processed file
|
|
$this->dynamicSourceLocatorProvider->setFilePath($filePath);
|
|
$fileContent = FileSystem::read($filePath);
|
|
$stmts = $this->rectorParser->parseString($fileContent);
|
|
$file = new File($filePath, $fileContent);
|
|
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($filePath, $stmts);
|
|
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
|
|
$this->currentFileProvider->setFile($file);
|
|
return $stmts;
|
|
}
|
|
}
|