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

Skip to content

Commit cf4107c

Browse files
[VarDumper] Casters for Generator, ReflectionGenerator and ReflectionType
1 parent dee62e7 commit cf4107c

File tree

10 files changed

+216
-42
lines changed

10 files changed

+216
-42
lines changed

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function castThrowingCasterException(ThrowingCasterException $e, a
7171
'file' => $b[$prefix.'file'],
7272
'line' => $b[$prefix.'line'],
7373
));
74-
$a[$xPrefix.'trace'] = new TraceStub($b[$xPrefix.'trace'], 1, false, 0, -1 - count($a[$xPrefix.'trace']->value));
74+
$a[$xPrefix.'trace'] = new TraceStub($b[$xPrefix.'trace'], false, 0, -1 - count($a[$xPrefix.'trace']->value));
7575
}
7676

7777
unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
@@ -90,15 +90,15 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
9090

9191
$a = array();
9292
$j = count($frames);
93-
if (0 > $i = $trace->offset) {
93+
if (0 > $i = $trace->sliceOffset) {
9494
$i = max(0, $j + $i);
9595
}
9696
if (!isset($trace->value[$i])) {
9797
return array();
9898
}
9999
$lastCall = isset($frames[$i]['function']) ? ' ==> '.(isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
100100

101-
for ($j -= $i++; isset($frames[$i]); ++$i, --$j) {
101+
for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
102102
$call = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[$i]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '???';
103103

104104
$a[Caster::PREFIX_VIRTUAL.$j.'. '.$call.$lastCall] = new FrameStub(
@@ -108,7 +108,6 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
108108
'type' => isset($frames[$i]['type']) ? $frames[$i]['type'] : null,
109109
'function' => isset($frames[$i]['function']) ? $frames[$i]['function'] : null,
110110
) + $frames[$i - 1],
111-
$trace->srcContext,
112111
$trace->keepArgs,
113112
true
114113
);
@@ -122,12 +121,11 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
122121
'type' => null,
123122
'function' => '{main}',
124123
) + $frames[$i - 1],
125-
$trace->srcContext,
126124
$trace->keepArgs,
127125
true
128126
);
129-
if (null !== $trace->length) {
130-
$a = array_slice($a, 0, $trace->length, true);
127+
if (null !== $trace->sliceLength) {
128+
$a = array_slice($a, 0, $trace->sliceLength, true);
131129
}
132130

133131
return $a;
@@ -247,15 +245,29 @@ private static function filterExceptionArray($xClass, array $a, $xPrefix, $filte
247245

248246
private static function extractSource(array $srcArray, $line, $srcContext)
249247
{
250-
$src = '';
248+
$src = array();
251249

252250
for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
253-
$src .= (isset($srcArray[$i]) ? $srcArray[$i] : '')."\n";
251+
$src[] = (isset($srcArray[$i]) ? $srcArray[$i] : '')."\n";
254252
}
255-
if (!$srcContext) {
256-
$src = trim($src);
253+
254+
$ltrim = 0;
255+
while (' ' === $src[0][$ltrim] || "\t" === $src[0][$ltrim]) {
256+
$i = $srcContext << 1;
257+
while ($i > 0 && $src[0][$ltrim] === $src[$i][$ltrim]) {
258+
--$i;
259+
}
260+
if ($i) {
261+
break;
262+
}
263+
++$ltrim;
264+
}
265+
if ($ltrim) {
266+
foreach ($src as $i => $line) {
267+
$src[$i] = substr($line, $ltrim);
268+
}
257269
}
258270

259-
return $src;
271+
return implode('', $src);
260272
}
261273
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
*/
1919
class FrameStub extends EnumStub
2020
{
21-
public $srcContext;
21+
const DEFAULT_SRC_CONTEXT = 1;
22+
2223
public $keepArgs;
2324
public $inTraceStub;
25+
public $srcContext;
2426

25-
public function __construct(array $trace, $srcContext = 1, $keepArgs = true, $inTraceStub = false)
27+
public function __construct(array $frame, $keepArgs = true, $inTraceStub = false, $srcContext = null)
2628
{
27-
$this->value = $trace;
28-
$this->srcContext = $srcContext;
29+
$this->value = $frame;
2930
$this->keepArgs = $keepArgs;
3031
$this->inTraceStub = $inTraceStub;
32+
$this->srcContext = null !== $srcContext ? $srcContext : self::DEFAULT_SRC_CONTEXT;
3133
}
3234
}

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,59 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
7474
return $a;
7575
}
7676

77+
public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
78+
{
79+
return self::castReflectionGenerator(new \ReflectionGenerator($c), $a, $stub, $isNested);
80+
}
81+
82+
public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
83+
{
84+
$prefix = Caster::PREFIX_VIRTUAL;
85+
86+
$a += array(
87+
$prefix.'type' => $c->__toString(),
88+
$prefix.'allowsNull' => $c->allowsNull(),
89+
$prefix.'isBuiltin' => $c->isBuiltin(),
90+
);
91+
92+
return $a;
93+
}
94+
95+
public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
96+
{
97+
$prefix = Caster::PREFIX_VIRTUAL;
98+
99+
if ($c->getThis()) {
100+
$a[$prefix.'this'] = new CutStub($c->getThis());
101+
}
102+
$x = $c->getFunction();
103+
$frame = array(
104+
'class' => isset($x->class) ? $x->class : null,
105+
'type' => isset($x->class) ? ($x->isStatic() ? '::' : '->') : null,
106+
'function' => $x->name,
107+
'file' => $c->getExecutingFile(),
108+
'line' => $c->getExecutingLine(),
109+
);
110+
if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
111+
$x = new \ReflectionGenerator($c->getExecutingGenerator());
112+
array_unshift($trace, array(
113+
'function' => 'yield',
114+
'file' => $x->getExecutingFile(),
115+
'line' => $x->getExecutingLine() - 1,
116+
));
117+
$trace[] = $frame;
118+
$a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
119+
} else {
120+
$x = new FrameStub($frame, false, true);
121+
$x = ExceptionCaster::castFrameStub($x, array(), $x, true);
122+
$a[$prefix.'executing'] = new EnumStub(array(
123+
$frame['class'].$frame['type'].$frame['function'].'()' => $x[$prefix.'src'],
124+
));
125+
}
126+
127+
return $a;
128+
}
129+
77130
public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
78131
{
79132
$prefix = Caster::PREFIX_VIRTUAL;
@@ -244,7 +297,7 @@ private static function addExtra(&$a, \Reflector $c)
244297
}
245298
}
246299

247-
private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
300+
private static function addMap(&$a, $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
248301
{
249302
foreach ($map as $k => $m) {
250303
if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static function castEnum(EnumStub $c, array $a, Stub $stub, $isNested)
5454
if ($isNested) {
5555
$stub->class = '';
5656
$stub->handle = 0;
57+
$stub->value = null;
5758

5859
$a = array();
5960

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
*/
2121
class TraceStub extends Stub
2222
{
23-
public $srcContext;
2423
public $keepArgs;
25-
public $offset;
26-
public $length;
24+
public $sliceOffset;
25+
public $sliceLength;
26+
public $numberingOffset;
2727

28-
public function __construct(array $trace, $srcContext = 1, $keepArgs = true, $offset = 0, $length = null)
28+
public function __construct(array $trace, $keepArgs = true, $sliceOffset = 0, $sliceLength = null, $numberingOffset = 0)
2929
{
3030
$this->value = $trace;
31-
$this->srcContext = $srcContext;
3231
$this->keepArgs = $keepArgs;
33-
$this->offset = $offset;
34-
$this->length = $length;
32+
$this->sliceOffset = $sliceOffset;
33+
$this->sliceLength = $sliceLength;
34+
$this->numberingOffset = $numberingOffset;
3535
}
3636
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ abstract class AbstractCloner implements ClonerInterface
2828
'Symfony\Component\VarDumper\Caster\EnumStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castEnum',
2929

3030
'Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',
31+
'Generator' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castGenerator',
32+
'ReflectionType' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castType',
33+
'ReflectionGenerator' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castReflectionGenerator',
3134
'ReflectionClass' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClass',
3235
'ReflectionFunctionAbstract' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castFunctionAbstract',
3336
'ReflectionMethod' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castMethod',

src/Symfony/Component/VarDumper/Cloner/VarCloner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ protected function doClone($var)
149149
$stub->handle = $h;
150150
$a = $this->castObject($stub, 0 < $i);
151151
if ($v !== $stub->value) {
152-
if (Stub::TYPE_OBJECT !== $stub->type) {
152+
if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
153153
break;
154154
}
155155
if ($useExt) {

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

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\VarDumper\Tests\Caster;
1313

1414
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
15+
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
1516

1617
/**
1718
* @author Nicolas Grekas <[email protected]>
@@ -72,7 +73,7 @@ public function testClosureCaster()
7273
\$b: & 123
7374
}
7475
file: "%sReflectionCasterTest.php"
75-
line: "62 to 62"
76+
line: "63 to 63"
7677
}
7778
EOTXT
7879
, $var
@@ -92,11 +93,92 @@ public function testReturnType()
9293
returnType: "int"
9394
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
9495
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
95-
file: "%sReflectionCasterTest.php(87) : eval()'d code"
96+
file: "%sReflectionCasterTest.php(88) : eval()'d code"
9697
line: "1 to 1"
9798
}
9899
EOTXT
99100
, $f
100101
);
101102
}
103+
104+
/**
105+
* @requires PHP 7.0
106+
*/
107+
public function testGenerator()
108+
{
109+
$g = new GeneratorDemo();
110+
$g = $g->baz();
111+
$r = new \ReflectionGenerator($g);
112+
113+
$xDump = <<<'EODUMP'
114+
Generator {
115+
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
116+
executing: {
117+
Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz(): {
118+
/home/nicolas.grekas/Code/symfony/src/Symfony/Component/VarDumper/Tests/Fixtures/GeneratorDemo.php:14: """
119+
{\n
120+
yield from bar();\n
121+
}\n
122+
"""
123+
}
124+
}
125+
}
126+
EODUMP;
127+
128+
$this->assertDumpMatchesFormat($xDump, $g);
129+
130+
foreach ($g as $v) {
131+
break;
132+
}
133+
134+
$xDump = <<<'EODUMP'
135+
array:2 [
136+
0 => ReflectionGenerator {
137+
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
138+
trace: {
139+
3. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo() ==> yield(): {
140+
src: {
141+
%sGeneratorDemo.php:9: """
142+
{\n
143+
yield 1;\n
144+
}\n
145+
"""
146+
}
147+
}
148+
2. Symfony\Component\VarDumper\Tests\Fixtures\bar() ==> Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo(): {
149+
src: {
150+
%sGeneratorDemo.php:20: """
151+
{\n
152+
yield from GeneratorDemo::foo();\n
153+
}\n
154+
"""
155+
}
156+
}
157+
1. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz() ==> Symfony\Component\VarDumper\Tests\Fixtures\bar(): {
158+
src: {
159+
%sGeneratorDemo.php:14: """
160+
{\n
161+
yield from bar();\n
162+
}\n
163+
"""
164+
}
165+
}
166+
}
167+
}
168+
1 => Generator {
169+
executing: {
170+
Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo(): {
171+
/home/nicolas.grekas/Code/symfony/src/Symfony/Component/VarDumper/Tests/Fixtures/GeneratorDemo.php:10: """
172+
yield 1;\n
173+
}\n
174+
\n
175+
"""
176+
}
177+
}
178+
}
179+
]
180+
EODUMP;
181+
182+
$this->assertDumpMatchesFormat($xDump, array($r, $r->getExecutingGenerator()));
183+
}
102184
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,45 +232,45 @@ public function testThrowingCaster()
232232
%d. __TwigTemplate_VarDumperFixture_u75a09->doDisplay() ==> new Exception(): {
233233
src: {
234234
%sTwig.php:19: """
235-
// line 2\\n
236-
throw new \Exception('Foobar');\\n
237-
}\\n
235+
// line 2\\n
236+
throw new \Exception('Foobar');\\n
237+
}\\n
238238
"""
239239
{$twig} }
240240
}
241241
%d. Twig_Template->displayWithErrorHandling() ==> __TwigTemplate_VarDumperFixture_u75a09->doDisplay(): {
242242
src: {
243243
%sTemplate.php:%d: """
244-
try {\\n
245-
\$this->doDisplay(\$context, \$blocks);\\n
246-
} catch (Twig_Error \$e) {\\n
244+
try {\\n
245+
\$this->doDisplay(\$context, \$blocks);\\n
246+
} catch (Twig_Error \$e) {\\n
247247
"""
248248
}
249249
}
250250
%d. Twig_Template->display() ==> Twig_Template->displayWithErrorHandling(): {
251251
src: {
252252
%sTemplate.php:%d: """
253-
{\\n
254-
\$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));\\n
255-
}\\n
253+
{\\n
254+
\$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));\\n
255+
}\\n
256256
"""
257257
}
258258
}
259259
%d. Twig_Template->render() ==> Twig_Template->display(): {
260260
src: {
261261
%sTemplate.php:%d: """
262-
try {\\n
263-
\$this->display(\$context);\\n
264-
} catch (Exception \$e) {\\n
262+
try {\\n
263+
\$this->display(\$context);\\n
264+
} catch (Exception \$e) {\\n
265265
"""
266266
}
267267
}
268268
%d. %slosure%s() ==> Twig_Template->render(): {
269269
src: {
270270
%sCliDumperTest.php:{$line}: """
271-
}\\n
272-
};'),\\n
273-
));\\n
271+
}\\n
272+
};'),\\n
273+
));\\n
274274
"""
275275
}
276276
}

0 commit comments

Comments
 (0)