{"record":{"id":"004f0542eb5c970f","repo":"w7corp/easywechat","slug":"response-is-immutable","errorCode":null,"errorMessage":"Response is immutable.","messagePattern":"Response is immutable\\.","errorType":"exception","errorClass":"BadMethodCallException","httpStatus":null,"severity":"error","filePath":"src/Kernel/HttpClient/Response.php","lineNumber":218,"sourceCode":"        return '';\n    }\n\n    public function offsetExists(mixed $offset): bool\n    {\n        return array_key_exists($offset, $this->toArray());\n    }\n\n    public function offsetGet(mixed $offset): mixed\n    {\n        return $this->toArray()[$offset] ?? null;\n    }\n\n    /**\n     * @throws BadMethodCallException\n     */\n    public function offsetSet(mixed $offset, mixed $value): void\n    {\n        throw new BadMethodCallException('Response is immutable.');\n    }\n\n    /**\n     * @throws BadMethodCallException\n     */\n    public function offsetUnset(mixed $offset): void\n    {\n        throw new BadMethodCallException('Response is immutable.');\n    }\n\n    /**\n     * @param  array<array-key, mixed>  $arguments\n     */\n    public function __call(string $name, array $arguments): mixed\n    {\n        return $this->response->{$name}(...$arguments);\n    }\n","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/w7corp/easywechat/blob/f0cf0a8b8361417ed683b8246d0ecbaf0aafcaa8/src/Kernel/HttpClient/Response.php#L200-L236","documentation":"Response implements ArrayAccess only for reading: offsetSet() unconditionally throws BadMethodCallException('Response is immutable.') because mutating the wrapper would desync it from the underlying HTTP response it delegates to. Writes via $response['key'] = $value always fail, regardless of key or value.","triggerScenarios":"Treating the response like a mutable array after reading it: $response['errcode'] = 0; or merging defaults by direct assignment.","commonSituations":"Copy-pasting array-manipulation code that targeted the toArray() output onto the response object itself, generic array helpers applied to an ArrayAccess object.","solutions":["Materialize a local array first: $data = $response->toArray(); then mutate $data freely","If a helper must accept both, branch on is_array() before writing"],"exampleFix":"// before\n$response = $api->get('/cgi-bin/user/info');\n$response['extra'] = 'computed'; // BadMethodCallException\n\n// after\n$data = $response->toArray();\n$data['extra'] = 'computed';","handlingStrategy":"validation","validationCode":"// Copy into a plain array before any mutation\n$data = $response->toArray(); // mutable copy\n$data['extra'] = 'value'; // safe","typeGuard":"function asMutableArray(\\EasyWeChat\\Kernel\\HttpClient\\Response $response): array\n{\n    return $response->toArray();\n}","tryCatchPattern":null,"preventionTips":["Treat the Response as read-only; immediately materialize toArray() when you need to edit","Static analysis (phpstan) on ArrayAccess writes catches this before runtime"],"tags":["php","easywechat","http-client","arrayaccess","immutability"],"backgroundTag":"immutable-object-mutation","analyzedSha":"f0cf0a8b8361417ed683b8246d0ecbaf0aafcaa8","analyzedAt":"2026-08-21T05:29:19.565Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}