Skip to content

Commit

Permalink
Discover all nested objects (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz authored Nov 4, 2024
1 parent 90bb196 commit 565a0d7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Collector/ContainerInterfaceProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
97 changes: 97 additions & 0 deletions tests/Unit/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<JSON
{
"stdClass#$lvl1Id": {
"public \$id": "lvl1",
"public \$prop1": "object@stdClass#$lvl2Id",
"public \$prop2": "object@stdClass#$lvl2Id"
},
"stdClass#$lvl2Id": {
"public \$id": "lvl2",
"public \$prop1": "object@stdClass#$lvl3Id",
"public \$prop2": "object@stdClass#$lvl3Id"
},
"stdClass#$lvl3Id": {
"public \$id": "lvl3",
"public \$prop1": "object@stdClass#$lvl4Id",
"public \$prop2": "object@stdClass#$lvl4Id"
},
"stdClass#$lvl4Id": {
"public \$id": "lvl4",
"public \$prop1": "object@stdClass#$lvl5Id",
"public \$prop2": "object@stdClass#$lvl5Id"
},
"stdClass#$lvl5Id": {
"public \$id": "lvl5",
"public \$prop1": "object@stdClass#$lvl6Id",
"public \$prop2": "object@stdClass#$lvl6Id"
},
"stdClass#$lvl6Id": {
"public \$id": "lvl6",
"public \$prop1": "object@stdClass#$lvl7Id",
"public \$prop2": "object@stdClass#$lvl7Id"
},
"stdClass#$lvl7Id": {
"public \$id": "lvl7",
"public \$prop1": "object@stdClass#$lvl8Id",
"public \$prop2": "object@stdClass#$lvl8Id"
},
"stdClass#$lvl8Id": {
"public \$id": "lvl8",
"public \$prop1": "object@stdClass#$lvl9Id",
"public \$prop2": "object@stdClass#$lvl9Id"
},
"stdClass#$lvl9Id": {
"public \$id": "lvl9",
"public \$prop1": "object@stdClass#$lvl10Id",
"public \$prop2": "object@stdClass#$lvl10Id"
},
"stdClass#$lvl10Id": {
"public \$id": "lvl10",
"public \$loop": [
[
"array (1 item) [...]"
]
],
"public \$head": "object@stdClass#$lvl1Id"
}
}
JSON;

$actualResult = Dumper::create($var)->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
*/
Expand Down

0 comments on commit 565a0d7

Please sign in to comment.