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

Skip to content

Commit 6ac8ec3

Browse files
committed
[VarDumper] Support for ReflectionAttribute.
1 parent 4b3015f commit 6ac8ec3

File tree

6 files changed

+330
-4
lines changed

6 files changed

+330
-4
lines changed

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNes
9999
return $a;
100100
}
101101

102+
public static function castAttribute(\ReflectionAttribute $c, array $a, Stub $tsub, $isNested)
103+
{
104+
self::addMap($a, $c, [
105+
'name' => 'getName',
106+
'arguments' => 'getArguments',
107+
]);
108+
109+
return $a;
110+
}
111+
102112
public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
103113
{
104114
$prefix = Caster::PREFIX_VIRTUAL;
@@ -147,7 +157,7 @@ public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isN
147157
self::addMap($a, $c, [
148158
'extends' => 'getParentClass',
149159
'implements' => 'getInterfaceNames',
150-
'constants' => 'getConstants',
160+
'constants' => method_exists($c, 'getReflectionConstants') ? 'getReflectionConstants' : 'getConstants',
151161
]);
152162

153163
foreach ($c->getProperties() as $n) {
@@ -158,6 +168,8 @@ public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isN
158168
$a[$prefix.'methods'][$n->name] = $n;
159169
}
160170

171+
self::addAttributes($a, $c, $prefix);
172+
161173
if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
162174
self::addExtra($a, $c);
163175
}
@@ -202,6 +214,8 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
202214
$a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
203215
}
204216

217+
self::addAttributes($a, $c, $prefix);
218+
205219
if ($v = $c->getStaticVariables()) {
206220
foreach ($v as $k => &$v) {
207221
if (\is_object($v)) {
@@ -224,6 +238,16 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
224238
return $a;
225239
}
226240

241+
public static function castClassConstant(\ReflectionClassConstant $c, array $a, Stub $stub, $isNested)
242+
{
243+
$a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
244+
$a[Caster::PREFIX_VIRTUAL.'value'] = $c->getValue();
245+
246+
self::addAttributes($a, $c);
247+
248+
return $a;
249+
}
250+
227251
public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
228252
{
229253
$a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
@@ -245,6 +269,8 @@ public static function castParameter(\ReflectionParameter $c, array $a, Stub $st
245269
'allowsNull' => 'allowsNull',
246270
]);
247271

272+
self::addAttributes($a, $c, $prefix);
273+
248274
if (method_exists($c, 'getType')) {
249275
if ($v = $c->getType()) {
250276
$a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
@@ -281,6 +307,8 @@ public static function castParameter(\ReflectionParameter $c, array $a, Stub $st
281307
public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
282308
{
283309
$a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
310+
311+
self::addAttributes($a, $c);
284312
self::addExtra($a, $c);
285313

286314
return $a;
@@ -330,7 +358,10 @@ private static function addExtra(&$a, \Reflector $c)
330358
}
331359
}
332360

333-
private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
361+
/**
362+
* @param \Reflector|\ReflectionAttribute $c
363+
*/
364+
private static function addMap(array &$a, $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
334365
{
335366
foreach ($map as $k => $m) {
336367
if (\PHP_VERSION_ID >= 80000 && 'isDisabled' === $k) {
@@ -342,4 +373,16 @@ private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFI
342373
}
343374
}
344375
}
376+
377+
/**
378+
* @param string $prefix
379+
*/
380+
private static function addAttributes(array &$a, \Reflector $c, $prefix = Caster::PREFIX_VIRTUAL)
381+
{
382+
if (\PHP_VERSION_ID >= 80000) {
383+
foreach ($c->getAttributes() as $n) {
384+
$a[$prefix.'attributes'][] = $n;
385+
}
386+
}
387+
}
345388
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ abstract class AbstractCloner implements ClonerInterface
3232
'Closure' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClosure'],
3333
'Generator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castGenerator'],
3434
'ReflectionType' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castType'],
35+
'ReflectionAttribute' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castAttribute'],
3536
'ReflectionGenerator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castReflectionGenerator'],
3637
'ReflectionClass' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClass'],
38+
'ReflectionClassConstant' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClassConstant'],
3739
'ReflectionFunctionAbstract' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castFunctionAbstract'],
3840
'ReflectionMethod' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castMethod'],
3941
'ReflectionParameter' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castParameter'],

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

Lines changed: 191 additions & 2 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\Test\VarDumperTestTrait;
1717
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
18+
use Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes;
1819
use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;
1920

2021
/**
@@ -24,7 +25,10 @@ class ReflectionCasterTest extends TestCase
2425
{
2526
use VarDumperTestTrait;
2627

27-
public function testReflectionCaster()
28+
/**
29+
* @requires PHP < 7.1
30+
*/
31+
public function testReflectionCasterPhp5()
2832
{
2933
$var = new \ReflectionClass('ReflectionClass');
3034

@@ -62,6 +66,62 @@ public function testReflectionCaster()
6266
);
6367
}
6468

69+
/**
70+
* @requires PHP 7.1
71+
*/
72+
public function testReflectionCaster()
73+
{
74+
$var = new \ReflectionClass('ReflectionClass');
75+
76+
$this->assertDumpMatchesFormat(
77+
<<<'EOTXT'
78+
ReflectionClass {
79+
+name: "ReflectionClass"
80+
%Aimplements: array:%d [
81+
0 => "Reflector"
82+
%A]
83+
constants: array:3 [
84+
0 => ReflectionClassConstant {
85+
+name: "IS_IMPLICIT_ABSTRACT"
86+
+class: "ReflectionClass"
87+
modifiers: "public"
88+
value: 16
89+
}
90+
1 => ReflectionClassConstant {
91+
+name: "IS_EXPLICIT_ABSTRACT"
92+
+class: "ReflectionClass"
93+
modifiers: "public"
94+
value: %d
95+
}
96+
2 => ReflectionClassConstant {
97+
+name: "IS_FINAL"
98+
+class: "ReflectionClass"
99+
modifiers: "public"
100+
value: %d
101+
}
102+
]
103+
properties: array:%d [
104+
"name" => ReflectionProperty {
105+
%A +name: "name"
106+
+class: "ReflectionClass"
107+
%A modifiers: "public"
108+
}
109+
%A]
110+
methods: array:%d [
111+
%A
112+
"__construct" => ReflectionMethod {
113+
+name: "__construct"
114+
+class: "ReflectionClass"
115+
%A parameters: {
116+
$%s: ReflectionParameter {
117+
%A position: 0
118+
%A
119+
}
120+
EOTXT
121+
, $var
122+
);
123+
}
124+
65125
public function testClosureCaster()
66126
{
67127
$a = $b = 123;
@@ -78,7 +138,7 @@ public function testClosureCaster()
78138
\$b: & 123
79139
}
80140
file: "%sReflectionCasterTest.php"
81-
line: "68 to 68"
141+
line: "128 to 128"
82142
}
83143
EOTXT
84144
, $var
@@ -263,6 +323,135 @@ public function testGenerator()
263323
EODUMP;
264324
$this->assertDumpMatchesFormat($expectedDump, $generator);
265325
}
326+
327+
/**
328+
* @requires PHP 8
329+
*/
330+
public function testReflectionClassWithAttribute()
331+
{
332+
$var = new \ReflectionClass(LotsOfAttributes::class);
333+
334+
$this->assertDumpMatchesFormat(<<< 'EOTXT'
335+
ReflectionClass {
336+
+name: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
337+
%A attributes: array:1 [
338+
0 => ReflectionAttribute {
339+
name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
340+
arguments: []
341+
}
342+
]
343+
%A
344+
}
345+
EOTXT
346+
, $var);
347+
}
348+
349+
/**
350+
* @requires PHP 8
351+
*/
352+
public function testReflectionMethodWithAttribute()
353+
{
354+
$var = new \ReflectionMethod(LotsOfAttributes::class, 'someMethod');
355+
356+
$this->assertDumpMatchesFormat(<<< 'EOTXT'
357+
ReflectionMethod {
358+
+name: "someMethod"
359+
+class: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
360+
%A attributes: array:1 [
361+
0 => ReflectionAttribute {
362+
name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
363+
arguments: array:1 [
364+
0 => "two"
365+
]
366+
}
367+
]
368+
%A
369+
}
370+
EOTXT
371+
, $var);
372+
}
373+
374+
/**
375+
* @requires PHP 8
376+
*/
377+
public function testReflectionPropertyWithAttribute()
378+
{
379+
$var = new \ReflectionProperty(LotsOfAttributes::class, 'someProperty');
380+
381+
$this->assertDumpMatchesFormat(<<< 'EOTXT'
382+
ReflectionProperty {
383+
+name: "someProperty"
384+
+class: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
385+
%A attributes: array:1 [
386+
0 => ReflectionAttribute {
387+
name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
388+
arguments: array:2 [
389+
0 => "one"
390+
"extra" => "hello"
391+
]
392+
}
393+
]
394+
}
395+
EOTXT
396+
, $var);
397+
}
398+
399+
/**
400+
* @requires PHP 8
401+
*/
402+
public function testReflectionClassConstantWithAttribute()
403+
{
404+
$var = new \ReflectionClassConstant(LotsOfAttributes::class, 'SOME_CONSTANT');
405+
406+
$this->assertDumpMatchesFormat(<<< 'EOTXT'
407+
ReflectionClassConstant {
408+
+name: "SOME_CONSTANT"
409+
+class: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
410+
modifiers: "public"
411+
value: "some value"
412+
attributes: array:2 [
413+
0 => ReflectionAttribute {
414+
name: "Symfony\Component\VarDumper\Tests\Fixtures\RepeatableAttribute"
415+
arguments: array:1 [
416+
0 => "one"
417+
]
418+
}
419+
1 => ReflectionAttribute {
420+
name: "Symfony\Component\VarDumper\Tests\Fixtures\RepeatableAttribute"
421+
arguments: array:1 [
422+
0 => "two"
423+
]
424+
}
425+
]
426+
}
427+
EOTXT
428+
, $var);
429+
}
430+
431+
/**
432+
* @requires PHP 8
433+
*/
434+
public function testReflectionParameterWithAttribute()
435+
{
436+
$var = new \ReflectionParameter([LotsOfAttributes::class, 'someMethod'], 'someParameter');
437+
438+
$this->assertDumpMatchesFormat(<<< 'EOTXT'
439+
ReflectionParameter {
440+
+name: "someParameter"
441+
position: 0
442+
attributes: array:1 [
443+
0 => ReflectionAttribute {
444+
name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
445+
arguments: array:1 [
446+
0 => "three"
447+
]
448+
}
449+
]
450+
%A
451+
}
452+
EOTXT
453+
, $var);
454+
}
266455
}
267456

268457
function reflectionParameterFixture(NotLoadableClass $arg1 = null, $arg2)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
13+
14+
#[MyAttribute]
15+
final class LotsOfAttributes
16+
{
17+
#[RepeatableAttribute('one'), RepeatableAttribute('two')]
18+
public const SOME_CONSTANT = 'some value';
19+
20+
#[MyAttribute('one', extra: 'hello')]
21+
private string $someProperty;
22+
23+
#[MyAttribute('two')]
24+
public function someMethod(
25+
#[MyAttribute('three')] string $someParameter
26+
): void {
27+
}
28+
}

0 commit comments

Comments
 (0)