{"record":{"id":"f237172a620be91f","repo":"twigphp/Twig","slug":"inlinestyle-can-only-be-created-from-iterable-values","errorCode":null,"errorMessage":"InlineStyle can only be created from iterable values.","messagePattern":"InlineStyle can only be created from iterable values\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/html-extra/HtmlAttr/InlineStyle.php","lineNumber":26,"sourceCode":" * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nnamespace Twig\\Extra\\Html\\HtmlAttr;\n\nuse Twig\\Error\\RuntimeError;\n\n/**\n * @author Matthias Pigulla <mp@webfactory.de>\n */\nfinal class InlineStyle implements MergeableInterface, AttributeValueInterface\n{\n    private readonly array $value;\n\n    public function __construct(mixed $value)\n    {\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","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/html-extra/HtmlAttr/InlineStyle.php#L8-L44","documentation":"InlineDialeStyle/InlineStyle is a Twig html-extra value object representing CSS inline styles; its constructor only accepts iterable values (arrays, Traversables) which are spread into a list of style declarations. Passing a scalar or null raises this RuntimeError so malformed style data fails fast instead of producing broken HTML attributes.","triggerScenarios":"Calling new InlineStyle('color:red') (string), new InlineStyle(null), new InlineStyle(42), or passing a non-iterable expression to a style attribute in a template via the html-style style() helper.","commonSituations":"Passing a raw CSS string instead of an array of declarations, or a variable that is null because a lookup failed.","solutions":["Pass an array or Traversable of style declarations, e.g. new InlineStyle(['color' => 'red']).","Explode a CSS string yourself into an array before constructing.","Guard the value for null/empty before constructing."],"exampleFix":"// before\n$style = new InlineStyle('color:red; font-weight:bold');\n// after\n$style = new InlineStyle(['color' => 'red', 'font-weight' => 'bold']);","handlingStrategy":"type-guard","validationCode":"if (!is_iterable($value)) {\n    throw new InvalidArgumentException('InlineStyle requires an iterable of style declarations');\n}\n$style = new InlineStyle($value);","typeGuard":"function isIterableForInlineStyle(mixed $v): bool {\n    return is_iterable($v);\n}","tryCatchPattern":"try {\n    $style = new InlineStyle($value);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'InlineStyle can only be created')) {\n        $style = new InlineStyle((array) ($value ?? []));\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Always construct InlineStyle from arrays of declarations, never raw CSS strings","Coerce nullable variables with ?? [] before constructing","Document that the style() helper expects iterables"],"tags":["twig","html-extra","inline-style","type-error"],"backgroundTag":"type-mismatch","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"}