Skip to content

Commit

Permalink
Dumper: hidden values are rendered with variable's type [Closes #380]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 13, 2019
1 parent be4f4ac commit dba5009
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
24 changes: 18 additions & 6 deletions src/Tracy/Dumper/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,13 @@ private function dumpArray(&$var, array $options, int $level): string
$out = $span . '>' . $out . count($var) . ")</span>\n" . '<div' . ($collapsed ? ' class="tracy-collapsed"' : '') . '>';
$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 .= '<span class="tracy-dump-indent"> ' . str_repeat('| ', $level) . '</span>'
. '<span class="tracy-dump-key">' . Helpers::escapeHtml($this->encodeKey($k)) . '</span> => '
. ($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']);

Expand Down Expand Up @@ -361,10 +364,13 @@ private function dumpObject(&$var, array $options, int $level): string
$vis = ' <span class="tracy-dump-visibility">' . ($k[1] === '*' ? 'protected' : 'private') . '</span>';
$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 .= '<span class="tracy-dump-indent"> ' . str_repeat('| ', $level) . '</span>'
. '<span class="tracy-dump-key">' . Helpers::escapeHtml($this->encodeKey($k)) . "</span>$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']);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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']];
Expand Down Expand Up @@ -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]
*/
Expand Down
24 changes: 12 additions & 12 deletions tests/Tracy/Dumper.keysToHide.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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']]));


Expand All @@ -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,
],
Expand Down

0 comments on commit dba5009

Please sign in to comment.