Skip to content

Commit 6d9a508

Browse files
[VarDumper] display the method we're in when dumping stack traces
1 parent 8e2bc5f commit 6d9a508

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
206206
$f['file'] = substr($f['file'], 0, -\strlen($match[0]));
207207
$f['line'] = (int) $match[1];
208208
}
209-
$caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null;
210209
$src = $f['line'];
211210
$srcKey = $f['file'];
212211
$ellipsis = new LinkStub($srcKey, 0);
@@ -226,13 +225,13 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
226225
$templatePath = null;
227226
}
228227
if ($templateSrc) {
229-
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, $caller, 'twig', $templatePath);
228+
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f);
230229
$srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
231230
}
232231
}
233232
}
234233
if ($srcKey == $f['file']) {
235-
$src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, $caller, 'php', $f['file']);
234+
$src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, 'php', $f['file'], $f);
236235
$srcKey .= ':'.$f['line'];
237236
if ($ellipsis) {
238237
$ellipsis += 1 + \strlen($f['line']);
@@ -308,7 +307,7 @@ private static function traceUnshift(array &$trace, ?string $class, string $file
308307
]);
309308
}
310309

311-
private static function extractSource(string $srcLines, int $line, int $srcContext, ?string $title, string $lang, string $file = null): EnumStub
310+
private static function extractSource(string $srcLines, int $line, int $srcContext, string $lang, ?string $file, array $frame): EnumStub
312311
{
313312
$srcLines = explode("\n", $srcLines);
314313
$src = [];
@@ -317,7 +316,33 @@ private static function extractSource(string $srcLines, int $line, int $srcConte
317316
$src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n";
318317
}
319318

320-
$srcLines = [];
319+
320+
if ($frame['function'] ?? false) {
321+
$stub = new CutStub(new \stdClass());
322+
$stub->class = (isset($frame['class']) ? $frame['class'].$frame['type'] : '').$frame['function'];
323+
$stub->type = Stub::TYPE_OBJECT;
324+
$stub->attr['cut_hash'] = true;
325+
$stub->attr['file'] = $frame['file'];
326+
$stub->attr['line'] = $frame['line'];
327+
328+
try {
329+
$caller = isset($frame['class']) ? new \ReflectionMethod($frame['class'], $frame['function']) : new \ReflectionFunction($frame['function']);
330+
$stub->class .= ReflectionCaster::getSignature(ReflectionCaster::castFunctionAbstract($caller, [], $stub, true, Caster::EXCLUDE_VERBOSE));
331+
332+
if ($f = $caller->getFileName()) {
333+
$stub->attr['file'] = $f;
334+
$stub->attr['line'] = $caller->getStartLine();
335+
}
336+
} catch (\ReflectionException $e) {
337+
// ignore fake class/function
338+
}
339+
340+
$srcLines = ["\0~separator=\0" => $stub];
341+
} else {
342+
$stub = null;
343+
$srcLines = [];
344+
}
345+
321346
$ltrim = 0;
322347
do {
323348
$pad = null;
@@ -344,7 +369,7 @@ private static function extractSource(string $srcLines, int $line, int $srcConte
344369
if ($i !== $srcContext) {
345370
$c = new ConstStub('default', $c);
346371
} else {
347-
$c = new ConstStub($c, $title);
372+
$c = new ConstStub($c, $stub ? 'in '.$stub->class : '');
348373
if (null !== $file) {
349374
$c->attr['file'] = $file;
350375
$c->attr['line'] = $line;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,14 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
283283

284284
$class = $this->utf8Encode($class);
285285
if (Cursor::HASH_OBJECT === $type) {
286-
$prefix = $class && 'stdClass' !== $class ? $this->style('note', $class, $attr).' {' : '{';
286+
$prefix = $class && 'stdClass' !== $class ? $this->style('note', $class, $attr).(empty($attr['cut_hash']) ? ' {' : '') : '{';
287287
} elseif (Cursor::HASH_RESOURCE === $type) {
288288
$prefix = $this->style('note', $class.' resource', $attr).($hasChild ? ' {' : ' ');
289289
} else {
290290
$prefix = $class && !(self::DUMP_LIGHT_ARRAY & $this->flags) ? $this->style('note', 'array:'.$class, $attr).' [' : '[';
291291
}
292292

293-
if ($cursor->softRefCount || 0 < $cursor->softRefHandle) {
293+
if (($cursor->softRefCount || 0 < $cursor->softRefHandle) && empty($attr['cut_hash'])) {
294294
$prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), ['count' => $cursor->softRefCount]);
295295
} elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) {
296296
$prefix .= $this->style('ref', '&'.$cursor->hardRefTo, ['count' => $cursor->hardRefCount]);
@@ -310,8 +310,11 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
310310
*/
311311
public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
312312
{
313-
$this->dumpEllipsis($cursor, $hasChild, $cut);
314-
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
313+
if (empty($cursor->attr['cut_hash'])) {
314+
$this->dumpEllipsis($cursor, $hasChild, $cut);
315+
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
316+
}
317+
315318
$this->endValue($cursor);
316319
}
317320

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function testDefaultSettings()
4747
#line: 28
4848
trace: {
4949
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
50+
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
5051
› {
5152
› return new \Exception(''.$msg);
5253
› }
@@ -66,11 +67,12 @@ public function testSeek()
6667
$expectedDump = <<<'EODUMP'
6768
{
6869
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
70+
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
6971
› {
7072
› return new \Exception(''.$msg);
7173
› }
7274
}
73-
%s%eTests%eCaster%eExceptionCasterTest.php:64 { …}
75+
%s%eTests%eCaster%eExceptionCasterTest.php:65 { …}
7476
%A
7577
EODUMP;
7678

@@ -90,11 +92,12 @@ public function testNoArgs()
9092
#line: 28
9193
trace: {
9294
%sExceptionCasterTest.php:28 {
95+
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
9396
› {
9497
› return new \Exception(''.$msg);
9598
› }
9699
}
97-
%s%eTests%eCaster%eExceptionCasterTest.php:82 { …}
100+
%s%eTests%eCaster%eExceptionCasterTest.php:84 { …}
98101
%A
99102
EODUMP;
100103

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public function testDumpWithCommaFlagsAndExceptionCodeExcerpt()
143143
#line: %d
144144
trace: {
145145
%ACliDumperTest.php:%d {
146+
Symfony\Component\VarDumper\Tests\Dumper\CliDumperTest->testDumpWithCommaFlagsAndExceptionCodeExcerpt()
146147
147148
› $ex = new \RuntimeException('foo');
148149
@@ -382,6 +383,7 @@ public function testThrowingCaster()
382383
#message: "Unexpected Exception thrown from a caster: Foobar"
383384
trace: {
384385
%sTwig.php:2 {
386+
__TwigTemplate_VarDumperFixture_u75a09->doDisplay(array \$context, array \$blocks = [])
385387
› foo bar
386388
› twig source
387389

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy