From 6c54af55a5ad9a46591200eef1f8b97adbea4bbc Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Fri, 21 Jun 2024 14:37:48 +1000 Subject: [PATCH] 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 --- src/Exporter.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Exporter.php b/src/Exporter.php index aad6ff1..1a6f4ea 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -33,6 +33,7 @@ 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 @@ public function shortenedExport(mixed $value): string } 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 @@ private function exportObject(object $value, RecursionContext $processed, int $i return $class . ' Object #' . spl_object_id($value) . ' (' . $buffer . ')'; } + + private function cannotUseReflection(object $object): bool + { + return $object instanceof Message; + } }