From 14a035b6861d80691ff717dc7db629310b39d725 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 29 Mar 2024 11:01:35 +0100 Subject: [PATCH] Implement suggestion from @staabm in https://github.com/sebastianbergmann/exporter/pull/56#pullrequestreview-1968169975 --- src/Exporter.php | 21 ++++++++++++++------- tests/ExporterTest.php | 6 +++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Exporter.php b/src/Exporter.php index 453cdee..467e00d 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -334,7 +334,7 @@ private function exportArray(array &$value, RecursionContext $processed, string return 'Array &' . (string) $key . ' [' . $values . ']'; } - private function exportObject(mixed $value, RecursionContext $processed, string $whitespace, int $indentation): string + private function exportObject(object $value, RecursionContext $processed, string $whitespace, int $indentation): string { $class = $value::class; @@ -345,15 +345,22 @@ private function exportObject(mixed $value, RecursionContext $processed, string $processed->add($value); if ($this->objectExporter !== null && $this->objectExporter->handles($value)) { - return $this->objectExporter->export($value); + $buffer = $this->objectExporter->export($value); + } else { + $buffer = $this->defaultObjectExport($value, $processed, $whitespace, $indentation); } - $values = ''; - $array = $this->toArray($value); + return $class . ' Object #' . spl_object_id($value) . ' (' . $buffer . ')'; + } + + private function defaultObjectExport(object $object, RecursionContext $processed, string $whitespace, int $indentation): string + { + $buffer = ''; + $array = $this->toArray($object); if (count($array) > 0) { foreach ($array as $k => $v) { - $values .= + $buffer .= $whitespace . ' ' . $this->recursiveExport($k, $indentation) @@ -362,9 +369,9 @@ private function exportObject(mixed $value, RecursionContext $processed, string . ",\n"; } - $values = "\n" . $values . $whitespace; + $buffer = "\n" . $buffer . $whitespace; } - return $class . ' Object #' . spl_object_id($value) . ' (' . $values . ')'; + return $buffer; } } diff --git a/tests/ExporterTest.php b/tests/ExporterTest.php index 9469013..a752dad 100644 --- a/tests/ExporterTest.php +++ b/tests/ExporterTest.php @@ -496,11 +496,11 @@ public function testExportOfObjectsCanBeCustomized(): void $exporter = new Exporter(new ObjectExporterChain([$objectExporter])); - $this->assertSame( + $this->assertStringMatchesFormat( <<<'EOT' Array &0 [ - 0 => custom object export, - 1 => custom object export, + 0 => stdClass Object #%d (custom object export), + 1 => stdClass Object #%d (custom object export), ] EOT ,