diff --git a/src/Dumper.php b/src/Dumper.php index be068907..83aa1bda 100644 --- a/src/Dumper.php +++ b/src/Dumper.php @@ -9,6 +9,10 @@ use ReflectionException; use Yiisoft\VarDumper\ClosureExporter; +use function array_key_exists; +use function is_array; +use function is_object; + final class Dumper { private array $objects = []; @@ -47,19 +51,20 @@ public function asJson(int $depth = 50, bool $format = false): string /** * Export variable as JSON summary of topmost items. + * Dumper go into the variable on full depth for search all objects. * - * @param int $depth Maximum depth that the dumper should go into the variable. + * @param int $depth Maximum depth that the dumper should print out. * @param bool $prettyPrint Whatever to format exported code. * * @return string JSON string containing summary. */ public function asJsonObjectsMap(int $depth = 50, bool $prettyPrint = false): string { - $this->buildObjectsCache($this->variable, $depth, false); - return $this->asJsonInternal($this->objects, $prettyPrint, $depth, 1, true); + $this->buildObjectsCache($this->variable); + return $this->asJsonInternal($this->objects, $prettyPrint, $depth + 1, 1, true); } - private function buildObjectsCache(mixed $variable, int $depth, bool $limitDepth = true, int $level = 0): void + private function buildObjectsCache(mixed $variable, ?int $depth = null, int $level = 0): void { if (is_object($variable)) { if (array_key_exists($variable::class, $this->excludedClasses) || @@ -68,26 +73,24 @@ private function buildObjectsCache(mixed $variable, int $depth, bool $limitDepth return; } $this->objects[$objectDescription] = $variable; + } - $nextLevel = $limitDepth ? $level + 1 : 0; - if ($depth <= $nextLevel) { - return; - } - $variable = $this->getObjectProperties($variable); + $nextLevel = $level + 1; + if ($depth !== null && $depth <= $nextLevel) { + return; + } + if (is_object($variable)) { + $variable = $this->getObjectProperties($variable); foreach ($variable as $value) { - $this->buildObjectsCache($value, $depth, $limitDepth, $nextLevel); + $this->buildObjectsCache($value, $depth, $nextLevel); } return; } if (is_array($variable)) { - $nextLevel = $level + 1; - if ($depth <= $nextLevel) { - return; - } foreach ($variable as $value) { - $this->buildObjectsCache($value, $depth, $limitDepth, $nextLevel); + $this->buildObjectsCache($value, $depth, $nextLevel); } } } diff --git a/tests/Unit/DumperTest.php b/tests/Unit/DumperTest.php index 54ec45a4..237f40fc 100644 --- a/tests/Unit/DumperTest.php +++ b/tests/Unit/DumperTest.php @@ -19,6 +19,96 @@ final class DumperTest extends TestCase { + public function testAsJsonObjectsMapLevelOne(): void + { + $object = new stdClass(); + $object->var = 'test'; + $objectId = spl_object_id($object); + + $this->assertSame( + <<asJsonObjectsMap(1, true) + ); + } + + public function testAsJsonObjectsMapNestedObject(): void + { + $nested2 = new stdClass(); + $nested2->name = 'nested2'; + $nested2Id = spl_object_id($nested2); + + $nested1 = new stdClass(); + $nested1->name = 'nested1'; + $nested1->var = $nested2; + $nested1Id = spl_object_id($nested1); + + $object = new stdClass(); + $object->name = 'root'; + $object->var = $nested1; + $objectId = spl_object_id($object); + + $this->assertSame( + <<asJsonObjectsMap(1, true) + ); + } + + public function testAsJsonObjectsMapArrayWithObject(): void + { + $nested2 = new stdClass(); + $nested2->name = 'nested2'; + $nested2Id = spl_object_id($nested2); + + $nested1 = new stdClass(); + $nested1->name = 'nested1'; + $nested1->var = [$nested2]; + $nested1Id = spl_object_id($nested1); + + $object = new stdClass(); + $object->name = 'root'; + $object->var = $nested1; + $objectId = spl_object_id($object); + + $this->assertSame( + <<asJsonObjectsMap(1, true) + ); + } + /** * @dataProvider loopAsJsonObjectMapDataProvider */ @@ -253,7 +343,7 @@ public function testObjectExpanding(): void } JSON; - $actualResult = Dumper::create($var)->asJsonObjectsMap(4, true); + $actualResult = Dumper::create($var)->asJsonObjectsMap(3, true); $this->assertEquals($expectedResult, $actualResult); } @@ -338,6 +428,7 @@ public function testCacheDoesNotCoversObjectOutOfDumpDepth(): void $object1 = new stdClass(); $object1Id = spl_object_id($object1); $object2 = new stdClass(); + $object2Id = spl_object_id($object2); $variable = [$object1, [[$object2]]]; $expectedResult = sprintf('["object@stdClass#%d",["array (1 item) [...]"]]', $object1Id); @@ -349,7 +440,7 @@ public function testCacheDoesNotCoversObjectOutOfDumpDepth(): void $map = $dumper->asJsonObjectsMap(2); $this->assertEqualsWithoutLE( <<