{"record":{"id":"638069b8843022b8","repo":"twigphp/Twig","slug":"the-html-classes-function-argument-d-key-d-should-be-a","errorCode":null,"errorMessage":"The \"html_classes\" function argument %d (key %d) should be a string, got \"%s\".","messagePattern":"The \"html_classes\" function argument (.+?) \\(key (.+?)\\) should be a string, got \"(.+?)\"\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/html-extra/HtmlExtension.php","lineNumber":110,"sourceCode":"            $repr .= ';base64,'.base64_encode($data);\n        }\n\n        return $repr;\n    }\n\n    /**\n     * @internal\n     */\n    public static function htmlClasses(...$args): string\n    {\n        $classes = [];\n        foreach ($args as $i => $arg) {\n            if (\\is_string($arg) || $arg instanceof Markup) {\n                $classes[] = (string) $arg;\n            } elseif (\\is_array($arg)) {\n                foreach ($arg as $class => $condition) {\n                    if (!\\is_string($class)) {\n                        throw new RuntimeError(\\sprintf('The \"html_classes\" function argument %d (key %d) should be a string, got \"%s\".', $i, $class, get_debug_type($class)));\n                    }\n                    if (!$condition) {\n                        continue;\n                    }\n                    $classes[] = $class;\n                }\n            } else {\n                throw new RuntimeError(\\sprintf('The \"html_classes\" function argument %d should be either a string or an array, got \"%s\".', $i, get_debug_type($arg)));\n            }\n        }\n\n        return implode(' ', array_unique(array_filter($classes, static function ($v) { return '' !== $v; })));\n    }\n\n    /**\n     * @param string|list<string|null>                           $base\n     * @param array<string, array<string, string|array<string>>> $variants\n     * @param array<array<string, string|array<string>>>         $compoundVariants","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/html-extra/HtmlExtension.php#L92-L128","documentation":"The twig html_classes() function builds a class attribute from strings plus array arguments mapping class names to boolean conditions. When an array argument's key is not a string, htmlClasses() throws this RuntimeError with the argument index, the offending key, and its debug type, because non-string keys cannot become CSS class names.","triggerScenarios":"Calling html_classes(['0' => true]) with integer keys, html_classes($conditions) where $conditions has int keys, or keys produced by array_values/array_column with numeric indexes.","commonSituations":"Passing a condition map built from a database result or numeric-indexed array instead of class-name-keyed array; PHP silently converts numeric-string keys like '5' to int keys.","solutions":["Ensure every array argument uses string class names as keys.","Cast or rebuild the array with class-name keys before passing.","Filter out numeric keys before calling html_classes.","Use string arguments (unconditional classes) instead of arrays when no condition is needed."],"exampleFix":"// before\nhtml_classes([0 => true, 1 => false]);\n// after\nhtml_classes(['is-active' => true, 'is-hidden' => false]);","handlingStrategy":"validation","validationCode":"foreach ($args as $arg) {\n    if (is_array($arg)) {\n        foreach (array_keys($arg) as $key) {\n            if (!is_string($key)) {\n                throw new InvalidArgumentException(sprintf('html_classes array keys must be class-name strings, got %s', get_debug_type($key)));\n            }\n        }\n    }\n}\nhtml_classes(...$args);","typeGuard":"function hasStringKeys(array $map): bool {\n    return [] === array_filter(array_keys($map), fn($k) => !is_string($k));\n}","tryCatchPattern":"try {\n    $classes = html_classes(...$args);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'should be a string')) {\n        // rebuild args with string keys before retrying\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Only pass class-name-keyed condition maps to html_classes","Beware PHP coercing numeric-string keys ('5') to int keys in array literals","Rebuild condition maps with array_combine against a list of class names","Use plain string arguments for unconditional classes"],"tags":["twig","html-extra","html-classes","argument-validation"],"backgroundTag":"invalid-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"}