From 565a0d7801086f05b105eecb182ab29727fbda08 Mon Sep 17 00:00:00 2001 From: Dmitriy Derepko Date: Mon, 4 Nov 2024 19:15:21 +0200 Subject: [PATCH] Discover all nested objects (#286) --- src/Dumper.php | 15 ++- .../Collector/ContainerInterfaceProxyTest.php | 3 +- tests/Unit/DumperTest.php | 97 +++++++++++++++++++ 3 files changed, 109 insertions(+), 6 deletions(-) diff --git a/src/Dumper.php b/src/Dumper.php index 59ab5d8d..0fdaee87 100644 --- a/src/Dumper.php +++ b/src/Dumper.php @@ -71,10 +71,12 @@ private function buildObjectsCache(mixed $variable, int $depth, int $level = 0): return; } $this->objects[$objectDescription] = $variable; - if ($depth <= $level + 1) { - return; - } $variable = $this->getObjectProperties($variable); + + foreach ($variable as $value) { + $this->buildObjectsCache($value, $depth, 0); + } + return; } if (is_array($variable)) { $nextLevel = $level + 1; @@ -159,7 +161,12 @@ private function dumpNestedInternal( break; } - if ($objectCollapseLevel < $level && array_key_exists($objectDescription, $this->objects)) { + if (!array_key_exists($objectDescription, $this->objects)) { + $output = 'object@' . $objectDescription; + $this->objects[$objectDescription] = $variable; + break; + } + if ($objectCollapseLevel < $level) { $output = 'object@' . $objectDescription; break; } diff --git a/tests/Unit/Collector/ContainerInterfaceProxyTest.php b/tests/Unit/Collector/ContainerInterfaceProxyTest.php index c5b3a87c..a9a99799 100644 --- a/tests/Unit/Collector/ContainerInterfaceProxyTest.php +++ b/tests/Unit/Collector/ContainerInterfaceProxyTest.php @@ -158,9 +158,8 @@ public function testGetAndHasWithWrongId(): void $this->expectException(ContainerExceptionInterface::class); $this->expectExceptionMessage( sprintf( - 'No definition or class found or resolvable for "%s" while building "%s".', + 'No definition or class found or resolvable for "%s" while building it.', CollectorInterface::class, - CollectorInterface::class ) ); $containerProxy->get(CollectorInterface::class); diff --git a/tests/Unit/DumperTest.php b/tests/Unit/DumperTest.php index 8ffcb277..85c81f25 100644 --- a/tests/Unit/DumperTest.php +++ b/tests/Unit/DumperTest.php @@ -19,6 +19,103 @@ final class DumperTest extends TestCase { + public function testObjectExpanding(): void + { + $var = $this->createNested(10, [[[[[[[[['key' => 'end']]]]]]]]]); + + $lvl1Id = spl_object_id($var); + $lvl2Id = spl_object_id($var->prop1); + $lvl3Id = spl_object_id($var->prop1->prop1); + $lvl4Id = spl_object_id($var->prop1->prop1->prop1); + $lvl5Id = spl_object_id($var->prop1->prop1->prop1->prop1); + $lvl6Id = spl_object_id($var->prop1->prop2->prop1->prop1->prop1); + $lvl7Id = spl_object_id($var->prop1->prop2->prop1->prop1->prop1->prop1); + $lvl8Id = spl_object_id($var->prop1->prop2->prop1->prop1->prop1->prop1->prop1); + $lvl9Id = spl_object_id($var->prop1->prop2->prop1->prop1->prop1->prop1->prop1->prop1); + $lvl10Id = spl_object_id($var->prop1->prop2->prop1->prop1->prop1->prop1->prop1->prop1->prop1); + + $expectedResult = <<asJsonObjectsMap(4, true); + + $this->assertEquals($expectedResult, $actualResult); + } + + private function createNested(int $depth, mixed $data): object + { + $head = $lvl = new stdClass(); + $lvl->id = 'lvl1'; + + for ($i = 2; $i <= $depth; $i++) { + $nested = new stdClass(); + $nested->id = 'lvl' . $i; + $lvl->prop1 = $nested; + $lvl->prop2 = $nested; + $lvl = $nested; + } + $lvl->loop = $data; + $lvl->head = $head; + + return $head; + } + /** * @dataProvider asJsonObjectMapDataProvider */