{"record":{"id":"49beed574f8eed1f","repo":"twigphp/Twig","slug":"the-html-classes-function-argument-d-should-be-either-a","errorCode":null,"errorMessage":"The \"html_classes\" function argument %d should be either a string or an array, got \"%s\".","messagePattern":"The \"html_classes\" function argument (.+?) should be either a string or an array, got \"(.+?)\"\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/html-extra/HtmlExtension.php","lineNumber":118,"sourceCode":"     */\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\n     * @param array<string, string>                              $defaultVariant\n     *\n     * @internal\n     */\n    public static function htmlCva(array|string $base = [], array $variants = [], array $compoundVariants = [], array $defaultVariant = []): Cva\n    {\n        return new Cva($base, $variants, $compoundVariants, $defaultVariant);\n    }","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/html-extra/HtmlExtension.php#L100-L136","documentation":"Twig's html_classes() function accepts a variable list of arguments where class names must be strings or arrays of strings (possibly with condition pairs). The HtmlExtension throws this RuntimeError when an argument is neither a string nor an array — e.g. an int, float, bool or object — so it fails fast instead of producing a broken class attribute.","triggerScenarios":"Calling twig_html_classes() with a scalar like an integer or null, or with an object that is neither Stringable nor traversable, e.g. html_classes('btn', 42) or passing a DateTime instance.","commonSituations":"Passing a PHP value straight from a database or API payload without casting to string; forgetting to map a collection to strings before passing it; passing null/true where a class list was expected after a refactor.","solutions":["Cast or join non-string values to a string before passing, e.g. implode(' ', $arrayOfNames).","Wrap the value in an array if it may sometimes be a single string, e.g. html_classes('btn', [$maybeString]).","Filter out null/false/empty values before the call (html_classes already skips falsy conditions, but not wrong-typed args).","Check get_debug_type($arg) at the call site to find which argument index is wrong-typed."],"exampleFix":"// before\nhtml_classes('alert', $errorCount)\n// after\nhtml_classes('alert', $errorCount > 0 ? 'has-errors' : '')","handlingStrategy":"type-guard","validationCode":"$args = array_filter($args, fn($a) => is_string($a) || is_array($a));","typeGuard":"function isClassArg(mixed $v): bool { return is_string($v) || is_array($v); }","tryCatchPattern":"try { $class = twig_html_classes(...$args); } catch (Twig\\Error\\RuntimeError $e) { $class = ''; // log $e->getMessage() }","preventionTips":["Cast DB/API values to string before templating","Map collections to string arrays before passing","Never pass objects or bools directly to html_classes"],"tags":["twig","type-error","template"],"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"}