rector/src/PhpParser/Parser/SimplePhpParser.php
Tomas Votruba 727b9f46f0 Updated Rector to commit bfa1891c50677b01136a9308fd3c3ecc12e267d9
bfa1891c50 [cleanup] Remove 73 unused public methods (#3245)
2022-12-23 17:10:25 +00:00

47 lines
1.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Core\PhpParser\Parser;
use RectorPrefix202212\Nette\Utils\FileSystem;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use PhpParser\Parser;
use PhpParser\ParserFactory;
final class SimplePhpParser
{
/**
* @readonly
* @var \PhpParser\Parser
*/
private $phpParser;
public function __construct()
{
$parserFactory = new ParserFactory();
$this->phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7);
}
/**
* @api tests
* @return Stmt[]
*/
public function parseFile(string $filePath) : array
{
$fileContent = FileSystem::read($filePath);
return $this->parseString($fileContent);
}
/**
* @return Stmt[]
*/
public function parseString(string $fileContent) : array
{
$stmts = $this->phpParser->parse($fileContent);
if ($stmts === null) {
return [];
}
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new NodeConnectingVisitor());
return $nodeTraverser->traverse($stmts);
}
}