ParserFactory example

public function __construct(
        /** * @param iterable<AbstractVisitor&NodeVisitor> $visitors */
        private readonly iterable $visitors,
        private string $prefix = '',
    ) {
        if (!class_exists(ParserFactory::class)) {
            throw new \LogicException(sprintf('You cannot use "%s" as the "nikic/php-parser" package is not installed. Try running "composer require nikic/php-parser".', static::class));
        }

        $this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
    }

    public function extract(iterable|string $resource, MessageCatalogue $catalogue): void
    {
        foreach ($this->extractFiles($resource) as $file) {
            $traverser = new NodeTraverser();
            /** @var AbstractVisitor&NodeVisitor $visitor */
            foreach ($this->visitors as $visitor) {
                $visitor->initialize($catalogue$file$this->prefix);
                $traverser->addVisitor($visitor);
            }

            
private function resolveNames(array $stmts): array
    {
        $nameResolver = new NameResolver();
        $nodeTraverser = new NodeTraverser();
        $nodeTraverser->addVisitor($nameResolver);

        return $nodeTraverser->traverse($stmts);
    }

    private function parseFile(string $filePath): array
    {
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);

        return $this->resolveNames($parser->parse(file_get_contents($filePath)));
    }

    /** * @param class-string $entityClass */
    private function dumpProperties(string $entityClass, int $deep = 1): ?array
    {
        if ($deep === 3) {
            return null;
        }
Home | Imprint | This part of the site doesn't use cookies.