Skip to content

MIR1206

PossiblyInvalidClone

The clone keyword is used on a value with a union type where at least one member is not an object. At runtime, if the non-object branch is taken, a fatal error will occur.

<?php
function duplicate(object|null $value): ?object {
return clone $value; // $value could be null
}

Narrow the type before cloning, or handle the non-object case explicitly:

<?php
function duplicate(object|null $value): ?object {
return $value !== null ? clone $value : null;
}