Skip to content

Commit 92a1f61

Browse files
committed
Dumper: refactoring, added dumpCustomObject()
1 parent b74f58d commit 92a1f61

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

src/PhpGenerator/Dumper.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private function dumpVar(mixed &$var, array $parents = [], int $level = 0, int $
4949
return $this->dumpLiteral($var, $level);
5050

5151
} elseif (is_object($var)) {
52-
return $this->dumpObject($var, $parents, $level);
52+
return $this->dumpObject($var, $parents, $level, $column);
5353

5454
} elseif (is_resource($var)) {
5555
throw new Nette\InvalidArgumentException('Cannot dump resource.');
@@ -136,12 +136,21 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
136136

137137

138138
/** @param array<mixed[]|object> $parents */
139-
private function dumpObject(object $var, array $parents, int $level): string
139+
private function dumpObject(object $var, array $parents, int $level, int $column): string
140140
{
141+
if ($level > $this->maxDepth || in_array($var, $parents, strict: true)) {
142+
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
143+
}
144+
141145
$class = $var::class;
146+
$parents[] = $var;
142147

143-
if (in_array($class, [\DateTime::class, \DateTimeImmutable::class], strict: true)) {
144-
return $this->format("new \\$class(?, new \\DateTimeZone(?))", $var->format('Y-m-d H:i:s.u'), $var->getTimeZone()->getName());
148+
if ($class === \DateTime::class || $class === \DateTimeImmutable::class) {
149+
return $this->format(
150+
"new \\$class(?, new \\DateTimeZone(?))",
151+
$var->format('Y-m-d H:i:s.u'),
152+
$var->getTimeZone()->getName(),
153+
);
145154

146155
} elseif ($var instanceof \UnitEnum) {
147156
return '\\' . $var::class . '::' . $var->name;
@@ -159,9 +168,22 @@ private function dumpObject(object $var, array $parents, int $level): string
159168
} elseif ((new \ReflectionObject($var))->isAnonymous()) {
160169
throw new Nette\InvalidArgumentException('Cannot dump anonymous class.');
161170

162-
} elseif ($level > $this->maxDepth || in_array($var, $parents, strict: true)) {
163-
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
171+
} elseif ($class === \stdClass::class) {
172+
$var = (array) $var;
173+
return '(object) ' . $this->dumpArray($var, $parents, $level, $column + 10);
174+
175+
} else {
176+
return $this->dumpCustomObject($var, $parents, $level);
164177
}
178+
}
179+
180+
181+
/** @param array<mixed[]|object> $parents */
182+
private function dumpCustomObject(object $var, array $parents, int $level): string
183+
{
184+
$class = $var::class;
185+
$space = str_repeat($this->indentation, $level);
186+
$out = "\n";
165187

166188
if (method_exists($var, '__serialize')) {
167189
$arr = $var->__serialize();
@@ -174,10 +196,6 @@ private function dumpObject(object $var, array $parents, int $level): string
174196
}
175197
}
176198

177-
$space = str_repeat($this->indentation, $level);
178-
$out = "\n";
179-
$parents[] = $var;
180-
181199
foreach ($arr as $k => &$v) {
182200
if (!isset($props) || isset($props[$k])) {
183201
$out .= $space . $this->indentation
@@ -187,11 +205,7 @@ private function dumpObject(object $var, array $parents, int $level): string
187205
}
188206
}
189207

190-
array_pop($parents);
191-
$out .= $space;
192-
return $class === \stdClass::class
193-
? "(object) [$out]"
194-
: '\\' . self::class . "::createObject(\\$class::class, [$out])";
208+
return '\\' . self::class . "::createObject(\\$class::class, [$out$space])";
195209
}
196210

197211

tests/PhpGenerator/Dumper.dump().phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ Assert::same("[0 => 'a', -2 => 'b', 1 => 'c']", $dumper->dump(['a', -2 => 'b', 1
7070

7171
// stdClass
7272
Assert::same(
73-
"(object) [\n\t'a' => 1,\n\t'b' => 2,\n]",
73+
"(object) ['a' => 1, 'b' => 2]",
7474
$dumper->dump((object) ['a' => 1, 'b' => 2]),
7575
);
7676

7777
Assert::same(
78-
"(object) [\n\t'a' => (object) [\n\t\t'b' => 2,\n\t],\n]",
78+
"(object) ['a' => (object) ['b' => 2]]",
7979
$dumper->dump((object) ['a' => (object) ['b' => 2]]),
8080
);
8181

tests/PhpGenerator/Dumper.dump().wrap.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require __DIR__ . '/../bootstrap.php';
1313

1414

1515
$dumper = new Dumper;
16-
$dumper->wrapLength = 21;
16+
$dumper->wrapLength = 28;
1717
same(
1818
<<<'XX'
1919
[

0 commit comments

Comments
 (0)