Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

282 discover all nested objects #287

Merged
merged 9 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,39 @@ public function asJson(int $depth = 50, bool $format = false): string
*/
public function asJsonObjectsMap(int $depth = 50, bool $prettyPrint = false): string
{
$this->buildObjectsCache($this->variable, $depth);
$this->buildObjectsCache($this->variable, $depth, false);
return $this->asJsonInternal($this->objects, $prettyPrint, $depth, 1, true);
}

private function buildObjectsCache(mixed $variable, int $depth, int $level = 0): void
private function buildObjectsCache(mixed $variable, int $depth, bool $limitDepth = true, int $level = 0): void
{
if ($depth <= $level) {
return;
}
if (is_object($variable)) {
if (array_key_exists($variable::class, $this->excludedClasses) ||
array_key_exists($objectDescription = $this->getObjectDescription($variable), $this->objects)
) {
return;
}
$this->objects[$objectDescription] = $variable;

$nextLevel = $limitDepth ? $level + 1 : 0;
if ($depth <= $nextLevel) {
return;
}
$variable = $this->getObjectProperties($variable);

foreach ($variable as $value) {
$this->buildObjectsCache($value, $depth, 0);
$this->buildObjectsCache($value, $depth, $limitDepth, $nextLevel);
}
return;
}

if (is_array($variable)) {
$nextLevel = $level + 1;
if ($depth <= $nextLevel) {
return;
}
foreach ($variable as $value) {
$this->buildObjectsCache($value, $depth, $nextLevel);
$this->buildObjectsCache($value, $depth, $limitDepth, $nextLevel);
}
}
}
Expand Down Expand Up @@ -149,10 +152,6 @@ private function dumpNestedInternal(
break;
case 'object':
$objectDescription = $this->getObjectDescription($variable);
if ($depth <= $level || array_key_exists($variable::class, $this->excludedClasses)) {
$output = $objectDescription . ' (...)';
break;
}

if ($variable instanceof Closure) {
$output = $inlineObject
Expand All @@ -161,13 +160,17 @@ private function dumpNestedInternal(
break;
}

if (!array_key_exists($objectDescription, $this->objects)) {
if ($objectCollapseLevel < $level && array_key_exists($objectDescription, $this->objects)) {
$output = 'object@' . $objectDescription;
$this->objects[$objectDescription] = $variable;
break;
}
if ($objectCollapseLevel < $level) {
$output = 'object@' . $objectDescription;

if (
$depth <= $level
|| array_key_exists($variable::class, $this->excludedClasses)
|| !array_key_exists($objectDescription, $this->objects)
) {
$output = $objectDescription . ' (...)';
break;
}

Expand Down
160 changes: 160 additions & 0 deletions tests/Unit/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,166 @@

final class DumperTest extends TestCase
{
/**
* @dataProvider loopAsJsonObjectMapDataProvider
*/
public function testLoopAsJsonObjectsMap(mixed $var, int $depth, $expectedResult): void
{
$exportResult = Dumper::create($var)->asJsonObjectsMap($depth, true);
$this->assertEquals($expectedResult, $exportResult);
}

public static function loopAsJsonObjectMapDataProvider(): iterable
{
// parent->child->parent structure
$nested1 = new stdClass();
$nested1->id = 'nested1';
$nested2 = new stdClass();
$nested2->id = 'nested2';
$nested2->nested1 = $nested1;
$nested1->nested2 = $nested2;

$nested1Id = spl_object_id($nested1);
$nested2Id = spl_object_id($nested2);

// 5 is a min level to reproduce buggy dumping of parent->child->parent structure
[$object1, $ids1] = self::getNested(5, $nested1);
yield 'nested loop - object' => [
$object1,
5,
<<<S
{
"stdClass#$ids1[0]": {
"public \$id": "lvl0",
"public \$lvl1": "object@stdClass#$ids1[1]"
},
"stdClass#$ids1[1]": {
"public \$id": "lvl1",
"public \$lvl2": "object@stdClass#$ids1[2]"
},
"stdClass#$ids1[2]": {
"public \$id": "lvl2",
"public \$lvl3": "object@stdClass#$ids1[3]"
},
"stdClass#$ids1[3]": {
"public \$id": "lvl3",
"public \$lvl4": "object@stdClass#$ids1[4]"
},
"stdClass#$ids1[4]": {
"public \$id": "lvl4",
"public \$lvl5": "object@stdClass#$nested1Id"
},
"stdClass#$nested1Id": {
"public \$id": "nested1",
"public \$nested2": "object@stdClass#$nested2Id"
},
"stdClass#$nested2Id": {
"public \$id": "nested2",
"public \$nested1": "object@stdClass#$nested1Id"
}
}
S,
];

// array loop must be 1 level deeper to parse loop objects
[$object2, $ids2] = self::getNested(6, [$nested1, $nested2]);
yield 'nested loop - array' => [
$object2,
6,
<<<S
{
"stdClass#$ids2[0]": {
"public \$id": "lvl0",
"public \$lvl1": "object@stdClass#$ids2[1]"
},
"stdClass#$ids2[1]": {
"public \$id": "lvl1",
"public \$lvl2": "object@stdClass#$ids2[2]"
},
"stdClass#$ids2[2]": {
"public \$id": "lvl2",
"public \$lvl3": "object@stdClass#$ids2[3]"
},
"stdClass#$ids2[3]": {
"public \$id": "lvl3",
"public \$lvl4": "object@stdClass#$ids2[4]"
},
"stdClass#$ids2[4]": {
"public \$id": "lvl4",
"public \$lvl5": "object@stdClass#$ids2[5]"
},
"stdClass#$ids2[5]": {
"public \$id": "lvl5",
"public \$lvl6": [
"object@stdClass#$nested1Id",
"object@stdClass#$nested2Id"
]
},
"stdClass#$nested1Id": {
"public \$id": "nested1",
"public \$nested2": "object@stdClass#$nested2Id"
},
"stdClass#$nested2Id": {
"public \$id": "nested2",
"public \$nested1": "object@stdClass#$nested1Id"
}
}
S,
];

// nested loop to inner array
$object3 = new stdClass();
$object3->id = 'lvl0';
$object3->lv11 = [
'id' => 'lvl1',
'loop' => $nested1,
];
$object3Id = spl_object_id($object3);

yield 'nested loop to object->array' => [
$object3,
3,
<<<S
{
"stdClass#$object3Id": {
"public \$id": "lvl0",
"public \$lv11": {
"id": "lvl1",
"loop": "object@stdClass#$nested1Id"
}
},
"stdClass#$nested1Id": {
"public \$id": "nested1",
"public \$nested2": "object@stdClass#$nested2Id"
},
"stdClass#$nested2Id": {
"public \$id": "nested2",
"public \$nested1": "object@stdClass#$nested1Id"
}
}
S,
];
}

private static function getNested(int $depth, mixed $data): array
{
$objectIds = [];
$head = $lvl = new stdClass();
$objectIds[] = spl_object_id($head);
$lvl->id = 'lvl0';

for ($i = 1; $i < $depth; $i++) {
$nested = new stdClass();
$nested->id = 'lvl' . $i;
$lvl->{'lvl' . $i} = $nested;
$lvl = $nested;
$objectIds[] = spl_object_id($nested);
}
$lvl->{'lvl' . $i} = $data;

return [$head, $objectIds];
}

public function testObjectExpanding(): void
{
$var = $this->createNested(10, [[[[[[[[['key' => 'end']]]]]]]]]);
Expand Down
Loading