From dba50097581bf9fa97fc8012e8bcee349440da41 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 variable's type [Closes #380] --- src/Tracy/Dumper/Dumper.php | 24 ++++++++++++++++++------ tests/Tracy/Dumper.keysToHide.phpt | 24 ++++++++++++------------ 2 files changed, 30 insertions(+), 18 deletions(-) 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] */ diff --git a/tests/Tracy/Dumper.keysToHide.phpt b/tests/Tracy/Dumper.keysToHide.phpt index 054ef6182..fb523bdd1 100644 --- a/tests/Tracy/Dumper.keysToHide.phpt +++ b/tests/Tracy/Dumper.keysToHide.phpt @@ -26,14 +26,14 @@ $obj = (object) [ Assert::match('stdClass #%a% a => 456 - password => "*****" (5) - PASSWORD => "*****" (5) - Pin => "*****" (5) + password => ***** (string) + PASSWORD => ***** (string) + Pin => ***** (string) inner => array (4) | a => 123 - | password => "*****" (5) - | PASSWORD => "*****" (5) - | Pin => "*****" (5) + | password => ***** (string) + | PASSWORD => ***** (string) + | Pin => ***** (string) ', Dumper::toText($obj, [Dumper::KEYS_TO_HIDE => ['password', 'PIN']])); @@ -49,16 +49,16 @@ Assert::equal([ 'hash' => Expect::match('%h%'), 'items' => [ ['a', 456, 0], - ['password', '*****', 0], - ['PASSWORD', '*****', 0], - ['Pin', '*****', 0], + ['password', ['type' => '***** (string)'], 0], + ['PASSWORD', ['type' => '***** (string)'], 0], + ['Pin', ['type' => '***** (string)'], 0], [ 'inner', [ ['a', 123], - ['password', '*****'], - ['PASSWORD', '*****'], - ['Pin', '*****'], + ['password', ['type' => '***** (string)']], + ['PASSWORD', ['type' => '***** (string)']], + ['Pin', ['type' => '***** (string)']], ], 0, ],