{"record":{"id":"0a08a1e8c3581378","repo":"twigphp/Twig","slug":"using-s-for-the-macro-s-of-s-is-not-supported-you-must-pass","errorCode":null,"errorMessage":"Using \"%s\" for the macro \"%s\" of \"%s\" is not supported. You must pass a \"%s\" instance.","messagePattern":"Using \"(.+?)\" for the macro \"(.+?)\" of \"(.+?)\" is not supported\\. You must pass a \"(.+?)\" instance\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Node/MacrosNode.php","lineNumber":34,"sourceCode":"\n/**\n * Represents the macros declared in a template.\n *\n * It compiles to the method returning the macro registry of the template.\n *\n * @author Fabien Potencier <fabien@symfony.com>\n */\n#[YieldReady]\nfinal class MacrosNode extends Node\n{\n    /**\n     * @param array<string, MacroNode> $macros\n     */\n    public function __construct(array $macros = [])\n    {\n        foreach ($macros as $name => $macro) {\n            if (!$macro instanceof MacroNode) {\n                throw new \\InvalidArgumentException(\\sprintf('Using \"%s\" for the macro \"%s\" of \"%s\" is not supported. You must pass a \"%s\" instance.', get_debug_type($macro), $name, static::class, MacroNode::class));\n            }\n        }\n\n        parent::__construct($macros);\n    }\n\n    public function setNode(string $name, Node $node): void\n    {\n        if (!$node instanceof MacroNode) {\n            throw new \\LogicException(\\sprintf('A \"%s\" can only contain \"%s\" nodes; replacing the macro \"%s\" with a \"%s\" node is not supported.', static::class, MacroNode::class, $name, get_debug_type($node)));\n        }\n\n        parent::setNode($name, $node);\n    }\n\n    public function compile(Compiler $compiler): void\n    {\n        if (!\\count($this)) {","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/MacrosNode.php#L16-L52","documentation":"MacrosNode's constructor validates that every entry in the $macros map is a MacroNode instance. Passing any other value (another Node type, a scalar, null) for a named macro slot is rejected with an InvalidArgumentException, because MacrosNode can only ever compile macro definitions.","triggerScenarios":"Constructing `new MacrosNode(['name' => $somethingNotMacroNode])` — typically custom compiler extensions or node visitors building macro containers programmatically and passing the wrong node type.","commonSituations":"Writing a Twig extension that manipulates the AST; refactoring code that used to put raw Nodes into a MacrosNode; accidental map mixing where a non-macro node lands in the macros array.","solutions":["Ensure every value passed in the $macros array is a \\Twig\\Node\\MacroNode instance.","If wrapping other content, use the appropriate Node container (e.g. Node or ModuleNode) instead of MacrosNode.","Log/var_dump get_debug_type($macro) at the failing key to find which entry is wrong."],"exampleFix":"// before\nnew \\Twig\\Node\\MacrosNode(['greet' => new \\Twig\\Node\\TextNode('hi')]);\n// after\nnew \\Twig\\Node\\MacrosNode(['greet' => new \\Twig\\Node\\MacroNode(...)]);","handlingStrategy":"type-guard","validationCode":"foreach ($macros as $name => $m) {\n    if (!$m instanceof \\Twig\\Node\\MacroNode) throw new InvalidArgumentException(\"$name is not a MacroNode\");\n}","typeGuard":"function isMacroMap(array $macros): bool {\n    return array_every($macros, fn($m) => $m instanceof \\Twig\\Node\\MacroNode);\n}\n// PHP has no array_every; use:\nfunction isMacroMap(array $macros): bool { foreach ($macros as $m) { if (!$m instanceof \\Twig\\Node\\MacroNode) return false; } return true; }","tryCatchPattern":"try {\n    $macrosNode = new \\Twig\\Node\\MacrosNode($macros);\n} catch (\\InvalidArgumentException $e) {\n    // log and fix the offending macro entry\n}","preventionTips":["Type-hint arrays with docblocks @param array<string, MacroNode>","Validate node maps before constructing AST containers","Avoid manually assembling MacrosNode unless writing extensions"],"tags":["twig","ast","macro"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}