{"record":{"id":"c0fa426736a69601","repo":"Intervention/image","slug":"unable-to-unset-array-key","errorCode":null,"errorMessage":"Unable to unset array key","messagePattern":"Unable to unset array key","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Size.php","lineNumber":494,"sourceCode":"     * @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>\n     */\n    public function __debugInfo(): array\n    {\n        return [\n            'width' => $this->width(),\n            'height' => $this->height(),\n            'pivot' => $this->pivot,\n        ];\n    }\n}\n","sourceCodeStart":476,"sourceCodeEnd":511,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Size.php#L476-L511","documentation":"Size implements ArrayAccess only for reading width/height, so offsetUnset() always throws RuntimeException: there is no way to remove a dimension from a size object. A size without width or height is meaningless in this library, so unset() is rejected by design. Note that offsetExists('width') returns true, so an exists() check does not make unset() safe.","triggerScenarios":"Calling unset($size['width']), unset($size[0]) or any unset($size[...]) on the result of $image->size(). Typically appears in cleanup/unset-all loops (foreach (array_keys($data) as $k) unset($obj[$k]);) or serializer code that 'clears' objects through ArrayAccess.","commonSituations":"Serialization/normalization helpers that reset objects before re-populating them; code ported from Intervention Image v2; copy-paste cleanup patterns that iterate ArrayAccess objects the same way as arrays.","solutions":["Remove the unset() call; Size has no removable fields. If you want a fresh size, create a new Intervention\\Image\\Size instance instead.","To 'reset' dimensions, call setWidth()/setHeight() with the new values rather than clearing them.","Guard generic unset loops so they skip objects whose offsetUnset() throws, catching Intervention\\Image\\Exceptions\\RuntimeException."],"exampleFix":"// before\nunset($size['width']); // RuntimeException: Unable to unset array key\n\n// after\n// nothing to unset - assign a new value instead\n$size->setWidth(0);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    unset($size[$key]);\n} catch (\\Intervention\\Image\\Exceptions\\RuntimeException $e) {\n    // nothing to unset: Size has no removable fields; ignore or assign defaults\n    $size->setWidth(0)->setHeight(0); // if a 'reset' was intended\n}","preventionTips":["Skip unset() on Size objects entirely — dimensions cannot be removed.","Do not trust offsetExists(): it returns true for 'width'/'height' even though unset() throws.","In generic cleanup loops, skip objects of class Intervention\\Image\\Size or wrap in try-catch."],"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"}