Skip to content

Commit

Permalink
Dumper::dumpObject() refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 12, 2022
1 parent 90c76b5 commit ef5978d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
35 changes: 17 additions & 18 deletions src/PhpGenerator/Dumper.php
Expand Up @@ -139,9 +139,9 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
private function dumpObject(object $var, array $parents, int $level): string
{
$class = $var::class;
if (method_exists($var, '__serialize')) {
$data = $this->dump($var->__serialize());
return '\\' . self::class . "::createObject(\\$class::class, $data)";

if (in_array($class, [\DateTime::class, \DateTimeImmutable::class], true)) {
return $this->format("new \\$class(?, new \\DateTimeZone(?))", $var->format('Y-m-d H:i:s.u'), $var->getTimeZone()->getName());

} elseif ($var instanceof \Serializable) { // deprecated
return 'unserialize(' . $this->dumpString(serialize($var)) . ')';
Expand All @@ -158,29 +158,28 @@ private function dumpObject(object $var, array $parents, int $level): string
}

throw new Nette\InvalidArgumentException('Cannot dump closure.');
}

if ((new \ReflectionObject($var))->isAnonymous()) {
} elseif ((new \ReflectionObject($var))->isAnonymous()) {
throw new Nette\InvalidArgumentException('Cannot dump anonymous class.');

} elseif (in_array($class, [\DateTime::class, \DateTimeImmutable::class], true)) {
return $this->format("new \\$class(?, new \\DateTimeZone(?))", $var->format('Y-m-d H:i:s.u'), $var->getTimeZone()->getName());
} elseif ($level > $this->maxDepth || in_array($var, $parents, true)) {
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
}

$arr = (array) $var;
$space = str_repeat($this->indentation, $level);

if ($level > $this->maxDepth || in_array($var, $parents, true)) {
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
if (method_exists($var, '__serialize')) {
$arr = $var->__serialize();
} else {
$arr = (array) $var;
if (method_exists($var, '__sleep')) {
foreach ($var->__sleep() as $v) {
$props[$v] = $props["\x00*\x00$v"] = $props["\x00$class\x00$v"] = true;
}
}
}

$space = str_repeat($this->indentation, $level);
$out = "\n";
$parents[] = $var;
if (method_exists($var, '__sleep')) {
foreach ($var->__sleep() as $v) {
$props[$v] = $props["\x00*\x00$v"] = $props["\x00$class\x00$v"] = true;
}
}

foreach ($arr as $k => &$v) {
if (!isset($props) || isset($props[$k])) {
Expand All @@ -195,7 +194,7 @@ private function dumpObject(object $var, array $parents, int $level): string
$out .= $space;
return $class === \stdClass::class
? "(object) [$out]"
: '\\' . self::class . "::createObject('$class', [$out])";
: '\\' . self::class . "::createObject(\\$class::class, [$out])";
}


Expand Down
10 changes: 5 additions & 5 deletions tests/PhpGenerator/Dumper.dump().phpt
Expand Up @@ -86,7 +86,7 @@ class Test
}

Assert::same(
"\\Nette\\PhpGenerator\\Dumper::createObject('Test', [\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n\t\"\\x00Test\\x00c\" => 3,\n])",
"\\Nette\\PhpGenerator\\Dumper::createObject(\\Test::class, [\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n\t\"\\x00Test\\x00c\" => 3,\n])",
$dumper->dump(new Test),
);
Assert::equal(new Test, eval('return ' . $dumper->dump(new Test) . ';'));
Expand All @@ -112,8 +112,8 @@ class Test2 extends Test

Assert::same(
PHP_VERSION_ID < 80100
? "\\Nette\\PhpGenerator\\Dumper::createObject('Test2', [\n\t\"\\x00Test2\\x00c\" => 4,\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n])"
: "\\Nette\\PhpGenerator\\Dumper::createObject('Test2', [\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n\t\"\\x00Test2\\x00c\" => 4,\n])",
? "\\Nette\\PhpGenerator\\Dumper::createObject(\\Test2::class, [\n\t\"\\x00Test2\\x00c\" => 4,\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n])"
: "\\Nette\\PhpGenerator\\Dumper::createObject(\\Test2::class, [\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n\t\"\\x00Test2\\x00c\" => 4,\n])",
$dumper->dump(new Test2),
);
Assert::equal(new Test2, eval('return ' . $dumper->dump(new Test2) . ';'));
Expand Down Expand Up @@ -188,7 +188,7 @@ class TestSer


$dumper = new Dumper;
Assert::same('\Nette\PhpGenerator\Dumper::createObject(\TestSer::class, [\'a\', \'b\'])', $dumper->dump(new TestSer));
Assert::same("\\Nette\\PhpGenerator\\Dumper::createObject(\\TestSer::class, [\n\t0 => 'a',\n\t1 => 'b',\n])", $dumper->dump(new TestSer));
Assert::equal(new TestSer, eval('return ' . $dumper->dump(new TestSer) . ';'));


Expand All @@ -207,7 +207,7 @@ Assert::same(
$dumper->dump(new DateTimeImmutable('2016-06-22 20:52:43.1234', new DateTimeZone('Europe/Prague'))),
);
same(
"\\Nette\\PhpGenerator\\Dumper::createObject('TestDateTime', [
"\\Nette\\PhpGenerator\\Dumper::createObject(\\TestDateTime::class, [
'date' => '2016-06-22 20:52:43.123400',
'timezone_type' => 3,
'timezone' => 'Europe/Prague',
Expand Down

0 comments on commit ef5978d

Please sign in to comment.