{"record":{"id":"812764118f4fcff8","repo":"symfony/translation","slug":"you-cannot-use-s-as-the-nikic-php-parser-package-is-not","errorCode":null,"errorMessage":"You cannot use \"%s\" as the \"nikic/php-parser\" package is not installed. Try running \"composer require nikic/php-parser\".","messagePattern":"You cannot use \"(.+?)\" as the \"nikic/php-parser\" package is not installed\\. Try running \"composer require nikic/php-parser\"\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"Extractor/PhpAstExtractor.php","lineNumber":39,"sourceCode":"\n/**\n * PhpAstExtractor extracts translation messages from a PHP AST.\n *\n * @author Mathieu Santostefano <msantostefano@protonmail.com>\n */\nfinal class PhpAstExtractor extends AbstractFileExtractor implements ExtractorInterface\n{\n    private Parser $parser;\n\n    public function __construct(\n        /**\n         * @param iterable<AbstractVisitor&NodeVisitor> $visitors\n         */\n        private readonly iterable $visitors,\n        private string $prefix = '',\n    ) {\n        if (!class_exists(ParserFactory::class)) {\n            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));\n        }\n\n        $this->parser = (new ParserFactory())->createForHostVersion();\n    }\n\n    public function extract(iterable|string $resource, MessageCatalogue $catalogue): void\n    {\n        foreach ($this->extractFiles($resource) as $file) {\n            $traverser = new NodeTraverser();\n\n            // This is needed to resolve namespaces in class methods/constants.\n            $nameResolver = new NodeVisitor\\NameResolver();\n            $traverser->addVisitor($nameResolver);\n\n            foreach ($this->visitors as $visitor) {\n                $visitor->initialize($catalogue, $file, $this->prefix);\n                $traverser->addVisitor($visitor);\n            }","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/symfony/translation/blob/ae9e8a51bc29780d63c7f9efd6005d7c875e100b/Extractor/PhpAstExtractor.php#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"composer install --no-dev in CI extracting translations; dependency pruning removed the parser; framework fork with fewer default requirements.","solutions":["Run: composer require nikic/php-parser","If it exists only as a dev dependency, move it to the required section in composer.json","Re-run composer install to regenerate the autoloader"],"exampleFix":"// before (shell)\ncomposer install --no-dev\nphp bin/console translation:extract --dump-messages en\n// after (shell)\ncomposer require nikic/php-parser\nphp bin/console translation:extract --dump-messages en","handlingStrategy":"fallback","validationCode":"if (!class_exists(\\PhpParser\\ParserFactory::class)) {\n    // fall back to non-AST extractor or fail fast with a clear message\n}","typeGuard":"function hasPhpParser(): bool {\n    return class_exists(\\PhpParser\\ParserFactory::class);\n}","tryCatchPattern":"try {\n    $extractor = new PhpAstExtractor($visitors, $prefix);\n} catch (\\LogicException $e) {\n    throw new \\RuntimeException('Run: composer require nikic/php-parser', 0, $e);\n}","preventionTips":["Add nikic/php-parser to composer.json require when using AST extraction","Run extraction in environments with a full composer install (not --no-dev)","Add a CI check that composer.json satisfies the extractor's suggested packages"],"tags":["php","composer","missing-dependency"],"backgroundTag":"missing-dependency","analyzedSha":"ae9e8a51bc29780d63c7f9efd6005d7c875e100b","analyzedAt":"2026-09-15T22:29:15.305Z","contentChangedAt":"2026-09-15T22:29:15.305Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}