10000 [VarDumper] Fix `Cannot instantiate abstract class on (...)` by event15 · Pull Request #43652 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarDumper] Fix Cannot instantiate abstract class on (...) #43652

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[VarDumper] Fix Cannot instantiate abstract class on (...) in `Exce…
…ptionCaster`
  • Loading branch information
event15 committed Oct 22, 2021
commit d3f4a0f74a666fc11748cbc5a8da810913ced990
28 changes: 17 additions & 11 deletions src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,24 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is

if (file_exists($f['file']) && 0 <= self::$srcContext) {
if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
$template = $f['object'] ?? unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));

$ellipsis = 0;
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
$templateInfo = $template->getDebugInfo();
if (isset($templateInfo[$f['line']])) {
$template = null;
if (isset($f['object'])) {
$template = $f['object'];
} elseif ((new \ReflectionClass($f['class']))->isInstantiable()) {
$template = unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class']));
}
if (isset($template)) {
$ellipsis = 0;
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
$templateInfo = $template->getDebugInfo();
if (isset($templateInfo[$f['line']])) {
if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) {
$templatePath = null;
}
if ($templateSrc) {
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f);
$srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
$templatePath = null;
}
if ($templateSrc) {
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f);
$srcKey = ($templatePath ?: $template->getTemplateName()) . ':' . $templateInfo[$f['line']];
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Caster\ExceptionCaster;
use Symfony\Component\VarDumper\Caster\FrameStub;
use Symfony\Component\VarDumper\Caster\TraceStub;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

Expand Down Expand Up @@ -44,15 +46,15 @@ public function testDefaultSettings()
#message: "foo"
#code: 0
#file: "%sExceptionCasterTest.php"
#line: 28
#line: %d
trace: {
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
%s%eTests%eCaster%eExceptionCasterTest.php:%d {
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
› {
› return new \Exception(''.$msg);
› }
}
%s%eTests%eCaster%eExceptionCasterTest.php:40 { …}
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
%A
EODUMP;

Expand All @@ -66,13 +68,13 @@ public function testSeek()

$expectedDump = <<<'EODUMP'
{
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
%s%eTests%eCaster%eExceptionCasterTest.php:%d {
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
› {
› return new \Exception(''.$msg);
› }
}
%s%eTests%eCaster%eExceptionCasterTest.php:65 { …}
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
%A
EODUMP;

Expand All @@ -89,15 +91,15 @@ public function testNoArgs()
#message: "1"
#code: 0
#file: "%sExceptionCasterTest.php"
#line: 28
#line: %d
trace: {
%sExceptionCasterTest.php:28 {
%sExceptionCasterTest.php:%d {
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
› {
› return new \Exception(''.$msg);
› }
}
%s%eTests%eCaster%eExceptionCasterTest.php:84 { …}
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
%A
EODUMP;

Expand All @@ -114,16 +116,43 @@ public function testNoSrcContext()
#message: "1"
#code: 0
#file: "%sExceptionCasterTest.php"
#line: 28
#line: %d
trace: {
%s%eTests%eCaster%eExceptionCasterTest.php:28
%s%eTests%eCaster%eExceptionCasterTest.php:%d
%s%eTests%eCaster%eExceptionCasterTest.php:%d
%A
EODUMP;

$this->assertDumpMatchesFormat($expectedDump, $e);
}

public function testShouldReturnTraceForConcreteTwigWithError()
{
require_once \dirname(__DIR__).'/Fixtures/ConcreteBug.php';

$cloner = new VarCloner();
$innerExc = (new \__TwigTemplate_VarDumperFixture_concreteBug(null, __FILE__))->provideError();
$nestingWrapper = new \stdClass();
$nestingWrapper->trace = new TraceStub($innerExc->getTrace());

$dumper = new CliDumper();
$dump = $dumper->dump($cloner->cloneVar($nestingWrapper)->withRefHandles(false), true);
$expectedDump = <<<'EODUMP'
{
+"trace": {
%sConcreteBug.php:%d {
__TwigTemplate_VarDumperFixture_abstractBug->provideError()
› {
› return $this->createError();
› }
}
%sExceptionCasterTest.php:%d { …}
%A
EODUMP;

$this->assertStringMatchesFormat($expectedDump, $dump);
}

public function testHtmlDump()
{
if (ini_get('xdebug.file_link_format') || get_cfg_var('xdebug.file_link_format')) {
Expand All @@ -146,10 +175,10 @@ public function testHtmlDump()
#<span class=sf-dump-protected title="Protected property">code</span>: <span class=sf-dump-num>0</span>
#<span class=sf-dump-protected title="Protected property">file</span>: "<span class=sf-dump-str title="%sExceptionCasterTest.php
%d characters"><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%e</span>Tests%eCaster%eExceptionCasterTest.php</span>"
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>28</span>
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>%d</span>
<span class=sf-dump-meta>trace</span>: {<samp>
<span class=sf-dump-meta title="%sExceptionCasterTest.php
Stack level %d."><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%e</span>Tests%eCaster%eExceptionCasterTest.php</span>:<span class=sf-dump-num>28</span>
Stack level %d."><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%e</span>Tests%eCaster%eExceptionCasterTest.php</span>:<span class=sf-dump-num>%d</span>
&hellip;%d
</samp>}
</samp>}
Expand Down Expand Up @@ -221,7 +250,7 @@ public function testExcludeVerbosity()
#message: "foo"
#code: 0
#file: "%sExceptionCasterTest.php"
#line: 28
#line: %d
}
EODUMP;

Expand Down
57 changes: 57 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Fixtures/ConcreteBug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

abstract class __TwigTemplate_VarDumperFixture_abstractBug extends \Twig\Template
{
private function createError()
{
return new \RuntimeException('Manually triggered error.');
}

public function provideError()
{
return $this->createError();
}
}

/* foo_abstract.twig */
class __TwigTemplate_VarDumperFixture_concreteBug extends __TwigTemplate_VarDumperFixture_abstractBug
{
public function __construct(Twig\Environment $env = null, $path = null)
{
if (null !== $env) {
parent::__construct($env);
}
$this->parent = false;
$this->blocks = [];
$this->path = $path;
}

/**
* @inheritDoc
*/
public function getTemplateName()
{
}

/**
* @inheritDoc
*/
public function getDebugInfo()
{
}

/**
* @inheritDoc
*/
public function getSourceContext()
{
return new Twig\Source(" foo bar\n twig source\n\n", 'foo_abstract.twig', $this->path ?: __FILE__);
}

/**
* @inheritDoc
*/
protected function doDisplay(array $context, array $blocks = [])
{
}
}
0