{"record":{"id":"9d74e4d77726deff","repo":"twigphp/Twig","slug":"trimming-side-must-be-left-right-or-both","errorCode":null,"errorMessage":"Trimming side must be \"left\", \"right\" or \"both\".","messagePattern":"Trimming side must be \"left\", \"right\" or \"both\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":1230,"sourceCode":"     * @param string|\\Stringable|null $string\n     * @param string|null             $characterMask\n     * @param string                  $side          left, right, or both\n     *\n     * @throws RuntimeError When an invalid trimming side is used\n     *\n     * @internal\n     */\n    public static function trim($string, $characterMask = null, $side = 'both'): string|\\Stringable\n    {\n        if (null === $characterMask) {\n            $characterMask = self::DEFAULT_TRIM_CHARS;\n        }\n\n        $trimmed = match ($side) {\n            'both' => trim($string ?? '', $characterMask),\n            'left' => ltrim($string ?? '', $characterMask),\n            'right' => rtrim($string ?? '', $characterMask),\n            default => throw new RuntimeError('Trimming side must be \"left\", \"right\" or \"both\".'),\n        };\n\n        // trimming a safe string with the default character mask always returns a safe string (independently of the context)\n        return $string instanceof Markup && self::DEFAULT_TRIM_CHARS === $characterMask ? new Markup($trimmed, $string->getCharset()) : $trimmed;\n    }\n\n    /**\n     * Inserts HTML line breaks before all newlines in a string.\n     *\n     * @param string|null $string\n     *\n     * @internal\n     */\n    public static function nl2br($string): string\n    {\n        return nl2br($string ?? '');\n    }\n","sourceCodeStart":1212,"sourceCodeEnd":1248,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1212-L1248","documentation":"Thrown by CoreExtension::trim when the `side` argument of Twig's `trim` filter is not one of 'left', 'right', or 'both'. The function uses a match expression and the default arm throws this RuntimeError for any other value. It protects users from silently getting no trimming when they misspell the side.","triggerScenarios":"Using the `trim` filter in a Twig template with a `side` argument that is misspelled, mis-translated, dynamically computed, or of the wrong type, e.g. `{{ name|trim('', 'lft') }}`, `{{ name|trim('', 'LEFT') }}`, or `{{ name|trim('', side) }}` where side is 'None'/'centre'/etc.","commonSituations":"Typo in template ('both' vs 'bot', 'left' vs 'lft'); case-sensitivity mistakes ('Left'); passing a Twig variable computed from config that holds an unexpected value; upgrading code that previously used custom filter semantics.","solutions":["Use exactly one of 'left', 'right', or 'both' (lowercase) as the side argument","If the side comes from a variable, normalize it first: side|lower, or default it with `side|default('both')`","Check the calling template or custom code in the caller list (getValue, htmlAttr, stripCommonIndentation, testMarkdown, compare) for a wrong literal","For dynamic values, validate in PHP before rendering or map unknown values to 'both'"],"exampleFix":"// before (Twig)\n{{ '  hello  '|trim(' ', 'Left') }}\n// after\n{{ '  hello  '|trim(' ', 'left') }}","handlingStrategy":"validation","validationCode":"$side = \\in_array($side, ['left', 'right', 'both'], true) ? $side : 'both';","typeGuard":null,"tryCatchPattern":"try {\n    $out = $twig->render('t.html', ['side' => $side]);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'Trimming side')) {\n        $out = $twig->render('t.html', ['side' => 'both']);\n    } else { throw $e; }\n}","preventionTips":["Only pass the exact lowercase literals 'left', 'right', 'both'","Normalize dynamic values with |lower and |default('both') in templates","Map user/config input through a whitelist before rendering","Review custom extensions (htmlAttr, compare, etc.) that call trim internally for correct literals"],"tags":["twig","argument-validation","trim","runtime-error"],"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"}