Skip to content

Commit

Permalink
fix infinite loop caused by nested object property
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghostff committed Aug 10, 2020
1 parent 80a5448 commit 36d600a
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/Dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Dump
'integer' => ['1BAABB', 'light_green'],
'double' => ['9C6E25', 'cyan'],
'boolean' => ['bb02ff', 'purple'],
'keyword' => ['bb02ff', 'purple'],
'null' => ['6789f8', 'white'],
'type' => ['AAAAAA', 'dark_gray'],
'size' => ['5BA415', 'green'],
Expand Down Expand Up @@ -282,7 +283,7 @@ private function output(string $data)
}
else
{
echo '<code><small>' . $file . '</small><br />' . $data . '</code>';
echo "<code><small>{$file}</small><br />{$data}</code>";
}
}

Expand Down Expand Up @@ -321,7 +322,7 @@ private function color($value, string $name): string
*
* @return string
*/
private function type(string $type, string $before = ' '): string
private function type(string $type, string $before = ''): string
{
return "{$before}{$this->color($type, 'type')}";
}
Expand Down Expand Up @@ -536,13 +537,13 @@ private function formatObject($object): string
}

$prop->setAccessible(true);
if ($prop->isInitialized($object))
if (version_compare(PHP_VERSION, '7.4.0') >= 0)
{
$value = $this->evaluate([$prop->getValue($object)], true, true);
$value = $prop->isInitialized($object) ? $this->getValue($prop, $object, $class_name) : $this->type('uninitialized');
}
else
{
$value = $this->type('uninitialized');
$value = $this->getValue($prop, $object, $class_name);
}

$tmp .= "{$from} {$this->color("'{$prop->getName()}'", 'property_name')} {$arrow_color} {$value}";
Expand All @@ -565,6 +566,27 @@ private function formatObject($object): string
return $tmp;
}

/**
* Formats object property values.
*
* @param \ReflectionProperty $property
* @param $object
* @param string $class_name
*
* @return string
*/
private function getValue(ReflectionProperty $property, $object, string $class_name): string
{
$value = $property->getValue($object);
# Prevent infinite loop caused by nested object property. e.g. when an object property is pointing to the same
# object.
if (is_object($value) && $value instanceof $object && $value == $object) {
return "{$this->type($class_name)} {$this->color('::self', 'keyword')}";
}

return $this->evaluate([$value], true, true);
}

/**
* Couples all formats.
*
Expand Down

0 comments on commit 36d600a

Please sign in to comment.