Skip to content

Commit

Permalink
improve + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 15, 2024
1 parent b5e1b83 commit 7ca1845
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 17 deletions.
33 changes: 18 additions & 15 deletions src/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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) ||
Expand All @@ -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);
}
}
}
Expand Down
95 changes: 93 additions & 2 deletions tests/Unit/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<<<JSON
{
"stdClass#$objectId": {
"public \$var": "test"
}
}
JSON,
Dumper::create($object)->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(
<<<JSON
{
"stdClass#$objectId": {
"public \$name": "root",
"public \$var": "object@stdClass#$nested1Id"
},
"stdClass#$nested1Id": {
"public \$name": "nested1",
"public \$var": "object@stdClass#$nested2Id"
},
"stdClass#$nested2Id": {
"public \$name": "nested2"
}
}
JSON,
Dumper::create($object)->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(
<<<JSON
{
"stdClass#$objectId": {
"public \$name": "root",
"public \$var": "object@stdClass#$nested1Id"
},
"stdClass#$nested1Id": {
"public \$name": "nested1",
"public \$var": "array (1 item) [...]"
},
"stdClass#$nested2Id": {
"public \$name": "nested2"
}
}
JSON,
Dumper::create($object)->asJsonObjectsMap(1, true)
);
}

/**
* @dataProvider loopAsJsonObjectMapDataProvider
*/
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -349,7 +440,7 @@ public function testCacheDoesNotCoversObjectOutOfDumpDepth(): void
$map = $dumper->asJsonObjectsMap(2);
$this->assertEqualsWithoutLE(
<<<S
{"stdClass#{$object1Id}":"{stateless object}"}
{"stdClass#$object1Id":"{stateless object}","stdClass#$object2Id":"{stateless object}"}
S,
$map,
);
Expand Down

0 comments on commit 7ca1845

Please sign in to comment.