{"record":{"id":"7c178200c1938082","repo":"twigphp/Twig","slug":"separatedtokenlist-can-only-be-merged-with-iterables-or","errorCode":null,"errorMessage":"SeparatedTokenList can only be merged with iterables or other SeparatedTokenList instances using the same separator.","messagePattern":"SeparatedTokenList can only be merged with iterables or other SeparatedTokenList instances using the same separator\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/html-extra/HtmlAttr/SeparatedTokenList.php","lineNumber":44,"sourceCode":"            $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    {\n        if (!is_iterable($newValue)) {\n            throw new RuntimeError('Only iterable values can be appended to SeparatedTokenList.');\n        }\n\n        return new self([...$this->value, ...$newValue], $this->separator);\n    }\n\n    public function getValue(): ?string\n    {\n        $filtered = array_filter($this->value, static fn ($v) => null !== $v && false !== $v);\n\n        // Omit attribute if list contains only false or null values\n        if (!$filtered) {\n            return null;","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/html-extra/HtmlAttr/SeparatedTokenList.php#L26-L62","documentation":"SeparatedTokenList::mergeInto merges this list into a previous attribute value. The previous value must be iterable, or a SeparatedTokenList using the same separator; anything else (string, null, different-separator list) raises this RuntimeError to prevent silently mixing token lists with inconsistent separators.","triggerScenarios":"Calling $list->mergeInto('a b c') with a plain string, mergeInto(null), or merging two SeparatedTokenList instances with different separators (e.g. ' ' vs ',').","commonSituations":"Merging into an already-rendered string attribute, or combining comma-separated lists with space-separated ones.","solutions":["Ensure the previous value is iterable or a SeparatedTokenList with the same separator.","Normalize both lists to the same separator before merging.","Convert a raw string into an array (explode by separator) before merging."],"exampleFix":"// before\n$merged = $list->mergeInto('a b c');\n// after\n$merged = $list->mergeInto(new SeparatedTokenList(' ', ['a','b','c']));","handlingStrategy":"type-guard","validationCode":"$canMerge = $previous instanceof SeparatedTokenList\n    ? $previous->getSeparator() === $list->getSeparator()\n    : is_iterable($previous);\nif (!$canMerge) {\n    throw new InvalidArgumentException('Incompatible previous value for SeparatedTokenList merge');\n}\n$merged = $list->mergeInto($previous);","typeGuard":"function canMergeTokenList(mixed $previous, SeparatedTokenList $list): bool {\n    return ($previous instanceof SeparatedTokenList && $previous->getSeparator() === $list->getSeparator())\n        || is_iterable($previous);\n}","tryCatchPattern":"try {\n    $merged = $list->mergeInto($previous);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'same separator')) {\n        // normalize separators, then retry\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Standardize on one separator (e.g. ' ') across your app's token lists","Never merge into already-rendered strings","Check separator compatibility before merging lists"],"tags":["twig","html-extra","separated-token-list","merge","separator"],"backgroundTag":"incompatible-source-type","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"}