Skip to content

Commit 4e84b97

Browse files
fancywebnicolas-grekas
authored andcommitted
[VarDumper][DebugBundle] Fix dump labels compatibility
1 parent 64a3403 commit 4e84b97

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

Cloner/Data.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,10 @@ public function dump(DumperInterface $dumper)
269269
{
270270
$refs = [0];
271271
$cursor = new Cursor();
272-
$label = $this->context['label'] ?? '';
273-
274-
if ($cursor->attr = $this->context[SourceContextProvider::class] ?? []) {
275-
$cursor->attr['if_links'] = true;
276-
$cursor->hashType = -1;
277-
$dumper->dumpScalar($cursor, 'default', $label.'^');
278-
$cursor->attr = ['if_links' => true];
279-
$dumper->dumpScalar($cursor, 'default', ' ');
280-
$cursor->hashType = 0;
281-
}
282-
272+
$cursor->hashType = -1;
273+
$cursor->attr = $this->context[SourceContextProvider::class] ?? [];
274+
$dumper->dumpScalar($cursor, 'label', $this->context['label'] ?? '');
275+
$cursor->hashType = 0;
283276
$this->dumpItem($dumper, $cursor, $refs, $this->data[$this->position][$this->key]);
284277
}
285278

Dumper/CliDumper.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,8 @@ public function dumpScalar(Cursor $cursor, string $type, string|int|float|bool|n
139139
$attr = $cursor->attr;
140140

141141
switch ($type) {
142-
case 'default':
143-
$style = 'default';
144-
break;
142+
case 'label': $style = 'label'; break;
143+
case 'default': $style = 'default'; break;
145144

146145
case 'integer':
147146
$style = 'num';
@@ -468,7 +467,7 @@ protected function style(string $style, string $value, array $attr = []): string
468467

469468
$map = static::$controlCharsMap;
470469
$startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : '';
471-
$endCchr = $this->colors ? "\033[m\033[{$this->styles[$style]}m" : '';
470+
$endCchr = $this->colors ? "\033[m\033[{$this->styles['label' === $style ? 'default' : $style]}m" : '';
472471
$value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) {
473472
$s = $startCchr;
474473
$c = $c[$i = 0];
@@ -487,11 +486,11 @@ protected function style(string $style, string $value, array $attr = []): string
487486
}, $value);
488487
}
489488

490-
if ($this->colors) {
489+
if ($this->colors && '' !== $value) {
491490
if ($cchrCount && "\033" === $value[0]) {
492491
$value = substr($value, \strlen($startCchr));
493492
} else {
494-
$value = "\033[{$this->styles[$style]}m".$value;
493+
$value = "\033[{$this->styles['label' === $style ? 'default' : $style]}m".$value;
495494
}
496495
if ($cchrCount && str_ends_with($value, $endCchr)) {
497496
$value = substr($value, 0, -\strlen($endCchr));
@@ -510,10 +509,15 @@ protected function style(string $style, string $value, array $attr = []): string
510509
}
511510
}
512511
if (isset($attr['href'])) {
512+
if ('label' === $style) {
513+
$value .= '^';
514+
}
513515
$value = "\033]8;;{$attr['href']}\033\\{$value}\033]8;;\033\\";
514516
}
515-
} elseif ($attr['if_links'] ?? false) {
516-
return '';
517+
}
518+
519+
if ('label' === $style && '' !== $value) {
520+
$value .= ' ';
517521
}
518522

519523
return $value;

Dumper/HtmlDumper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ public function leaveHash(Cursor $cursor, int $type, string|int|null $class, boo
849849

850850
protected function style(string $style, string $value, array $attr = []): string
851851
{
852-
if ('' === $value) {
852+
if ('' === $value && ('label' !== $style || !isset($attr['file']) && !isset($attr['href']))) {
853853
return '';
854854
}
855855

@@ -903,7 +903,7 @@ protected function style(string $style, string $value, array $attr = []): string
903903
}
904904

905905
$map = static::$controlCharsMap;
906-
$v = "<span class=sf-dump-{$style}>".preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
906+
$v = '<span class=sf-dump-'.('label' === $style ? 'default' : $style).'>'.preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
907907
$s = $b = '<span class="sf-dump-default';
908908
$c = $c[$i = 0];
909909
if ($ns = "\r" === $c[$i] || "\n" === $c[$i]) {
@@ -927,14 +927,17 @@ protected function style(string $style, string $value, array $attr = []): string
927927

928928
if (!($attr['binary'] ?? false)) {
929929
$v = preg_replace_callback(static::$unicodeCharsRx, function ($c) {
930-
return '<span class="sf-dump-default">\u{'.strtoupper(dechex(mb_ord($c[0]))).'}</span>';
930+
return '<span class=sf-dump-default>\u{'.strtoupper(dechex(mb_ord($c[0]))).'}</span>';
931931
}, $v);
932932
}
933933

934934
if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], $attr['line'] ?? 0)) {
935935
$attr['href'] = $href;
936936
}
937937
if (isset($attr['href'])) {
938+
if ('label' === $style) {
939+
$v .= '^';
940+
}
938941
$target = isset($attr['file']) ? '' : ' target="_blank"';
939942
$v = sprintf('<a href="%s"%s rel="noopener noreferrer">%s</a>', esc($this->utf8Encode($attr['href'])), $target, $v);
940943
}

Resources/functions/dump.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function dump(mixed ...$vars): mixed
2525
return null;
2626
}
2727

28-
if (isset($vars[0]) && 1 === count($vars)) {
28+
if (array_key_exists(0, $vars) && 1 === count($vars)) {
2929
VarDumper::dump($vars[0]);
3030
$k = 0;
3131
} else {

Tests/Dumper/HtmlDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testGet()
6969
<span class=sf-dump-str title="11 binary or non-UTF-8 characters">&#233;<span class="sf-dump-default">\\x01</span>test<span class="sf-dump-default">\\t</span><span class="sf-dump-default sf-dump-ns">\\n</span></span>
7070
<span class=sf-dump-str title="11 binary or non-UTF-8 characters">ing</span>
7171
"""
72-
"<span class=sf-dump-key>bo<span class="sf-dump-default">\\u{FEFF}</span>m</span>" => "<span class=sf-dump-str title="5 characters">te<span class="sf-dump-default">\\u{FEFF}</span>st</span>"
72+
"<span class=sf-dump-key>bo<span class=sf-dump-default>\\u{FEFF}</span>m</span>" => "<span class=sf-dump-str title="5 characters">te<span class=sf-dump-default>\\u{FEFF}</span>st</span>"
7373
"<span class=sf-dump-key>[]</span>" => []
7474
"<span class=sf-dump-key>res</span>" => <span class=sf-dump-note>stream resource</span> <a class=sf-dump-ref>@{$res}</a><samp data-depth=2 class=sf-dump-compact>
7575
%A <span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"

0 commit comments

Comments
 (0)