Skip to content

Commit 71bf18e

Browse files
feature #34184 [VarDumper] display the method we're in when dumping stack traces (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [VarDumper] display the method we're in when dumping stack traces | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR adds the line in blue: ![image](https://user-images.githubusercontent.com/243674/68152006-131bee00-ff43-11e9-8270-1d547732a7e5.png) Without it, we're missing some context as the method is from a trait. This allows knowing which class is actually importing and using the method. Commits ------- 23600cc8e1 [VarDumper] display the method we're in when dumping stack traces
2 parents 4f8ba2e + 33923dd commit 71bf18e

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

Caster/ExceptionCaster.php

Lines changed: 30 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,32 @@ private static function extractSource(string $srcLines, int $line, int $srcConte
317316
$src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n";
318317
}
319318

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

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

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

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)