Skip to content
Permalink
Browse files

Dumper: do not quote dumped keys [Closes #340]

  • Loading branch information...
Miloslav Hůla authored and dg committed Feb 15, 2019
1 parent d654908 commit 43dfafd7f7598ec52d63edba81618596f78faa63
Showing with 84 additions and 11 deletions.
  1. +16 −8 src/Tracy/Dumper.php
  2. +65 −0 tests/Tracy/Dumper.keys.phpt
  3. +3 −3 tests/Tracy/Dumper.objectExporters.phpt
@@ -218,9 +218,8 @@ private static function dumpArray(&$var, array $options, int $level): string
foreach ($var as $k => &$v) {
if ($k !== $marker) {
$hide = is_string($k) && isset($options[self::KEYS_TO_HIDE][strtolower($k)]) ? self::HIDDEN_VALUE : null;
$k = is_int($k) || preg_match('#^\w{1,50}\z#', $k) ? $k : '"' . Helpers::escapeHtml(self::encodeString($k, $options[self::TRUNCATE])) . '"';
$out .= '<span class="tracy-dump-indent"> ' . str_repeat('| ', $level) . '</span>'
. '<span class="tracy-dump-key">' . $k . '</span> => '
. '<span class="tracy-dump-key">' . Helpers::escapeHtml(self::encodeKey($k, $options)) . '</span> => '
. ($hide ? self::dumpString($hide, $options) : self::dumpVar($v, $options, $level + 1));
}
}
@@ -275,9 +274,8 @@ private static function dumpObject(&$var, array $options, int $level): string
$k = substr($k, strrpos($k, "\x00") + 1);
}
$hide = is_string($k) && isset($options[self::KEYS_TO_HIDE][strtolower($k)]) ? self::HIDDEN_VALUE : null;
$k = is_int($k) || preg_match('#^\w{1,50}\z#', $k) ? $k : '"' . Helpers::escapeHtml(self::encodeString($k, $options[self::TRUNCATE])) . '"';
$out .= '<span class="tracy-dump-indent"> ' . str_repeat('| ', $level) . '</span>'
. '<span class="tracy-dump-key">' . $k . "</span>$vis => "
. '<span class="tracy-dump-key">' . Helpers::escapeHtml(self::encodeKey($k, $options)) . "</span>$vis => "
. ($hide ? self::dumpString($hide, $options) : self::dumpVar($v, $options, $level + 1));
}
array_pop($list);
@@ -335,8 +333,7 @@ private static function toJson(&$var, array $options, int $level = 0)
foreach ($var as $k => &$v) {
if ($k !== $marker) {
$hide = is_string($k) && isset($options[self::KEYS_TO_HIDE][strtolower($k)]);
$k = is_int($k) || preg_match('#^\w{1,50}\z#', $k) ? $k : '"' . self::encodeString($k, $options[self::TRUNCATE]) . '"';
$res[] = [$k, $hide ? self::HIDDEN_VALUE : self::toJson($v, $options, $level + 1)];
$res[] = [self::encodeKey($k, $options), $hide ? self::HIDDEN_VALUE : self::toJson($v, $options, $level + 1)];
}
}
unset($var[$marker]);
@@ -374,8 +371,7 @@ private static function toJson(&$var, array $options, int $level = 0)
$k = substr($k, strrpos($k, "\x00") + 1);
}
$hide = is_string($k) && isset($options[self::KEYS_TO_HIDE][strtolower($k)]);
$k = is_int($k) || preg_match('#^\w{1,50}\z#', $k) ? $k : '"' . self::encodeString($k, $options[self::TRUNCATE]) . '"';
$obj['items'][] = [$k, $hide ? self::HIDDEN_VALUE : self::toJson($v, $options, $level + 1), $vis];
$obj['items'][] = [self::encodeKey($k, $options), $hide ? self::HIDDEN_VALUE : self::toJson($v, $options, $level + 1), $vis];
}
}
return ['object' => $obj['id']];
@@ -457,6 +453,18 @@ public static function encodeString(string $s, int $maxLength = null): string
}
/**
* @param int|string $k
* @return int|string
*/
private static function encodeKey($key, array $options)
{
return is_int($key) || preg_match('#^[!\#$%&()*+,./0-9:;<=>?@A-Z[\]^_`a-z{|}~-]{1,50}\z#', $key)
? $key
: '"' . self::encodeString($key, $options[self::TRUNCATE]) . '"';
}
/**
* @param object $obj
*/
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
use Tester\Assert;
use Tracy\Dumper;
require __DIR__ . '/../bootstrap.php';
$keys = [
'' => 0,
'"' => 0,
"'" => 0,
'key' => 0,
' key' => 0,
'key ' => 0,
0 => 0,
'01' => 0,
];
Assert::match('array (%i%)
"" => 0
""" => 0
"\'" => 0
key => 0
" key" => 0
"key " => 0
0 => 0
01 => 0
', Dumper::toText($keys));
Assert::match('stdClass #%a%
"" => 0
""" => 0
"\'" => 0
key => 0
" key" => 0
"key " => 0
0 => 0
01 => 0
', Dumper::toText((object) $keys));
Assert::match(
'<pre class="tracy-dump" data-tracy-dump=\'{"object":"01"}\'></pre>',
Dumper::toHtml((object) $keys, [Dumper::LIVE => true])
);
Assert::same([
'01' => [
'name' => 'stdClass',
'editor' => null,
'items' => [
['""', 0, 0],
['"""', 0, 0],
['"\'"', 0, 0],
['key', 0, 0],
['" key"', 0, 0],
['"key "', 0, 0],
[0, 0, 0],
['01', 0, 0],
],
],
], Dumper::fetchLiveData());
@@ -39,9 +39,9 @@ $obj = unserialize('O:1:"Y":7:{s:1:"a";N;s:1:"b";i:2;s:4:"' . "\0" . '*' . "\0"
Assert::match('__PHP_Incomplete_Class #%a%
className => "Y"
private => array (3)
| "Y::$e" => null
| "Y::$i" => "bar" (3)
| "X::$i" => "foo" (3)
| Y::$e => null
| Y::$i => "bar" (3)
| X::$i => "foo" (3)
protected => array (2)
| c => null
| d => "d"

0 comments on commit 43dfafd

Please sign in to comment.
You can’t perform that action at this time.