symfony/translation · error · LogicException

You cannot use " " as the "nikic/php-parser" package is not…

Error message

You cannot use "%s" as the "nikic/php-parser" package is not installed. Try running "composer require nikic/php-parser".

What it means

PhpAstExtractor relies on nikic/php-parser (specifically ParserFactory) to parse PHP files for translatable strings. Its constructor checks class_exists(ParserFactory::class) and throws LogicException if the package is not installed, telling you to run composer require nikic/php-parser.

Solutions

  1. Run: composer require nikic/php-parser
  2. If it exists only as a dev dependency, move it to the required section in composer.json
  3. Re-run composer install to regenerate the autoloader

Example fix

// before (shell)
composer install --no-dev
php bin/console translation:extract --dump-messages en
// after (shell)
composer require nikic/php-parser
php bin/console translation:extract --dump-messages en
Defensive patterns

Strategy: fallback

Validate before calling

if (!class_exists(\PhpParser\ParserFactory::class)) {
    // fall back to non-AST extractor or fail fast with a clear message
}

Type guard

function hasPhpParser(): bool {
    return class_exists(\PhpParser\ParserFactory::class);
}

Try / catch

try {
    $extractor = new PhpAstExtractor($visitors, $prefix);
} catch (\LogicException $e) {
    throw new \RuntimeException('Run: composer require nikic/php-parser', 0, $e);
}

Prevention

When it happens

Trigger: Instantiating PhpAstExtractor in a project where nikic/php-parser is not a dependency (e.g. it was a dev dependency or removed); running translation extraction in a production-only composer install without dev packages.

Common situations: composer install --no-dev in CI extracting translations; dependency pruning removed the parser; framework fork with fewer default requirements.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of symfony/translation@ae9e8a51bc (2026-09-15). Data as JSON: /api/errors/812764118f4fcff8. Report an issue: GitHub.

Appendix: source

Thrown at Extractor/PhpAstExtractor.php:39

/**
 * PhpAstExtractor extracts translation messages from a PHP AST.
 *
 * @author Mathieu Santostefano <msantostefano@protonmail.com>
 */
final class PhpAstExtractor extends AbstractFileExtractor implements ExtractorInterface
{
    private Parser $parser;

    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())->createForHostVersion();
    }

    public function extract(iterable|string $resource, MessageCatalogue $catalogue): void
    {
        foreach ($this->extractFiles($resource) as $file) {
            $traverser = new NodeTraverser();

            // This is needed to resolve namespaces in class methods/constants.
            $nameResolver = new NodeVisitor\NameResolver();
            $traverser->addVisitor($nameResolver);

            foreach ($this->visitors as $visitor) {
                $visitor->initialize($catalogue, $file, $this->prefix);
                $traverser->addVisitor($visitor);
            }

View on GitHub (pinned to ae9e8a51bc)