{"record":{"id":"bb1c3114fe4760aa","repo":"Intervention/image","slug":"unable-to-set-array-key-use-setwidth-or-setheig","errorCode":null,"errorMessage":"Unable to set array key, use setWidth() or setHeight()","messagePattern":"Unable to set array key, use setWidth\\(\\) or setHeight\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Size.php","lineNumber":482,"sourceCode":"    public function offsetGet(mixed $offset): mixed\n    {\n        return match ($offset) {\n            0, 'width' => $this->width,\n            1, 'height' => $this->height,\n            default => throw new RuntimeException('Undefined array key ' . $offset)\n        };\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see ArrayAccess::offsetExists()\n     *\n     * @throws RuntimeException\n     */\n    public function offsetSet(mixed $offset, mixed $value): void\n    {\n        throw new RuntimeException('Unable to set array key, use setWidth() or setHeight()');\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see ArrayAccess::offsetExists()\n     *\n     * @throws RuntimeException\n     */\n    public function offsetUnset(mixed $offset): void\n    {\n        throw new RuntimeException('Unable to unset array key');\n    }\n\n    /**\n     * Show debug info for the current size.\n     *\n     * @return array<string, int|object>","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Size.php#L464-L500","documentation":"Intervention Image's Size object (the width/height pair returned by $image->size()) implements ArrayAccess for reading only: $size['width'], $size['height'], $size[0] and $size[1] work, but the write side offsetSet() unconditionally throws RuntimeException. The class is intentionally not mutable through array syntax; the message tells you the real mutation API is setWidth()/setHeight(). Any generic code that writes objects via ArrayAccess will always fail here.","triggerScenarios":"Executing any array-style assignment on an Intervention\\Image\\Size instance: $size['width'] = 800; or $size[0] = 800;. Most often hit in generic hydration/copy loops such as foreach ($dims as $k => $v) { $size[$k] = $v; }, or when porting Intervention Image v2 code that treated sizes like arrays.","commonSituations":"Porting v2-era scripts to v3's object API; generic object populators (config-to-object mapping, serializer hydration) that write via ArrayAccess; following IDE autocompletion that offers array syntax after seeing offsetGet() work for reads.","solutions":["Replace the array assignment with $size->setWidth(int) and/or $size->setHeight(int), which return the Size instance for chaining.","In hydration loops, whitelist the keys and map them explicitly to setWidth()/setHeight() instead of blind offset writes.","If you cannot change the third-party writer, wrap the write in try-catch for Intervention\\Image\\Exceptions\\RuntimeException and apply the value through the setter in the catch block."],"exampleFix":"// before\n$size = $image->size();\n$size['width'] = 800; // RuntimeException: Unable to set array key\n\n// after\n$size = $image->size();\n$size->setWidth(800);\n$size->setHeight(600);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    $size[$key] = $value; // generic ArrayAccess write\n} catch (\\Intervention\\Image\\Exceptions\\RuntimeException $e) {\n    // Size is read-only over array syntax - apply via setter\n    match ($key) {\n        'width', 0 => $size->setWidth((int) $value),\n        'height', 1 => $size->setHeight((int) $value),\n        default => throw $e,\n    };\n}","preventionTips":["Never write Size via $size[...] — the ArrayAccess implementation is read-only by design.","In hydration code, map keys to setWidth()/setHeight() explicitly instead of generic offset writes.","Treat RuntimeException from offsetSet/offsetGet as a programming error to fix at the call site, not to silence."],"tags":["php","intervention-image","arrayaccess","immutable","size"],"backgroundTag":"read-only-collection-mutation","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}