{"record":{"id":"1ef14333cb49e677","repo":"twigphp/Twig","slug":"separatedtokenlist-can-only-be-constructed-from-iterable-or","errorCode":null,"errorMessage":"SeparatedTokenList can only be constructed from iterable or scalar values.","messagePattern":"SeparatedTokenList can only be constructed from iterable or scalar values\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/html-extra/HtmlAttr/SeparatedTokenList.php","lineNumber":30,"sourceCode":"namespace Twig\\Extra\\Html\\HtmlAttr;\n\nuse Twig\\Error\\RuntimeError;\n\n/**\n * @author Matthias Pigulla <mp@webfactory.de>\n */\nfinal class SeparatedTokenList implements AttributeValueInterface, MergeableInterface\n{\n    private readonly array $value;\n\n    public function __construct(mixed $value, private readonly string $separator = ' ')\n    {\n        if (is_iterable($value)) {\n            $this->value = [...$value];\n        } elseif (\\is_scalar($value)) {\n            $this->value = [$value];\n        } else {\n            throw new RuntimeError('SeparatedTokenList can only be constructed from iterable or scalar values.');\n        }\n    }\n\n    public function mergeInto(mixed $previous): mixed\n    {\n        if ($previous instanceof self && $previous->separator === $this->separator) {\n            return new self([...$previous->value, ...$this->value], $this->separator);\n        }\n\n        if (is_iterable($previous)) {\n            return new self([...$previous, ...$this->value], $this->separator);\n        }\n\n        throw new RuntimeError('SeparatedTokenList can only be merged with iterables or other SeparatedTokenList instances using the same separator.');\n    }\n\n    public function appendFrom(mixed $newValue): mixed\n    {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/html-extra/HtmlAttr/SeparatedTokenList.php#L12-L48","documentation":"SeparatedTokenList represents a list of tokens joined by a separator (e.g. space-separated classes). Its constructor accepts iterable values (spread into a list) or a scalar (wrapped in a single-element list); any other value — null, arrays-as-objects, resources, objects — raises this RuntimeError.","triggerScenarios":"Calling new SeparatedTokenList(' ', null), new SeparatedTokenList(',', new stdClass()), or passing a non-iterable/non-scalar expression to a class-list attribute helper in a template.","commonSituations":"A class-list variable is null because a lookup returned nothing, or an object was passed where an array was expected.","solutions":["Pass an array/Traversable of tokens or a single scalar value.","Coerce null to [] before constructing.","Cast the value to string or array depending on intent before construction."],"exampleFix":"// before\n$list = new SeparatedTokenList(' ', $maybeNull);\n// after\n$list = new SeparatedTokenList(' ', $maybeNull ?? []);","handlingStrategy":"type-guard","validationCode":"if (!is_iterable($value) && !is_scalar($value)) {\n    throw new InvalidArgumentException('SeparatedTokenList requires iterable or scalar');\n}\n$list = new SeparatedTokenList(' ', $value);","typeGuard":"function isValidTokenListValue(mixed $v): bool {\n    return is_iterable($v) || is_scalar($v);\n}","tryCatchPattern":"try {\n    $list = new SeparatedTokenList(' ', $value);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'SeparatedTokenList can only be constructed')) {\n        $list = new SeparatedTokenList(' ', []);\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Coerce nullable class-list variables with ?? [] before construction","Only pass arrays, Traversables, or scalars — never objects/null","Validate data sources (DB results) return arrays, not objects"],"tags":["twig","html-extra","separated-token-list","type-error"],"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"}