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

Skip to content

Commit f76f986

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

File tree

10 files changed

+213
-35
lines changed

10 files changed

+213
-35
lines changed

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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(
@@ -126,8 +126,8 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
126126
$trace->keepArgs,
127127
true
128128
);
129-
if (null !== $trace->length) {
130-
$a = array_slice($a, 0, $trace->length, true);
129+
if (null !== $trace->sliceLength) {
130+
$a = array_slice($a, 0, $trace->sliceLength, true);
131131
}
132132

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

248248
private static function extractSource(array $srcArray, $line, $srcContext)
249249
{
250-
$src = '';
250+
$src = array();
251251

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

259-
return $src;
273+
return implode('', $src);
260274
}
261275
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class FrameStub extends EnumStub
2222
public $keepArgs;
2323
public $inTraceStub;
2424

25-
public function __construct(array $trace, $srcContext = 1, $keepArgs = true, $inTraceStub = false)
25+
public function __construct(array $frame, $srcContext = TraceStub::DEFAULT_SRC_CONTEXT, $keepArgs = true, $inTraceStub = false)
2626
{
27-
$this->value = $trace;
27+
$this->value = $frame;
2828
$this->srcContext = $srcContext;
2929
$this->keepArgs = $keepArgs;
3030
$this->inTraceStub = $inTraceStub;

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, TraceStub::DEFAULT_SRC_CONTEXT, false, 0, -1, -1);
119+
} else {
120+
$x = new FrameStub($frame, TraceStub::DEFAULT_SRC_CONTEXT, 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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@
2020
*/
2121
class TraceStub extends Stub
2222
{
23+
const DEFAULT_SRC_CONTEXT = 1;
24+
2325
public $srcContext;
2426
public $keepArgs;
25-
public $offset;
26-
public $length;
27+
public $sliceOffset;
28+
public $sliceLength;
29+
public $numberingOffset;
2730

28-
public function __construct(array $trace, $srcContext = 1, $keepArgs = true, $offset = 0, $length = null)
31+
public function __construct(array $trace, $srcContext = self::DEFAULT_SRC_CONTEXT, $keepArgs = true, $sliceOffset = 0, $sliceLength = null, $numberingOffset = 0)
2932
{
3033
$this->value = $trace;
3134
$this->srcContext = $srcContext;
3235
$this->keepArgs = $keepArgs;
33-
$this->offset = $offset;
34-
$this->length = $length;
36+
$this->sliceOffset = $sliceOffset;
37+
$this->sliceLength = $sliceLength;
38+
$this->numberingOffset = $numberingOffset;
3539
}
3640
}

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
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
4+
5+
class GeneratorDemo
6+
{
7+
public static function foo()
8+
{
9+
yield 1;
10+
}
11+
12+
public function baz()
13+
{
14+
yield from bar();
15+
}
16+
}
17+
18+
function bar()
19+
{
20+
yield from GeneratorDemo::foo();
21+
}

0 commit comments

Comments
 (0)