From b4160e39602f2d0c4626fca0c9b3d269d3f99691 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 30 Aug 2019 13:46:44 +0200 Subject: [PATCH] Dumper: hidden values are rendered with type of variable [Closes #380] --- src/Tracy/BlueScreen/BlueScreen.php | 15 ++++++++------- src/Tracy/Dumper/Dumper.php | 24 ++++++++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Tracy/BlueScreen/BlueScreen.php b/src/Tracy/BlueScreen/BlueScreen.php index 4d9052293..6fad73da1 100644 --- a/src/Tracy/BlueScreen/BlueScreen.php +++ b/src/Tracy/BlueScreen/BlueScreen.php @@ -274,13 +274,14 @@ public static function highlightPhp(string $source, int $line, int $lines = 15, if ($vars) { $out = preg_replace_callback('#">\$(\w+)( )?#', function (array $m) use ($vars, $keysToHide): string { - $dump = Dumper::toHtml($vars[$m[1]], [ - Dumper::DEPTH => 1, - Dumper::KEYS_TO_HIDE => $keysToHide, - ]); - return array_key_exists($m[1], $vars) - ? '" title="' . str_replace('"', '"', trim(strip_tags($dump))) . $m[0] - : $m[0]; + if (array_key_exists($m[1], $vars)) { + $dump = Dumper::toHtml($vars[$m[1]], [ + Dumper::DEPTH => 1, + Dumper::KEYS_TO_HIDE => $keysToHide, + ]); + return '" title="' . str_replace('"', '"', trim(strip_tags($dump))) . $m[0]; + } + return $m[0]; }, $out); } diff --git a/src/Tracy/Dumper/Dumper.php b/src/Tracy/Dumper/Dumper.php index d08f913ca..54e8ec6e8 100644 --- a/src/Tracy/Dumper/Dumper.php +++ b/src/Tracy/Dumper/Dumper.php @@ -297,10 +297,13 @@ private function dumpArray(&$var, array $options, int $level): string $out = $span . '>' . $out . count($var) . ")\n" . ''; $options['parents'][] = $var; foreach ($var as $k => &$v) { - $hide = is_string($k) && isset($this->keysToHide[strtolower($k)]) ? self::HIDDEN_VALUE : null; + $hide = is_string($k) && isset($this->keysToHide[strtolower($k)]); $out .= ' ' . str_repeat('| ', $level) . '' . '' . Helpers::escapeHtml($this->encodeKey($k)) . ' => ' - . ($hide ? $this->dumpString($hide) : $this->dumpVar($v, $options, $level + 1)); + . ($hide + ? Helpers::escapeHtml(self::hideValue($v)) . "\n" + : $this->dumpVar($v, $options, $level + 1) + ); } array_pop($options['parents']); @@ -361,10 +364,13 @@ private function dumpObject(&$var, array $options, int $level): string $vis = ' ' . ($k[1] === '*' ? 'protected' : 'private') . ''; $k = substr($k, strrpos($k, "\x00") + 1); } - $hide = is_string($k) && isset($this->keysToHide[strtolower($k)]) ? self::HIDDEN_VALUE : null; + $hide = is_string($k) && isset($this->keysToHide[strtolower($k)]); $out .= ' ' . str_repeat('| ', $level) . '' . '' . Helpers::escapeHtml($this->encodeKey($k)) . "$vis => " - . ($hide ? $this->dumpString($hide) : $this->dumpVar($v, $options, $level + 1)); + . ($hide + ? Helpers::escapeHtml(self::hideValue($v)) . "\n" + : $this->dumpVar($v, $options, $level + 1) + ); } array_pop($options['parents']); @@ -419,7 +425,7 @@ private function toJson(&$var, array $options = [], int $level = 0) $options['parents'][] = $var; foreach ($var as $k => &$v) { $hide = is_string($k) && isset($this->keysToHide[strtolower($k)]); - $res[] = [$this->encodeKey($k), $hide ? self::HIDDEN_VALUE : $this->toJson($v, $options, $level + 1)]; + $res[] = [$this->encodeKey($k), $hide ? ['type' => self::hideValue($v)] : $this->toJson($v, $options, $level + 1)]; } array_pop($options['parents']); return $res; @@ -456,7 +462,7 @@ private function toJson(&$var, array $options = [], int $level = 0) $k = substr($k, strrpos($k, "\x00") + 1); } $hide = is_string($k) && isset($this->keysToHide[strtolower($k)]); - $obj['items'][] = [$this->encodeKey($k), $hide ? self::HIDDEN_VALUE : $this->toJson($v, $options, $level + 1), $vis]; + $obj['items'][] = [$this->encodeKey($k), $hide ? ['type' => self::hideValue($v)] : $this->toJson($v, $options, $level + 1), $vis]; } } return ['object' => $obj['id']]; @@ -619,6 +625,12 @@ private static function exportPhpIncompleteClass(\__PHP_Incomplete_Class $obj): } + private static function hideValue($var): string + { + return self::HIDDEN_VALUE . ' (' . (is_object($var) ? Helpers::getClass($var) : gettype($var)) . ')'; + } + + /** * Finds the location where dump was called. Returns [file, line, code] */