Skip to content

Commit

Permalink
avoid using Reflection for some classes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
brettmc committed Jun 21, 2024
1 parent 507d233 commit 6c54af5
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)',
Expand Down Expand Up @@ -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;

Check failure on line 402 in src/Exporter.php

View workflow job for this annotation

GitHub Actions / Type Checker

UndefinedClass

src/Exporter.php:402:35: UndefinedClass: Class, interface or enum named Google\Protobuf\Internal\Message does not exist (see https://psalm.dev/019)
}
}

0 comments on commit 6c54af5

Please sign in to comment.