{"record":{"id":"eac7909365cab766","repo":"twigphp/Twig","slug":"cannot-merge-incompatible-values-for-key-s","errorCode":null,"errorMessage":"Cannot merge incompatible values for key \"%s\".","messagePattern":"Cannot merge incompatible values for key \"(.+?)\"\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/html-extra/HtmlExtension.php","lineNumber":186,"sourceCode":"                }\n\n                $existing = $result[$key];\n\n                switch (true) {\n                    case $value instanceof MergeableInterface:\n                        $result[$key] = $value->mergeInto($existing);\n                        break;\n                    case $existing instanceof MergeableInterface:\n                        $result[$key] = $existing->appendFrom($value);\n                        break;\n                    case is_iterable($existing) && is_iterable($value):\n                        $result[$key] = [...$existing, ...$value];\n                        break;\n                    case (\\is_scalar($existing) || \\is_object($existing)) && (\\is_scalar($value) || \\is_object($value)):\n                        $result[$key] = $value;\n                        break;\n                    default:\n                        throw new RuntimeError(\\sprintf('Cannot merge incompatible values for key \"%s\".', $key));\n                }\n            }\n        }\n\n        return $result;\n    }\n\n    /** @internal */\n    public static function htmlAttr(Environment $env, iterable|string|false|null ...$args): string\n    {\n        $attr = self::htmlAttrMerge(...$args);\n\n        $result = '';\n        $runtime = $env->getRuntime(EscaperRuntime::class);\n\n        foreach ($attr as $name => $value) {\n            if (null === $value = self::htmlAttrValue($name, $value)) {\n                continue;","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/html-extra/HtmlExtension.php#L168-L204","documentation":"When html_attr_merge() combines two attribute arrays under the same key, it can only merge array+array (union) or replace scalar+scalar / object+scalar combinations. Any other pairing (e.g. array vs scalar, or two incompatible AttributeValueInterface objects) falls to the default arm and throws this RuntimeError.","triggerScenarios":"Merging two arrays where the same key has an array value in one and a scalar in the other, e.g. ['class' => ['a','b']] merged with ['class' => 'a'].","commonSituations":"Combining default attributes with user-supplied overrides where a key changed shape between versions; conditional Twig expressions giving different shapes per branch.","solutions":["Make the shapes consistent: wrap scalars in arrays (['class' => ['a']] instead of ['class' => 'a']).","Pre-normalize both arrays so each key has the same type before merging.","Drop or rename one of the conflicting keys if they are semantically different.","Catch the RuntimeError and fall back to one operand."],"exampleFix":"// before\nhtml_attr_merge(['class' => ['a', 'b']], ['class' => 'c'])\n// after\nhtml_attr_merge(['class' => ['a', 'b']], ['class' => ['c']])","handlingStrategy":"validation","validationCode":"foreach ($extra as $k => $v) { if (isset($base[$k]) && ((is_array($base[$k]) xor is_array($v)))) { $v = is_array($v) ? $v : [$v]; $base[$k] = is_array($base[$k]) ? $base[$k] : [$base[$k]]; } }","typeGuard":"function areMergeable(mixed $a, mixed $b): bool { return (is_array($a) && is_array($b)) || (!is_array($a) && !is_array($b)); }","tryCatchPattern":"try { $attrs = html_attr_merge($base, $override); } catch (Twig\\Error\\RuntimeError $e) { $attrs = array_merge($base, $override); }","preventionTips":["Keep a consistent shape per attribute key (always array or always scalar)","Wrap scalars in arrays when merging list-like attributes","Centralize attribute defaults in one builder function"],"tags":["twig","merge-conflict","html-attr"],"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"}