From 64d6e76e42c356a2e129c6bd5b1f0cdd18e7880e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 26 Mar 2015 20:35:25 +0100 Subject: [PATCH] [VarDumper] Add casters for Reflection* classes --- .../VarDumper/Caster/ReflectionCaster.php | 208 +++++++++++++++++- .../VarDumper/Cloner/AbstractCloner.php | 8 +- .../Tests/Caster/ReflectionCasterTest.php | 70 ++++++ .../VarDumper/Tests/CliDumperTest.php | 31 ++- .../VarDumper/Tests/HtmlDumperTest.php | 30 ++- 5 files changed, 320 insertions(+), 27 deletions(-) create mode 100644 src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php diff --git a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php index 94a25a89e484..092073940ee8 100644 --- a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php @@ -20,19 +20,221 @@ */ class ReflectionCaster { + private static $extraMap = array( + 'docComment' => 'getDocComment', + 'extension' => 'getExtensionName', + 'isDisabled' => 'isDisabled', + 'isDeprecated' => 'isDeprecated', + 'isInternal' => 'isInternal', + 'isUserDefined' => 'isUserDefined', + 'isGenerator' => 'isGenerator', + 'isVariadic' => 'isVariadic', + ); + + /** + * @deprecated since Symfony 2.7, to be removed in 3.0. + */ public static function castReflector(\Reflector $c, array $a, Stub $stub, $isNested) { - $a["\0~\0reflection"] = $c->__toString(); + trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0.', E_USER_DEPRECATED); + $a[Caster::PREFIX_VIRTUAL.'reflection'] = $c->__toString(); return $a; } public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested) { + $prefix = Caster::PREFIX_VIRTUAL; + $c = new \ReflectionFunction($c); + $stub->class = 'Closure'; // HHVM generates unique class names for closures - $a = static::castReflector(new \ReflectionFunction($c), $a, $stub, $isNested); - unset($a["\0+\0000"], $a['name'], $a["\0+\0this"], $a["\0+\0parameter"]); + $a = static::castFunctionAbstract($c, $a, $stub, $isNested); + + if (isset($a[$prefix.'parameters'])) { + foreach ($a[$prefix.'parameters'] as &$v) { + $param = $v; + $v = array(); + foreach (static::castParameter($param, array(), $stub, true) as $k => $param) { + if ("\0" === $k[0]) { + $v[substr($k, 3)] = $param; + } + } + unset($v['position'], $v['isVariadic'], $v['byReference'], $v); + } + } + + if ($f = $c->getFileName()) { + $a[$prefix.'file'] = $f; + $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine(); + } + + $prefix = Caster::PREFIX_DYNAMIC; + unset($a['name'], $a[$prefix.'0'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']); + + return $a; + } + + public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0) + { + $prefix = Caster::PREFIX_VIRTUAL; + + if ($n = \Reflection::getModifierNames($c->getModifiers())) { + $a[$prefix.'modifiers'] = implode(' ', $n); + } + + self::addMap($a, $c, array( + 'extends' => 'getParentClass', + 'implements' => 'getInterfaceNames', + 'constants' => 'getConstants', + )); + + foreach ($c->getProperties() as $n) { + $a[$prefix.'properties'][$n->name] = $n; + } + + foreach ($c->getMethods() as $n) { + $a[$prefix.'methods'][$n->name] = $n; + } + + if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) { + self::addExtra($a, $c); + } return $a; } + + public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0) + { + $prefix = Caster::PREFIX_VIRTUAL; + + self::addMap($a, $c, array( + 'returnsReference' => 'returnsReference', + 'class' => 'getClosureScopeClass', + 'this' => 'getClosureThis', + )); + + if (isset($a[$prefix.'this'])) { + $a[$prefix.'this'] = new CutStub($a[$prefix.'this']); + } + + foreach ($c->getParameters() as $v) { + $k = '$'.$v->name; + if ($v->isPassedByReference()) { + $k = '&'.$k; + } + if (method_exists($v, 'isVariadic') && $v->isVariadic()) { + $k = '...'.$k; + } + $a[$prefix.'parameters'][$k] = $v; + } + + if ($v = $c->getStaticVariables()) { + foreach ($v as $k => &$v) { + $a[$prefix.'use']['$'.$k] =& $v; + } + unset($v); + } + + if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) { + self::addExtra($a, $c); + } + + return $a; + } + + public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested) + { + $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers())); + + return $a; + } + + public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested) + { + $prefix = Caster::PREFIX_VIRTUAL; + + self::addMap($a, $c, array( + 'position' => 'getPosition', + 'isVariadic' => 'isVariadic', + 'byReference' => 'isPassedByReference', + )); + + try { + if ($c->isArray()) { + $a[$prefix.'typeHint'] = 'array'; + } elseif (method_exists($c, 'isCallable') && $c->isCallable()) { + $a[$prefix.'typeHint'] = 'callable'; + } elseif ($v = $c->getClass()) { + $a[$prefix.'typeHint'] = $v->name; + } + } catch (\ReflectionException $e) { + } + + try { + $a[$prefix.'default'] = $v = $c->getDefaultValue(); + if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) { + $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v); + } + } catch (\ReflectionException $e) { + } + + return $a; + } + + public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested) + { + $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers())); + self::addExtra($a, $c); + + return $a; + } + + public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested) + { + self::addMap($a, $c, array( + 'version' => 'getVersion', + 'dependencies' => 'getDependencies', + 'iniEntries' => 'getIniEntries', + 'isPersistent' => 'isPersistent', + 'isTemporary' => 'isTemporary', + 'constants' => 'getConstants', + 'functions' => 'getFunctions', + 'classes' => 'getClasses', + )); + + return $a; + } + + public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested) + { + self::addMap($a, $c, array( + 'version' => 'getVersion', + 'author' => 'getAuthor', + 'copyright' => 'getCopyright', + 'url' => 'getURL', + )); + + return $a; + } + + private static function addExtra(&$a, \Reflector $c) + { + $a =& $a[Caster::PREFIX_VIRTUAL.'extra']; + + if (method_exists($c, 'getFileName') && $m = $c->getFileName()) { + $a['file'] = $m; + $a['line'] = $c->getStartLine().' to '.$c->getEndLine(); + } + + self::addMap($a, $c, self::$extraMap, ''); + } + + private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL) + { + foreach ($map as $k => $m) { + if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) { + $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m; + } + } + } } diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 7c641770fe0a..e4f7a7c0538b 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -26,7 +26,13 @@ abstract class AbstractCloner implements ClonerInterface 'Symfony\Component\VarDumper\Caster\ConstStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub', 'Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure', - 'Reflector' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castReflector', + 'ReflectionClass' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClass', + 'ReflectionFunctionAbstract' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castFunctionAbstract', + 'ReflectionMethod' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castMethod', + 'ReflectionParameter' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castParameter', + 'ReflectionProperty' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castProperty', + 'ReflectionExtension' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castExtension', + 'ReflectionZendExtension' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castZendExtension', 'Doctrine\Common\Persistence\ObjectManager' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals', 'Doctrine\Common\Proxy\Proxy' => 'Symfony\Component\VarDumper\Caster\DoctrineCaster::castCommonProxy', diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php new file mode 100644 index 000000000000..be4947278d5a --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php @@ -0,0 +1,70 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Tests\Caster; + +use Symfony\Component\VarDumper\Cloner\VarCloner; +use Symfony\Component\VarDumper\Dumper\CliDumper; +use Symfony\Component\VarDumper\Test\VarDumperTestCase; + +/** + * @author Nicolas Grekas + */ +class ReflectionCasterTest extends VarDumperTestCase +{ + public function testReflectionCaster() + { + $var = new \ReflectionClass('ReflectionClass'); + + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionClass { + +name: "ReflectionClass" + implements: array:1 [ + 0 => "Reflector" + ] + constants: array:3 [ + "IS_IMPLICIT_ABSTRACT" => 16 + "IS_EXPLICIT_ABSTRACT" => 32 + "IS_FINAL" => 64 + ] + properties: array:1 [ + "name" => ReflectionProperty { + +name: "name" + +class: "ReflectionClass" + modifiers: "public" + extra: null + } + ] + methods: array:%d [ +%A + "export" => ReflectionMethod { + +name: "export" + +class: "ReflectionClass" + parameters: array:2 [ + "$argument" => ReflectionParameter { + +name: "argument" + position: 0 + } + "$return" => ReflectionParameter { + +name: "return" + position: 1 + } + ] + modifiers: "public static" + } +%A +} +EOTXT + , $var + ); + } +} diff --git a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php index 66c6eb72fb37..b72247e1c056 100644 --- a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php @@ -39,11 +39,19 @@ public function testGet() ob_start(); $dumper->dump($data); $out = ob_get_clean(); - $closureLabel = PHP_VERSION_ID >= 50400 ? 'public method' : 'function'; $out = preg_replace('/[ \t]+$/m', '', $out); $intMax = PHP_INT_MAX; $res1 = (int) $var['res']; $res2 = (int) $var[8]; + $closure54 = ''; + + if (PHP_VERSION_ID >= 50400) { + $closure54 = <<assertStringMatchesFormat( << Closure {#%d - reflection: """ - Closure [ {$closureLabel} Symfony\Component\VarDumper\Tests\Fixture\{closure} ] { - @@ {$var['file']} {$var['line']} - {$var['line']} - - - Parameters [2] { - Parameter #0 [ \$a ] - Parameter #1 [ PDO or NULL &\$b = NULL ] - } - } - """ + "closure" => Closure {#%d{$closure54} + parameters: array:2 [ + "\$a" => [] + "&\$b" => array:2 [ + "typeHint" => "PDO" + "default" => null + ] + ] + file: "{$var['file']}" + line: "{$var['line']} to {$var['line']}" } "line" => {$var['line']} "nobj" => array:1 [ diff --git a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php index 22083b516154..2148cbf07ffa 100644 --- a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php @@ -47,6 +47,15 @@ public function testGet() $dumpId = $dumpId[0]; $res1 = (int) $var['res']; $res2 = (int) $var[8]; + $closure54 = ''; + + if (PHP_VERSION_ID >= 50400) { + $closure54 = <<class: "Symfony\Component\VarDumper\Tests\HtmlDumperTest" + this: HtmlDumperTest {#%d …} +EOTXT; + } $this->assertStringMatchesFormat( <<foo: "foo" +"bar": "bar" } - "closure" => Closure {#%d - reflection: """ - Closure [ <user> {$closureLabel} Symfony\Component\VarDumper\Tests\Fixture\{closure} ] { - @@ {$var['file']} {$var['line']} - {$var['line']} - - - Parameters [2] { - Parameter #0 [ <required> \$a ] - Parameter #1 [ <optional> PDO or NULL &\$b = NULL ] - } - } - """ + "closure" => Closure {#%d{$closure54} + parameters: array:2 [ + "\$a" => [] + "&\$b" => array:2 [ + "typeHint" => "PDO" + "default" => null + ] + ] + file: "{$var['file']}" + line: "{$var['line']} to {$var['line']}" } "line" => {$var['line']} "nobj" => array:1 [