{"record":{"id":"a990ab7c86b8ac2e","repo":"twigphp/Twig","slug":"attributes-using-inlinestyle-can-only-be-merged-with","errorCode":null,"errorMessage":"Attributes using InlineStyle can only be merged with iterables or other InlineStyle instances.","messagePattern":"Attributes using InlineStyle can only be merged with iterables or other InlineStyle instances\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/html-extra/HtmlAttr/InlineStyle.php","lineNumber":42,"sourceCode":"    {\n        if (!is_iterable($value)) {\n            throw new RuntimeError('InlineStyle can only be created from iterable values.');\n        }\n\n        $this->value = [...$value];\n    }\n\n    public function mergeInto(mixed $previous): mixed\n    {\n        if ($previous instanceof self) {\n            return new self([...$previous->value, ...$this->value]);\n        }\n\n        if (is_iterable($previous)) {\n            return new self([...$previous, ...$this->value]);\n        }\n\n        throw new RuntimeError('Attributes using InlineStyle can only be merged with iterables or other InlineStyle instances.');\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 InlineStyle.');\n        }\n\n        return new self([...$this->value, ...$newValue]);\n    }\n\n    public function getValue(): ?string\n    {\n        $style = '';\n        foreach ($this->value as $name => $value) {\n            // `0`, `0.0` and `'0'` are valid CSS values, so only the values that carry\n            // no declaration at all are skipped here\n            if (null === $value || false === $value || true === $value || '' === $value || [] === $value) {","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/html-extra/HtmlAttr/InlineStyle.php#L24-L60","documentation":"InlineStyle::mergeInto merges this style object into a previous attribute value. A previous value that is neither iterable nor an InlineStyle instance cannot be combined, so a RuntimeError is thrown. This guards against overwriting a rendered string attribute with incompatible types.","triggerScenarios":"Calling $inlineStyle->mergeInto('color:red') with a plain string, mergeInto(null), or merging into any scalar/object that is not InlineStyle or iterable.","commonSituations":"Merging into an attribute value that a template already rendered as a plain string, or into a null from an unset variable.","solutions":["Ensure the previous value is an InlineStyle instance or an iterable of declarations.","Convert a plain CSS string into an iterable (explode on ';') before merging.","Initialize the attribute value as an empty InlineStyle instead of null/string."],"exampleFix":"// before\n$merged = $style->mergeInto('color:red');\n// after\n$merged = $style->mergeInto(new InlineStyle(['color' => 'red']));","handlingStrategy":"type-guard","validationCode":"if (!$previous instanceof InlineStyle && !is_iterable($previous)) {\n    throw new InvalidArgumentException('Cannot merge InlineStyle into non-iterable previous value');\n}\n$merged = $style->mergeInto($previous);","typeGuard":"function canMergeInlineStyle(mixed $previous): bool {\n    return $previous instanceof InlineStyle || is_iterable($previous);\n}","tryCatchPattern":"try {\n    $merged = $style->mergeInto($previous);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'can only be merged')) {\n        $merged = $style->mergeInto(new InlineStyle(is_iterable($previous) ? [...$previous] : []));\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Keep attribute values as InlineStyle/iterables until final rendering","Never merge into already-rendered HTML strings","Initialize previous values as empty InlineStyle rather than null/string"],"tags":["twig","html-extra","inline-style","merge","type-error"],"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"}