Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4037141

Browse files
committed
Fixing tests for class src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php
1 parent c7cc20a commit 4037141

File tree

8 files changed

+125
-110
lines changed

8 files changed

+125
-110
lines changed

src/Symfony/Component/VarDumper/Cloner/Data.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\VarDumper\Cloner;
1313

1414
use Symfony\Component\VarDumper\Caster\Caster;
15+
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
1516

1617
/**
1718
* @author Nicolas Grekas <[email protected]>
@@ -24,13 +25,16 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate
2425
private $maxDepth = 20;
2526
private $maxItemsPerDepth = -1;
2627
private $useRefHandles = -1;
28+
private $file = null;
29+
private $line = null;
2730

2831
/**
2932
* @param array $data An array as returned by ClonerInterface::cloneVar()
3033
*/
3134
public function __construct(array $data)
3235
{
3336
$this->data = $data;
37+
['file' => $this->file, 'line' => $this->line] = (new SourceContextProvider())->getContext();
3438
}
3539

3640
/**
@@ -280,8 +284,11 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
280284
$cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
281285
$firstSeen = true;
282286

287+
$cursor->attr = [
288+
'file' => $this->file,
289+
'line' => $this->line
290+
];
283291
if (!$item instanceof Stub) {
284-
$cursor->attr = [];
285292
$type = \gettype($item);
286293
if ($item && 'array' === $type) {
287294
$item = $this->getStub($item);
@@ -297,7 +304,7 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
297304
$cursor->hardRefHandle = $this->useRefHandles & $item->handle;
298305
$cursor->hardRefCount = $item->refCount;
299306
}
300-
$cursor->attr = $item->attr;
307+
$cursor->attr += $item->attr;
301308
$type = $item->class ?: \gettype($item->value);
302309
$item = $this->getStub($item->value);
303310
}
@@ -312,7 +319,7 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
312319
}
313320
$cursor->softRefHandle = $this->useRefHandles & $item->handle;
314321
$cursor->softRefCount = $item->refCount;
315-
$cursor->attr = $item->attr;
322+
$cursor->attr += $item->attr;
316323
$cut = $item->cut;
317324

318325
if ($item->position && $firstSeen) {
@@ -420,4 +427,25 @@ private function getStub($item)
420427

421428
return $stub;
422429
}
430+
431+
public function getFile(): ?string
432+
{
433+
return $this->file;
434+
}
435+
436+
public function setFile(?string $file): void
437+
{
438+
$this->file = $file;
439+
}
440+
441+
public function getLine(): ?string
442+
{
443+
return $this->line;
444+
}
445+
446+
public function setLine(?string $line): void
447+
{
448+
$this->line = $line;
449+
}
450+
423451
}

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

Lines changed: 49 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
class CliDumper extends AbstractDumper
2626
{
27+
const DUMP_DUMP_LOCATION = 16;
28+
2729
public static $defaultColors;
2830
public static $defaultOutput = 'php://stdout';
2931

@@ -88,6 +90,8 @@ public function __construct($output = null, string $charset = null, int $flags =
8890
]);
8991
}
9092

93+
$this->handlesHrefGracefully = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR');
94+
9195
$this->displayOptions['fileLinkFormat'] = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f';
9296
}
9397

@@ -131,37 +135,6 @@ public function setDisplayOptions(array $displayOptions)
131135
$this->displayOptions = $displayOptions + $this->displayOptions;
132136
}
133137

134-
/**
135-
* {@inheritdoc}
136-
*/
137-
public function dump(Data $data, $output = null, array $extraDisplayOptions = [])
138-
{
139-
$this->extraDisplayOptions = $extraDisplayOptions;
140-
$result = parent::dump($data, $output);
141-
142-
return $result;
143-
}
144-
145-
/**
146-
* Configures file of the dump call function.
147-
*
148-
* @param string $file File path issued from debug_backtrace
149-
*/
150-
public function setFile(string $file)
151-
{
152-
$this->attr['file'] = $file;
153-
}
154-
155-
/**
156-
* Configures line of the dump call function.
157-
*
158-
* @param int $line File line issued from debug_backtrace
159-
*/
160-
public function setLine(int $line)
161-
{
162-
$this->attr['line'] = $line;
163-
}
164-
165138
/**
166139
* {@inheritdoc}
167140
*/
@@ -334,7 +307,7 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
334307
$prefix = substr($prefix, 0, -1);
335308
}
336309

337-
$this->line .= $prefix;
310+
$this->line .= $prefix . $this->dumpLocation($cursor, $attr);
338311

339312
if ($hasChild) {
340313
$this->dumpLine($cursor->depth);
@@ -470,10 +443,6 @@ protected function style($style, $value, $attr = [])
470443
$this->colors = $this->supportsColors();
471444
}
472445

473-
if (null === $this->handlesHrefGracefully) {
474-
$this->handlesHrefGracefully = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR');
475-
}
476-
477446
if (isset($attr['ellipsis'], $attr['ellipsis-type'])) {
478447
$prefix = substr($value, 0, -$attr['ellipsis']);
479448
if ('cli' === \PHP_SAPI && 'path' === $attr['ellipsis-type'] && isset($_SERVER[$pwd = '\\' === \DIRECTORY_SEPARATOR ? 'CD' : 'PWD']) && 0 === strpos($prefix, $_SERVER[$pwd])) {
@@ -487,47 +456,31 @@ protected function style($style, $value, $attr = [])
487456
}
488457

489458
$value = $this->style('default', $prefix).$this->style($style, $value);
490-
491-
goto href;
492-
}
493-
494-
$map = static::$controlCharsMap;
495-
$startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : '';
496-
$endCchr = $this->colors ? "\033[m\033[{$this->styles[$style]}m" : '';
497-
$value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) {
498-
$s = $startCchr;
499-
$c = $c[$i = 0];
500-
do {
501-
$s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', \ord($c[$i]));
502-
} while (isset($c[++$i]));
503-
504-
return $s.$endCchr;
505-
}, $value, -1, $cchrCount);
506-
507-
if ($this->colors) {
508-
if ($cchrCount && "\033" === $value[0]) {
509-
$value = substr($value, \strlen($startCchr));
510-
} else {
511-
$value = "\033[{$this->styles[$style]}m".$value;
512-
}
513-
if ($cchrCount && $endCchr === substr($value, -\strlen($endCchr))) {
514-
$value = substr($value, 0, -\strlen($endCchr));
515-
} else {
516-
$value .= "\033[{$this->styles['default']}m";
517-
}
518-
}
519-
520-
href:
521-
if ($this->colors && $this->handlesHrefGracefully) {
522-
if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], isset($attr['line']) ? $attr['line'] : 0)) {
523-
if ('note' === $style) {
524-
$value .= "\033]8;;{$href}\033\\^\033]8;;\033\\";
459+
} else {
460+
$map = static::$controlCharsMap;
461+
$startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : '';
462+
$endCchr = $this->colors ? "\033[m\033[{$this->styles[$style]}m" : '';
463+
$value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) {
464+
$s = $startCchr;
465+
$c = $c[$i = 0];
466+
do {
467+
$s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', \ord($c[$i]));
468+
} while (isset($c[++$i]));
469+
470+
return $s.$endCchr;
471+
}, $value, -1, $cchrCount);
472+
473+
if ($this->colors) {
474+
if ($cchrCount && "\033" === $value[0]) {
475+
$value = substr($value, \strlen($startCchr));
525476
} else {
526-
$attr['href'] = $href;
477+
$value = "\033[{$this->styles[$style]}m".$value;
478+
}
479+
if ($cchrCount && $endCchr === substr($value, -\strlen($endCchr))) {
480+
$value = substr($value, 0, -\strlen($endCchr));
481+
} else {
482+
$value .= "\033[{$this->styles['default']}m";
527483
}
528-
}
529-
if (isset($attr['href'])) {
530-
$value .= " \033]8;;{$attr['href']}\033\\[^]\033]8;;\033\\";
531484
}
532485
}
533486

@@ -668,10 +621,29 @@ private function isWindowsTrueColor()
668621
return $result;
669622
}
670623

671-
private function getSourceLink($file, $line)
624+
/**
625+
* Return the substring with the dump() function file location to get displayed with a small caret after
626+
* an hash.
627+
*
628+
* @param Cursor $cursor
629+
* @param array $attr
630+
* @return string
631+
*/
632+
private function dumpLocation(Cursor $cursor, $attr = []) :string
633+
{
634+
if ($this->handlesHrefGracefully && $cursor->depth === 0 && isset($attr['file']) && $href = $this->getSourceLink($attr['file'], $attr['line'])) {
635+
$color = $this->colors ? "\033" : '';
636+
637+
return " {$color}]8;;{$href}{$color}\\[^]{$color}]8;;{$color}\\";
638+
}
639+
640+
return '';
641+
}
642+
643+
private function getSourceLink($file, $lineNumber)
672644
{
673645
if ($fmt = $this->displayOptions['fileLinkFormat']) {
674-
return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : ($fmt->format($file, $line) ?: 'file://'.$file);
646+
return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $lineNumber]) : ($fmt->format($file, $lineNumber) ?: 'file://'.$file);
675647
}
676648

677649
return false;

src/Symfony/Component/VarDumper/Dumper/ContextProvider/CliContextProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ public function getContext(): ?array
2424
return null;
2525
}
2626

27+
['file' => $file, 'line' => $line] = (new SourceContextProvider())->getContext();
28+
2729
return [
2830
'command_line' => $commandLine = implode(' ', $_SERVER['argv']),
2931
'identifier' => hash('crc32b', $commandLine.$_SERVER['REQUEST_TIME_FLOAT']),
32+
'source' => [
33+
'file' => $file,
34+
'line' => $line
35+
]
3036
];
3137
}
3238
}

src/Symfony/Component/VarDumper/Resources/functions/dump.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
*/
1818
function dump($var, ...$moreVars)
1919
{
20-
$backtrace = debug_backtrace();
21-
VarDumper::dump($var, $backtrace);
20+
VarDumper::dump($var);
2221

2322
foreach ($moreVars as $v) {
24-
VarDumper::dump($v, $backtrace);
23+
VarDumper::dump($v);
2524
}
2625

2726
if (1 < func_num_args()) {
@@ -35,9 +34,8 @@ function dump($var, ...$moreVars)
3534
if (!function_exists('dd')) {
3635
function dd(...$vars)
3736
{
38-
$backtrace = debug_backtrace();
3937
foreach ($vars as $v) {
40-
VarDumper::dump($v, $backtrace);
38+
VarDumper::dump($v);
4139
}
4240

4341
die(1);

src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ protected function getDump($data, $key = null, $filter = 0)
4040
$dumper = new CliDumper(null, null, $flags);
4141
$dumper->setColors(false);
4242
$data = $cloner->cloneVar($data, $filter)->withRefHandles(false);
43+
$data->setFile(null);
44+
$data->setLine(null);
4345
if (null !== $key && null === $data = $data->seek($key)) {
4446
return;
4547
}

src/Symfony/Component/VarDumper/Tests/Caster/IntlCasterTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public function testCastNumberFormatter()
4040

4141
$expectedLocale = $var->getLocale();
4242
$expectedPattern = $var->getPattern();
43-
dump("Here is a dummy dump.");
4443

4544
$expectedAttribute1 = $var->getAttribute(\NumberFormatter::PARSE_INT_ONLY);
4645
$expectedAttribute2 = $var->getAttribute(\NumberFormatter::GROUPING_USED);

0 commit comments

Comments
 (0)