Skip to content

[VarDumper] Support for ReflectionAttribute #38167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/patch-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
case false !== strpos($file, '/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php80Dummy.php'):
case false !== strpos($file, '/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures'):
case false !== strpos($file, '/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php'):
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/LotsOfAttributes.php'):
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/MyAttribute.php'):
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/NotLoadableClass.php'):
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/Php74.php') && \PHP_VERSION_ID < 70400:
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/RepeatableAttribute.php'):
continue 2;
}

Expand Down
41 changes: 39 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ public static function castType(\ReflectionType $c, array $a, Stub $stub, bool $
return $a;
}

public static function castAttribute(\ReflectionAttribute $c, array $a, Stub $stub, bool $isNested)
{
self::addMap($a, $c, [
'name' => 'getName',
'arguments' => 'getArguments',
]);

return $a;
}

public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, bool $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;
Expand Down Expand Up @@ -151,7 +161,7 @@ public static function castClass(\ReflectionClass $c, array $a, Stub $stub, bool
self::addMap($a, $c, [
'extends' => 'getParentClass',
'implements' => 'getInterfaceNames',
'constants' => 'getConstants',
'constants' => 'getReflectionConstants',
]);

foreach ($c->getProperties() as $n) {
Expand All @@ -162,6 +172,8 @@ public static function castClass(\ReflectionClass $c, array $a, Stub $stub, bool
$a[$prefix.'methods'][$n->name] = $n;
}

self::addAttributes($a, $c, $prefix);

if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
self::addExtra($a, $c);
}
Expand Down Expand Up @@ -206,6 +218,8 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
$a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
}

self::addAttributes($a, $c, $prefix);

if (!($filter & Caster::EXCLUDE_VERBOSE) && $v = $c->getStaticVariables()) {
foreach ($v as $k => &$v) {
if (\is_object($v)) {
Expand All @@ -225,6 +239,16 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
return $a;
}

public static function castClassConstant(\ReflectionClassConstant $c, array $a, Stub $stub, bool $isNested)
{
$a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
$a[Caster::PREFIX_VIRTUAL.'value'] = $c->getValue();

self::addAttributes($a, $c);

return $a;
}

public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, bool $isNested)
{
$a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
Expand All @@ -243,6 +267,8 @@ public static function castParameter(\ReflectionParameter $c, array $a, Stub $st
'allowsNull' => 'allowsNull',
]);

self::addAttributes($a, $c, $prefix);

if ($v = $c->getType()) {
$a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
}
Expand Down Expand Up @@ -271,6 +297,8 @@ public static function castParameter(\ReflectionParameter $c, array $a, Stub $st
public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, bool $isNested)
{
$a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));

self::addAttributes($a, $c);
self::addExtra($a, $c);

return $a;
Expand Down Expand Up @@ -377,7 +405,7 @@ private static function addExtra(array &$a, \Reflector $c)
}
}

private static function addMap(array &$a, \Reflector $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL)
private static function addMap(array &$a, object $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL)
{
foreach ($map as $k => $m) {
if (\PHP_VERSION_ID >= 80000 && 'isDisabled' === $k) {
Expand All @@ -389,4 +417,13 @@ private static function addMap(array &$a, \Reflector $c, array $map, string $pre
}
}
}

private static function addAttributes(array &$a, \Reflector $c, string $prefix = Caster::PREFIX_VIRTUAL): void
{
if (\PHP_VERSION_ID >= 80000) {
foreach ($c->getAttributes() as $n) {
$a[$prefix.'attributes'][] = $n;
}
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ abstract class AbstractCloner implements ClonerInterface
'Closure' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClosure'],
'Generator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castGenerator'],
'ReflectionType' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castType'],
'ReflectionAttribute' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castAttribute'],
'ReflectionGenerator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castReflectionGenerator'],
'ReflectionClass' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClass'],
'ReflectionClassConstant' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClassConstant'],
'ReflectionFunctionAbstract' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castFunctionAbstract'],
'ReflectionMethod' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castMethod'],
'ReflectionParameter' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castParameter'],
Expand Down
153 changes: 149 additions & 4 deletions src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
use Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes;
use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;

/**
Expand All @@ -36,9 +37,24 @@ public function testReflectionCaster()
0 => "Reflector"
%A]
constants: array:3 [
"IS_IMPLICIT_ABSTRACT" => 16
"IS_EXPLICIT_ABSTRACT" => %d
"IS_FINAL" => %d
0 => ReflectionClassConstant {
+name: "IS_IMPLICIT_ABSTRACT"
+class: "ReflectionClass"
modifiers: "public"
value: 16
}
1 => ReflectionClassConstant {
+name: "IS_EXPLICIT_ABSTRACT"
+class: "ReflectionClass"
modifiers: "public"
value: %d
}
2 => ReflectionClassConstant {
+name: "IS_FINAL"
+class: "ReflectionClass"
modifiers: "public"
value: %d
}
]
properties: array:%d [
"name" => ReflectionProperty {
Expand Down Expand Up @@ -75,7 +91,7 @@ public function testClosureCaster()
$b: & 123
}
file: "%sReflectionCasterTest.php"
line: "68 to 68"
line: "84 to 84"
}
EOTXT
, $var
Expand Down Expand Up @@ -242,6 +258,135 @@ public function testGenerator()
$this->assertDumpMatchesFormat($expectedDump, $generator);
}

/**
* @requires PHP 8
*/
public function testReflectionClassWithAttribute()
{
$var = new \ReflectionClass(LotsOfAttributes::class);

$this->assertDumpMatchesFormat(<<< 'EOTXT'
ReflectionClass {
+name: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
%A attributes: array:1 [
0 => ReflectionAttribute {
name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
arguments: []
}
]
%A
}
EOTXT
, $var);
}

/**
* @requires PHP 8
*/
public function testReflectionMethodWithAttribute()
{
$var = new \ReflectionMethod(LotsOfAttributes::class, 'someMethod');

$this->assertDumpMatchesFormat(<<< 'EOTXT'
ReflectionMethod {
+name: "someMethod"
+class: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
%A attributes: array:1 [
0 => ReflectionAttribute {
name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
arguments: array:1 [
0 => "two"
]
}
]
%A
}
EOTXT
, $var);
}

/**
* @requires PHP 8
*/
public function testReflectionPropertyWithAttribute()
{
$var = new \ReflectionProperty(LotsOfAttributes::class, 'someProperty');

$this->assertDumpMatchesFormat(<<< 'EOTXT'
ReflectionProperty {
+name: "someProperty"
+class: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
%A attributes: array:1 [
0 => ReflectionAttribute {
name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
arguments: array:2 [
0 => "one"
"extra" => "hello"
]
}
]
}
EOTXT
, $var);
}

/**
* @requires PHP 8
*/
public function testReflectionClassConstantWithAttribute()
{
$var = new \ReflectionClassConstant(LotsOfAttributes::class, 'SOME_CONSTANT');

$this->assertDumpMatchesFormat(<<< 'EOTXT'
ReflectionClassConstant {
+name: "SOME_CONSTANT"
+class: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
modifiers: "public"
value: "some value"
attributes: array:2 [
0 => ReflectionAttribute {
name: "Symfony\Component\VarDumper\Tests\Fixtures\RepeatableAttribute"
arguments: array:1 [
0 => "one"
]
}
1 => ReflectionAttribute {
name: "Symfony\Component\VarDumper\Tests\Fixtures\RepeatableAttribute"
arguments: array:1 [
0 => "two"
]
}
]
}
EOTXT
, $var);
}

/**
* @requires PHP 8
*/
public function testReflectionParameterWithAttribute()
{
$var = new \ReflectionParameter([LotsOfAttributes::class, 'someMethod'], 'someParameter');

$this->assertDumpMatchesFormat(<<< 'EOTXT'
ReflectionParameter {
+name: "someParameter"
position: 0
attributes: array:1 [
0 => ReflectionAttribute {
name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
arguments: array:1 [
0 => "three"
]
}
]
%A
}
EOTXT
, $var);
}

public static function stub(): void
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Tests\Fixtures;

#[MyAttribute]
final class LotsOfAttributes
{
#[RepeatableAttribute('one'), RepeatableAttribute('two')]
public const SOME_CONSTANT = 'some value';

#[MyAttribute('one', extra: 'hello')]
private string $someProperty;

#[MyAttribute('two')]
public function someMethod(
#[MyAttribute('three')] string $someParameter
): void {
}
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Fixtures/MyAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Tests\Fixtures;

use Attribute;

#[Attribute]
final class MyAttribute
{
public function __construct(
private string $foo = 'default',
private ?string $extra = null,
) {
}

public function getFoo(): string
{
return $this->foo;
}

public function getExtra(): ?string
{
return $this->extra;
}
}
Loading