for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace ComposerRequireChecker\ASTLocator;
use PhpParser\ErrorHandler;
use PhpParser\Node\Stmt;
use PhpParser\Parser;
use RuntimeException;
use Traversable;
use function sprintf;
final class LocateASTFromFiles
{
/**
* @var Parser
*/
private $parser;
* @var ErrorHandler
private $errorHandler;
public function __construct(Parser $parser, ?ErrorHandler $errorHandler)
$this->parser = $parser;
$this->errorHandler = $errorHandler;
}
* @param Traversable<string> $files
*
* @return Traversable<int, array<Stmt>> a series of AST roots, one for each given file
public function __invoke(Traversable $files): Traversable
foreach ($files as $file) {
$stmts = $this->parser->parse(file_get_contents($file), $this->errorHandler);
if ($stmts === null) {
throw new RuntimeException(sprintf("Parsing the file [%s] resulted in an error.", $file));
yield $stmts;