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

Skip to content

Commit 37554f3

Browse files
author
Amrouche Hamza
committed
[VarDumper] fix trailling comma when dumping an exception
1 parent e7b555e commit 37554f3

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

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

1414
use Symfony\Component\VarDumper\Cloner\Cursor;
15+
use Symfony\Component\VarDumper\Cloner\Stub;
1516

1617
/**
1718
* CliDumper dumps variables for command line output.
@@ -494,9 +495,10 @@ protected function dumpLine($depth, $endOfValue = false)
494495

495496
protected function endValue(Cursor $cursor)
496497
{
497-
if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) {
498+
$isStubArray = (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType);
499+
if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth && $isStubArray) {
498500
$this->line .= ',';
499-
} elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) {
501+
} elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex && $isStubArray) {
500502
$this->line .= ',';
501503
}
502504

src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,36 @@ public function testDumpWithCommaFlag($expected, $flags)
130130
$this->assertSame($expected, $dump);
131131
}
132132

133+
public function testDumpWithCommaFlagsAndExceptionCodeExcerpt()
134+
{
135+
$dumper = new CliDumper(null, null, CliDumper::DUMP_TRAILING_COMMA);
136+
$dumper->setColors(false);
137+
$cloner = new VarCloner();
138+
139+
$ex = new \RuntimeException('foo');
140+
141+
$dump = $dumper->dump($cloner->cloneVar($ex)->withRefHandles(false), true);
142+
143+
$this->assertStringMatchesFormat(<<<'EOTXT'
144+
RuntimeException {
145+
#message: "foo"
146+
#code: 0
147+
#file: "%A/CliDumperTest.php"
148+
#line: %d
149+
trace: {
150+
%A/CliDumperTest.php:%d: {
151+
:
152+
: $ex = new \RuntimeException('foo');
153+
:
154+
}
155+
%A
156+
}
157+
}
158+
159+
EOTXT
160+
, $dump);
161+
}
162+
133163
public function provideDumpWithCommaFlagTests()
134164
{
135165
$expected = <<<'EOTXT'

0 commit comments

Comments
 (0)