Skip to content

Commit 7867c56

Browse files
Merge branch '4.4' into 5.3
* 4.4: cs fix [VarDumper] Fix dumping twig templates found in exceptions
2 parents 23a50d1 + 5dbb2ab commit 7867c56

File tree

3 files changed

+75
-32
lines changed

3 files changed

+75
-32
lines changed

Caster/ExceptionCaster.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,24 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, boo
214214

215215
if (is_file($f['file']) && 0 <= self::$srcContext) {
216216
if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
217-
$template = $f['object'] ?? unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));
218-
219-
$ellipsis = 0;
220-
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
221-
$templateInfo = $template->getDebugInfo();
222-
if (isset($templateInfo[$f['line']])) {
223-
if (!method_exists($template, 'getSourceContext') || !is_file($templatePath = $template->getSourceContext()->getPath())) {
224-
$templatePath = null;
225-
}
226-
if ($templateSrc) {
227-
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f);
228-
$srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
217+
$template = null;
218+
if (isset($f['object'])) {
219+
$template = $f['object'];
220+
} elseif ((new \ReflectionClass($f['class']))->isInstantiable()) {
221+
$template = unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));
222+
}
223+
if (null !== $template) {
224+
$ellipsis = 0;
225+
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
226+
$templateInfo = $template->getDebugInfo();
227+
if (isset($templateInfo[$f['line']])) {
228+
if (!method_exists($template, 'getSourceContext') || !is_file($templatePath = $template->getSourceContext()->getPath())) {
229+
$templatePath = null;
230+
}
231+
if ($templateSrc) {
232+
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f);
233+
$srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
234+
}
229235
}
230236
}
231237
}

Tests/Caster/ExceptionCasterTest.php

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\VarDumper\Caster\Caster;
1616
use Symfony\Component\VarDumper\Caster\ExceptionCaster;
1717
use Symfony\Component\VarDumper\Caster\FrameStub;
18+
use Symfony\Component\VarDumper\Caster\TraceStub;
1819
use Symfony\Component\VarDumper\Cloner\VarCloner;
1920
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
2021
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
@@ -44,15 +45,15 @@ public function testDefaultSettings()
4445
#message: "foo"
4546
#code: 0
4647
#file: "%sExceptionCasterTest.php"
47-
#line: 28
48+
#line: %d
4849
trace: {
49-
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
50+
%s%eTests%eCaster%eExceptionCasterTest.php:%d {
5051
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
5152
› {
5253
› return new \Exception(''.$msg);
5354
› }
5455
}
55-
%s%eTests%eCaster%eExceptionCasterTest.php:40 { …}
56+
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
5657
%A
5758
EODUMP;
5859

@@ -66,13 +67,13 @@ public function testSeek()
6667

6768
$expectedDump = <<<'EODUMP'
6869
{
69-
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
70+
%s%eTests%eCaster%eExceptionCasterTest.php:%d {
7071
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
7172
› {
7273
› return new \Exception(''.$msg);
7374
› }
7475
}
75-
%s%eTests%eCaster%eExceptionCasterTest.php:65 { …}
76+
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
7677
%A
7778
EODUMP;
7879

@@ -89,15 +90,15 @@ public function testNoArgs()
8990
#message: "1"
9091
#code: 0
9192
#file: "%sExceptionCasterTest.php"
92-
#line: 28
93+
#line: %d
9394
trace: {
94-
%sExceptionCasterTest.php:28 {
95+
%sExceptionCasterTest.php:%d {
9596
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
9697
› {
9798
› return new \Exception(''.$msg);
9899
› }
99100
}
100-
%s%eTests%eCaster%eExceptionCasterTest.php:84 { …}
101+
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
101102
%A
102103
EODUMP;
103104

@@ -114,16 +115,40 @@ public function testNoSrcContext()
114115
#message: "1"
115116
#code: 0
116117
#file: "%sExceptionCasterTest.php"
117-
#line: 28
118+
#line: %d
118119
trace: {
119-
%s%eTests%eCaster%eExceptionCasterTest.php:28
120+
%s%eTests%eCaster%eExceptionCasterTest.php:%d
120121
%s%eTests%eCaster%eExceptionCasterTest.php:%d
121122
%A
122123
EODUMP;
123124

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

128+
public function testShouldReturnTraceForConcreteTwigWithError()
129+
{
130+
require_once \dirname(__DIR__).'/Fixtures/Twig.php';
131+
132+
$innerExc = (new \__TwigTemplate_VarDumperFixture_u75a09(null, __FILE__))->provideError();
133+
$nestingWrapper = new \stdClass();
134+
$nestingWrapper->trace = new TraceStub($innerExc->getTrace());
135+
136+
$expectedDump = <<<'EODUMP'
137+
{
138+
+"trace": {
139+
%sTwig.php:%d {
140+
AbstractTwigTemplate->provideError()
141+
› {
142+
› return $this->createError();
143+
› }
144+
}
145+
%sExceptionCasterTest.php:%d { …}
146+
%A
147+
EODUMP;
148+
149+
$this->assertDumpMatchesFormat($expectedDump, $nestingWrapper);
150+
}
151+
127152
public function testHtmlDump()
128153
{
129154
if (ini_get('xdebug.file_link_format') || get_cfg_var('xdebug.file_link_format')) {
@@ -146,10 +171,10 @@ public function testHtmlDump()
146171
#<span class=sf-dump-protected title="Protected property">code</span>: <span class=sf-dump-num>0</span>
147172
#<span class=sf-dump-protected title="Protected property">file</span>: "<span class=sf-dump-str title="%sExceptionCasterTest.php
148173
%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>"
149-
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>28</span>
174+
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>%d</span>
150175
<span class=sf-dump-meta>trace</span>: {<samp data-depth=2 class=sf-dump-compact>
151176
<span class=sf-dump-meta title="%sExceptionCasterTest.php
152-
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>
177+
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>
153178
&hellip;%d
154179
</samp>}
155180
</samp>}
@@ -169,12 +194,12 @@ public function testFrameWithTwig()
169194
$f = [
170195
new FrameStub([
171196
'file' => \dirname(__DIR__).'/Fixtures/Twig.php',
172-
'line' => 20,
197+
'line' => 33,
173198
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
174199
]),
175200
new FrameStub([
176201
'file' => \dirname(__DIR__).'/Fixtures/Twig.php',
177-
'line' => 21,
202+
'line' => 34,
178203
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
179204
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(null, __FILE__),
180205
]),
@@ -186,7 +211,7 @@ public function testFrameWithTwig()
186211
class: "__TwigTemplate_VarDumperFixture_u75a09"
187212
src: {
188213
%sTwig.php:1 {
189-
214+
%s
190215
› foo bar
191216
› twig source
192217
}
@@ -201,12 +226,11 @@ class: "__TwigTemplate_VarDumperFixture_u75a09"
201226
%sExceptionCasterTest.php:2 {
202227
› foo bar
203228
› twig source
204-
229+
%s
205230
}
206231
}
207232
}
208233
]
209-
210234
EODUMP;
211235

212236
$this->assertDumpMatchesFormat($expectedDump, $f);
@@ -221,7 +245,7 @@ public function testExcludeVerbosity()
221245
#message: "foo"
222246
#code: 0
223247
#file: "%sExceptionCasterTest.php"
224-
#line: 28
248+
#line: %d
225249
}
226250
EODUMP;
227251

Tests/Fixtures/Twig.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
<?php
22

3+
abstract class AbstractTwigTemplate extends Twig\Template
4+
{
5+
private function createError()
6+
{
7+
return new \RuntimeException('Manually triggered error.');
8+
}
9+
10+
public function provideError()
11+
{
12+
return $this->createError();
13+
}
14+
}
15+
316
/* foo.twig */
4-
class __TwigTemplate_VarDumperFixture_u75a09 extends Twig\Template
17+
class __TwigTemplate_VarDumperFixture_u75a09 extends AbstractTwigTemplate
518
{
619
private $path;
720

@@ -28,7 +41,7 @@ public function getTemplateName()
2841

2942
public function getDebugInfo()
3043
{
31-
return [20 => 1, 21 => 2];
44+
return [33 => 1, 34 => 2];
3245
}
3346

3447
public function getSourceContext()

0 commit comments

Comments
 (0)