symfony/var-dumper · error · BadMethodCallException
objects are immutable.
Error message
objects are immutable.
What it means
Data objects are read-only views over cloned dump values. offsetSet() (invoked by ArrayAccess writes like $data[$key] = $value) unconditionally throws BadMethodCallException because Data is intentionally immutable.
Solutions
- Do not mutate Data; convert first with $data->getValue() or iterator_to_array($data) and modify the plain array
- Clone the raw value via $data->getValue(), change it, and re-dump if needed
- If you need a mutable structure, build your own array from the Data's contents
Example fix
// before $data['secret'] = '[redacted]'; // after $arr = $data->getValue(); $arr['secret'] = '[redacted]';
Defensive patterns
Strategy: fallback
Validate before calling
if ($data instanceof \Symfony\Component\VarDumper\Cloner\Data) {
// never mutate; copy out first
$copy = is_array($v = $data->getValue()) ? $v : (array) $v;
} Type guard
function mutableCopy(\Symfony\Component\VarDumper\Cloner\Data $data): array {
return is_array($v = $data->getValue()) ? $v : (array) $v;
} Try / catch
try {
$data[$key] = $value;
} catch (\BadMethodCallException $e) {
// Data is immutable: work on a copied array instead
$arr = $data->getValue();
} Prevention
- Treat Data as read-only by design; never use ArrayAccess writes on it
- Copy values out to plain arrays before any modification
- Document in team code that dump/clone results must not be mutated
When it happens
Trigger: Attempting to mutate a Data object with array-write syntax, e.g. `$data['key'] = 'x'` or `$data[] = $value`, including accidental mutation inside loops.
Common situations: Developers treating a dump/clone Data handle like a normal array and trying to modify it; mutating dump results to redact values before display.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
AI-assisted analysis of symfony/var-dumper@e9d9cf5dcd (2026-09-14).
Data as JSON: /api/errors/1da9b51ffdd3d18a.
Report an issue: GitHub.
Appendix: source
Thrown at Cloner/Data.php:152
public function __isset(string $key): bool
{
return null !== $this->seek($key);
}
public function offsetExists(mixed $key): bool
{
return $this->__isset($key);
}
public function offsetGet(mixed $key): mixed
{
return $this->__get($key);
}
public function offsetSet(mixed $key, mixed $value): void
{
throw new \BadMethodCallException(self::class.' objects are immutable.');
}
public function offsetUnset(mixed $key): void
{
throw new \BadMethodCallException(self::class.' objects are immutable.');
}
public function __toString(): string
{
$value = $this->getValue();
if (!\is_array($value)) {
return (string) $value;
}
return \sprintf('%s (count=%d)', $this->getType(), \count($value));
}
View on GitHub (pinned to e9d9cf5dcd)