avoid using Reflection for some classes

if a class has problems with Reflection, then fall back to the old toArray
method of counting its properties. For example, using reflection on an ext-protobuf
Message causes a segfault
This commit is contained in:
Brett McBride 2024-06-21 14:37:48 +10:00 committed by Sebastian Bergmann
parent 507d2333cb
commit 1e4815983a
No known key found for this signature in database
GPG Key ID: 4AA394086372C20A
1 changed files with 9 additions and 1 deletions

View File

@ -33,6 +33,7 @@ use function str_replace;
use function strtr;
use function var_export;
use BackedEnum;
use Google\Protobuf\Internal\Message;
use ReflectionObject;
use SebastianBergmann\RecursionContext\Context as RecursionContext;
use SplObjectStorage;
@ -129,7 +130,9 @@ final readonly class Exporter
}
if (is_object($value)) {
$numberOfProperties = count((new ReflectionObject($value))->getProperties());
$numberOfProperties = $this->cannotUseReflection($value)
? count($this->toArray($value))
: count((new ReflectionObject($value))->getProperties());
return sprintf(
'%s Object (%s)',
@ -393,4 +396,9 @@ final readonly class Exporter
return $class . ' Object #' . spl_object_id($value) . ' (' . $buffer . ')';
}
private function cannotUseReflection(object $object): bool
{
return $object instanceof Message;
}
}