{"record":{"id":"7858e0ad09593b39","repo":"twigphp/Twig","slug":"optimizer-mode-s-is-not-valid","errorCode":null,"errorMessage":"Optimizer mode \"%s\" is not valid.","messagePattern":"Optimizer mode \"(.+?)\" is not valid\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/NodeVisitor/OptimizerNodeVisitor.php","lineNumber":58,"sourceCode":"final class OptimizerNodeVisitor implements NodeVisitorInterface\n{\n    public const OPTIMIZE_ALL = -1;\n    public const OPTIMIZE_NONE = 0;\n    public const OPTIMIZE_FOR = 2;\n    public const OPTIMIZE_RAW_FILTER = 4;\n    public const OPTIMIZE_TEXT_NODES = 8;\n\n    private $loops = [];\n    private $loopsTargets = [];\n\n    /**\n     * @param int $optimizers The optimizer mode\n     */\n    public function __construct(\n        private int $optimizers = -1,\n    ) {\n        if ($optimizers > (self::OPTIMIZE_FOR | self::OPTIMIZE_RAW_FILTER | self::OPTIMIZE_TEXT_NODES)) {\n            throw new \\InvalidArgumentException(\\sprintf('Optimizer mode \"%s\" is not valid.', $optimizers));\n        }\n\n        if (-1 !== $optimizers && self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $optimizers)) {\n            trigger_deprecation('twig/twig', '3.11', 'The \"Twig\\NodeVisitor\\OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER\" option is deprecated and does nothing.');\n        }\n\n        if (-1 !== $optimizers && self::OPTIMIZE_TEXT_NODES === (self::OPTIMIZE_TEXT_NODES & $optimizers)) {\n            trigger_deprecation('twig/twig', '3.12', 'The \"Twig\\NodeVisitor\\OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES\" option is deprecated and does nothing.');\n        }\n    }\n\n    public function enterNode(Node $node, Environment $env): Node\n    {\n        if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {\n            $this->enterOptimizeFor($node);\n        }\n\n        return $node;","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/NodeVisitor/OptimizerNodeVisitor.php#L40-L76","documentation":"OptimizerNodeVisitor's constructor validates that the $optimizers bitmask contains no bits beyond the known modes OPTIMIZE_FOR, OPTIMIZE_RAW_FILTER, and OPTIMIZE_TEXT_NODES. Passing a larger integer is an InvalidArgumentException because the mode flag is unknown to this Twig version. Note that -1 (the default, meaning \"all\") and any combination of valid bits are accepted.","triggerScenarios":"new OptimizerNodeVisitor(4) or any value greater than OPTIMIZE_FOR|OPTIMIZE_RAW_FILTER|OPTIMIZE_TEXT_NODES (currently 1|4|8 = 13, so >=14 invalid); passing flags copied from another library or hand-invented bit values; passing 2 in newer Twig where OPTIMIZE_RAW_FILTER was removed from valid composition.","commonSituations":"Custom Twig environment factory configuring the optimizer with an invalid bitmask literal (e.g. old constant values from Twig 1.x); copy-pasted configuration from another optimizer library; a config file value cast into the constructor without validation.","solutions":["Use the class constants only: new OptimizerNodeVisitor(OptimizerNodeVisitor::OPTIMIZE_FOR | OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES).","Pass -1 (or no argument) to enable all optimizations.","Remove stale constant values from Twig 1.x/2.x configs; OPTIMIZE_RAW_FILTER is deprecated (3.11) and other numeric values no longer match.","Validate any config-sourced integer against the valid bitmask before constructing."],"exampleFix":"// before\n$env->addNodeVisitor(new \\Twig\\NodeVisitor\\OptimizerNodeVisitor(7));\n// after\n$env->addNodeVisitor(new \\Twig\\NodeVisitor\\OptimizerNodeVisitor(\n    \\Twig\\NodeVisitor\\OptimizerNodeVisitor::OPTIMIZE_FOR\n    | \\Twig\\NodeVisitor\\OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES\n));","handlingStrategy":"validation","validationCode":"$valid = -1 | OptimizerNodeVisitor::OPTIMIZE_FOR | OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER | OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES;\n$mask = OptimizerNodeVisitor::OPTIMIZE_FOR | OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER | OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES;\nif ($mode !== -1 && ($mode < 0 || $mode > $mask)) {\n    throw new \\InvalidArgumentException(\"Invalid optimizer mode: $mode\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    $visitor = new OptimizerNodeVisitor($mode);\n} catch (\\InvalidArgumentException $e) {\n    $visitor = new OptimizerNodeVisitor(); // fall back to default (all)\n}","preventionTips":["Only compose the mode from the class's own constants, never raw integers.","Use -1 or omit the argument to enable all optimizations.","Audit configs migrated from Twig 1.x/2.x for stale constant values."],"tags":["twig","optimizer","bitmask","invalid-argument"],"backgroundTag":"invalid-argument-value","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"}