Skip to content

[VarDumper] display the method we're in when dumping stack traces #34184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
$f['file'] = substr($f['file'], 0, -\strlen($match[0]));
$f['line'] = (int) $match[1];
}
$caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null;
$src = $f['line'];
$srcKey = $f['file'];
$ellipsis = new LinkStub($srcKey, 0);
Expand All @@ -226,13 +225,13 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
$templatePath = null;
}
if ($templateSrc) {
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, $caller, 'twig', $templatePath);
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f);
$srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
}
}
}
if ($srcKey == $f['file']) {
$src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, $caller, 'php', $f['file']);
$src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, 'php', $f['file'], $f);
$srcKey .= ':'.$f['line'];
if ($ellipsis) {
$ellipsis += 1 + \strlen($f['line']);
Expand Down Expand Up @@ -308,7 +307,7 @@ private static function traceUnshift(array &$trace, ?string $class, string $file
]);
}

private static function extractSource(string $srcLines, int $line, int $srcContext, ?string $title, string $lang, string $file = null): EnumStub
private static function extractSource(string $srcLines, int $line, int $srcContext, string $lang, ?string $file, array $frame): EnumStub
{
$srcLines = explode("\n", $srcLines);
$src = [];
Expand All @@ -317,7 +316,32 @@ private static function extractSource(string $srcLines, int $line, int $srcConte
$src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n";
}

$srcLines = [];
if ($frame['function'] ?? false) {
$stub = new CutStub(new \stdClass());
$stub->class = (isset($frame['class']) ? $frame['class'].$frame['type'] : '').$frame['function'];
$stub->type = Stub::TYPE_OBJECT;
$stub->attr['cut_hash'] = true;
$stub->attr['file'] = $frame['file'];
$stub->attr['line'] = $frame['line'];

try {
$caller = isset($frame['class']) ? new \ReflectionMethod($frame['class'], $frame['function']) : new \ReflectionFunction($frame['function']);
$stub->class .= ReflectionCaster::getSignature(ReflectionCaster::castFunctionAbstract($caller, [], $stub, true, Caster::EXCLUDE_VERBOSE));

if ($f = $caller->getFileName()) {
$stub->attr['file'] = $f;
$stub->attr['line'] = $caller->getStartLine();
}
} catch (\ReflectionException $e) {
// ignore fake class/function
}

$srcLines = ["\0~separator=\0" => $stub];
} else {
$stub = null;
$srcLines = [];
}

$ltrim = 0;
do {
$pad = null;
Expand All @@ -344,7 +368,7 @@ private static function extractSource(string $srcLines, int $line, int $srcConte
if ($i !== $srcContext) {
$c = new ConstStub('default', $c);
} else {
$c = new ConstStub($c, $title);
$c = new ConstStub($c, $stub ? 'in '.$stub->class : '');
if (null !== $file) {
$c->attr['file'] = $file;
$c->attr['line'] = $line;
Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)

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

if ($cursor->softRefCount || 0 < $cursor->softRefHandle) {
if (($cursor->softRefCount || 0 < $cursor->softRefHandle) && empty($attr['cut_hash'])) {
$prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), ['count' => $cursor->softRefCount]);
} elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) {
$prefix .= $this->style('ref', '&'.$cursor->hardRefTo, ['count' => $cursor->hardRefCount]);
Expand All @@ -310,8 +310,11 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
*/
public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
{
$this->dumpEllipsis($cursor, $hasChild, $cut);
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
if (empty($cursor->attr['cut_hash'])) {
$this->dumpEllipsis($cursor, $hasChild, $cut);
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
}

$this->endValue($cursor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testDefaultSettings()
#line: 28
trace: {
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
› {
› return new \Exception(''.$msg);
› }
Expand All @@ -66,11 +67,12 @@ public function testSeek()
$expectedDump = <<<'EODUMP'
{
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
› {
› return new \Exception(''.$msg);
› }
}
%s%eTests%eCaster%eExceptionCasterTest.php:64 { …}
%s%eTests%eCaster%eExceptionCasterTest.php:65 { …}
%A
EODUMP;

Expand All @@ -90,11 +92,12 @@ public function testNoArgs()
#line: 28
trace: {
%sExceptionCasterTest.php:28 {
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
› {
› return new \Exception(''.$msg);
› }
}
%s%eTests%eCaster%eExceptionCasterTest.php:82 { …}
%s%eTests%eCaster%eExceptionCasterTest.php:84 { …}
%A
EODUMP;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function testDumpWithCommaFlagsAndExceptionCodeExcerpt()
#line: %d
trace: {
%ACliDumperTest.php:%d {
Symfony\Component\VarDumper\Tests\Dumper\CliDumperTest->testDumpWithCommaFlagsAndExceptionCodeExcerpt()
› $ex = new \RuntimeException('foo');
Expand Down Expand Up @@ -382,6 +383,7 @@ public function testThrowingCaster()
#message: "Unexpected Exception thrown from a caster: Foobar"
trace: {
%sTwig.php:2 {
__TwigTemplate_VarDumperFixture_u75a09->doDisplay(array \$context, array \$blocks = [])
› foo bar
› twig source
Expand Down