Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90fd3b0

Browse files
[VarDumper] Dumping exceptions is now more compact
1 parent 5213778 commit 90fd3b0

File tree

4 files changed

+99
-105
lines changed

4 files changed

+99
-105
lines changed

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

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
8888
$stub->class = '';
8989
$stub->handle = 0;
9090
$frames = $trace->value;
91+
$prefix = Caster::PREFIX_VIRTUAL;
9192

9293
$a = array();
9394
$j = count($frames);
@@ -98,33 +99,36 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
9899
return array();
99100
}
100101
$lastCall = isset($frames[$i]['function']) ? ' ==> '.(isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
102+
$frames[] = array('function' => '');
101103

102104
for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
103-
$call = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[$i]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '???';
105+
$f = $frames[$i];
106+
$call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'].'()' : '???';
104107

105-
$a[Caster::PREFIX_VIRTUAL.$j.'. '.$call.$lastCall] = new FrameStub(
108+
$label = $call.$lastCall;
109+
$frame = new FrameStub(
106110
array(
107-
'object' => isset($frames[$i]['object']) ? $frames[$i]['object'] : null,
108-
'class' => isset($frames[$i]['class']) ? $frames[$i]['class'] : null,
109-
'type' => isset($frames[$i]['type']) ? $frames[$i]['type'] : null,
110-
'function' => isset($frames[$i]['function']) ? $frames[$i]['function'] : null,
111+
'object' => isset($f['object']) ? $f['object'] : null,
112+
'class' => isset($f['class']) ? $f['class'] : null,
113+
'type' => isset($f['type']) ? $f['type'] : null,
114+
'function' => isset($f['function']) ? $f['function'] : null,
111115
) + $frames[$i - 1],
112116
$trace->keepArgs,
113117
true
114118
);
119+
if (array($prefix.'src') === array_keys($f = self::castFrameStub($frame, array(), $frame, true))) {
120+
$frame = $f[$prefix.'src'];
121+
if (1 === count($frame->value)) {
122+
foreach ($frame->value as $label => $frame) {
123+
}
124+
} else {
125+
$label = '';
126+
}
127+
}
128+
$a[$prefix.$j.'. '.$label] = $frame;
115129

116130
$lastCall = ' ==> '.$call;
117131
}
118-
$a[Caster::PREFIX_VIRTUAL.$j.'. {main}'.$lastCall] = new FrameStub(
119-
array(
120-
'object' => null,
121-
'class' => null,
122-
'type' => null,
123-
'function' => '{main}',
124-
) + $frames[$i - 1],
125-
$trace->keepArgs,
126-
true
127-
);
128132
if (null !== $trace->sliceLength) {
129133
$a = array_slice($a, 0, $trace->sliceLength, true);
130134
}
@@ -145,8 +149,13 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
145149
$f['file'] = substr($f['file'], 0, -strlen($match[0]));
146150
$f['line'] = (int) $match[1];
147151
}
152+
$relFile = $f['file'];
153+
$home = getenv('HOME');
154+
if (false !== $home && 0 === strpos($relFile, $home.DIRECTORY_SEPARATOR)) {
155+
$relFile = '~'.substr($relFile, strlen($home));
156+
}
148157
if (file_exists($f['file']) && 0 <= self::$srcContext) {
149-
$src[$f['file'].':'.$f['line']] = self::extractSource(explode("\n", file_get_contents($f['file'])), $f['line'], self::$srcContext);
158+
$src[$relFile] = self::extractSource(explode("\n", file_get_contents($f['file'])), $f['line'], self::$srcContext);
150159

151160
if (!empty($f['class']) && is_subclass_of($f['class'], 'Twig_Template') && method_exists($f['class'], 'getDebugInfo')) {
152161
$template = isset($f['object']) ? $f['object'] : new $f['class'](new \Twig_Environment(new \Twig_Loader_Filesystem()));
@@ -156,13 +165,13 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
156165
$templateSrc = explode("\n", method_exists($template, 'getSource') ? $template->getSource() : $template->getEnvironment()->getLoader()->getSource($templateName));
157166
$templateInfo = $template->getDebugInfo();
158167
if (isset($templateInfo[$f['line']])) {
159-
$src[$templateName.':'.$templateInfo[$f['line']]] = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext);
168+
$src[$templateName] = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext);
160169
}
161170
} catch (\Twig_Error_Loader $e) {
162171
}
163172
}
164173
} else {
165-
$src[$f['file']] = $f['line'];
174+
$src[$relFile] = $f['line'];
166175
}
167176
$a[$prefix.'src'] = new EnumStub($src);
168177
}
@@ -232,12 +241,23 @@ private static function extractSource(array $srcArray, $line, $srcContext)
232241
++$ltrim;
233242
} while (0 > $i && null !== $pad);
234243

235-
if (--$ltrim) {
236-
foreach ($src as $i => $line) {
237-
$src[$i] = isset($line[$ltrim]) && "\r" !== $line[$ltrim] ? substr($line, $ltrim) : ltrim($line, " \t");
244+
--$ltrim;
245+
246+
$pad = strlen($line + $srcContext);
247+
$srcArray = array();
248+
249+
foreach ($src as $i => $c) {
250+
if ($ltrim) {
251+
$c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t");
252+
}
253+
$c = substr($c, 0, -1);
254+
$c = new ConstStub($c, $c);
255+
if ($i !== $srcContext) {
256+
$c->class = 'default';
238257
}
258+
$srcArray[sprintf("% {$pad}d", $i + $line - $srcContext)] = $c;
239259
}
240260

241-
return implode('', $src);
261+
return new EnumStub($srcArray);
242262
}
243263
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public function dumpScalar(Cursor $cursor, $type, $value)
115115
$attr = array();
116116

117117
switch ($type) {
118+
case 'default':
119+
$style = 'default';
120+
break;
121+
118122
case 'integer':
119123
$style = 'num';
120124
break;

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

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ public function testGenerator()
156156
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
157157
executing: {
158158
Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz(): {
159-
%sGeneratorDemo.php:14: """
160-
{\n
161-
yield from bar();\n
162-
}\n
163-
"""
159+
%sGeneratorDemo.php: {
160+
13: {
161+
14: yield from bar();
162+
15: }
163+
}
164164
}
165165
}
166166
}
@@ -177,43 +177,31 @@ public function testGenerator()
177177
0 => ReflectionGenerator {
178178
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
179179
trace: {
180-
3. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo() ==> yield(): {
181-
src: {
182-
%sGeneratorDemo.php:9: """
183-
{\n
184-
yield 1;\n
185-
}\n
186-
"""
187-
}
180+
3. %sGeneratorDemo.php: {
181+
8: {
182+
9: yield 1;
183+
10: }
188184
}
189-
2. Symfony\Component\VarDumper\Tests\Fixtures\bar() ==> Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo(): {
190-
src: {
191-
%sGeneratorDemo.php:20: """
192-
{\n
193-
yield from GeneratorDemo::foo();\n
194-
}\n
195-
"""
196-
}
185+
2. %sGeneratorDemo.php: {
186+
19: {
187+
20: yield from GeneratorDemo::foo();
188+
21: }
197189
}
198-
1. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz() ==> Symfony\Component\VarDumper\Tests\Fixtures\bar(): {
199-
src: {
200-
%sGeneratorDemo.php:14: """
201-
{\n
202-
yield from bar();\n
203-
}\n
204-
"""
205-
}
190+
1. %sGeneratorDemo.php: {
191+
13: {
192+
14: yield from bar();
193+
15: }
206194
}
207195
}
208196
}
209197
1 => Generator {
210198
executing: {
211199
Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo(): {
212-
%sGeneratorDemo.php:10: """
213-
yield 1;\n
214-
}\n
215-
\n
216-
"""
200+
%sGeneratorDemo.php: {
201+
9: yield 1;
202+
10: }
203+
11:
204+
}
217205
}
218206
}
219207
}

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

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,21 @@ public function testThrowingCaster()
225225

226226
if (method_exists($twig, 'getSource')) {
227227
$twig = <<<EOTXT
228-
foo.twig:2: """
229-
foo bar\\n
230-
twig source\\n
231-
\\n
232-
"""
233-
228+
: {
229+
%sTwig.php: {
230+
%d: // line 2
231+
%d: throw new \\Exception('Foobar');
232+
%d: }
233+
}
234+
foo.twig: {
235+
1: foo bar
236+
2: twig source
237+
3:
238+
}
239+
}
234240
EOTXT;
235241
} else {
236-
$twig = '';
242+
$twig = '%A';
237243
}
238244

239245
$r = defined('HHVM_VERSION') ? '' : '#%d';
@@ -250,50 +256,26 @@ public function testThrowingCaster()
250256
⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {{$r}
251257
#message: "Unexpected Exception thrown from a caster: Foobar"
252258
-trace: {
253-
%d. __TwigTemplate_VarDumperFixture_u75a09->doDisplay() ==> new Exception(): {
254-
src: {
255-
%sTwig.php:19: """
256-
// line 2\\n
257-
throw new \Exception('Foobar');\\n
258-
}\\n
259-
"""
260-
{$twig} }
259+
%d. {$twig}
260+
%d. %sTemplate.php: {
261+
%d: try {
262+
%d: \$this->doDisplay(\$context, \$blocks);
263+
%d: } catch (Twig_Error \$e) {
261264
}
262-
%d. Twig_Template->displayWithErrorHandling() ==> __TwigTemplate_VarDumperFixture_u75a09->doDisplay(): {
263-
src: {
264-
%sTemplate.php:%d: """
265-
try {\\n
266-
\$this->doDisplay(\$context, \$blocks);\\n
267-
} catch (Twig_Error \$e) {\\n
268-
"""
269-
}
265+
%d. %sTemplate.php: {
266+
%d: {
267+
%d: \$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));
268+
%d: }
270269
}
271-
%d. Twig_Template->display() ==> Twig_Template->displayWithErrorHandling(): {
272-
src: {
273-
%sTemplate.php:%d: """
274-
{\\n
275-
\$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));\\n
276-
}\\n
277-
"""
278-
}
279-
}
280-
%d. Twig_Template->render() ==> Twig_Template->display(): {
281-
src: {
282-
%sTemplate.php:%d: """
283-
try {\\n
284-
\$this->display(\$context);\\n
285-
} catch (Exception \$e) {\\n
286-
"""
287-
}
270+
%d. %sTemplate.php: {
271+
%d: try {
272+
%d: \$this->display(\$context);
273+
%d: } catch (Exception \$e) {
288274
}
289-
%d. %slosure%s() ==> Twig_Template->render(): {
290-
src: {
291-
%sCliDumperTest.php:{$line}: """
292-
}\\n
293-
};'),\\n
294-
));\\n
295-
"""
296-
}
275+
%d. %sCliDumperTest.php: {
276+
%d: }
277+
{$line}: };'),
278+
%d: ));
297279
}
298280
}
299281
}

0 commit comments

Comments
 (0)