84686efff5d6a2417bf0231ce081873bdd4882e4
[yaffs-website] / vendor / symfony / dependency-injection / LazyProxy / ProxyHelper.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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\DependencyInjection\LazyProxy;
13
14 /**
15  * @author Nicolas Grekas <p@tchwork.com>
16  *
17  * @internal
18  */
19 class ProxyHelper
20 {
21     /**
22      * @return string|null The FQCN or builtin name of the type hint, or null when the type hint references an invalid self|parent context
23      */
24     public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionParameter $p = null, $noBuiltin = false)
25     {
26         if ($p instanceof \ReflectionParameter) {
27             if (method_exists($p, 'getType')) {
28                 $type = $p->getType();
29             } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $p, $type)) {
30                 $name = $type = $type[1];
31
32                 if ('callable' === $name || 'array' === $name) {
33                     return $noBuiltin ? null : $name;
34                 }
35             }
36         } else {
37             $type = method_exists($r, 'getReturnType') ? $r->getReturnType() : null;
38         }
39         if (!$type) {
40             return;
41         }
42         if (!is_string($type)) {
43             $name = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
44
45             if ($type->isBuiltin()) {
46                 return $noBuiltin ? null : $name;
47             }
48         }
49         $lcName = strtolower($name);
50         $prefix = $noBuiltin ? '' : '\\';
51
52         if ('self' !== $lcName && 'parent' !== $lcName) {
53             return $prefix.$name;
54         }
55         if (!$r instanceof \ReflectionMethod) {
56             return;
57         }
58         if ('self' === $lcName) {
59             return $prefix.$r->getDeclaringClass()->name;
60         }
61         if ($parent = $r->getDeclaringClass()->getParentClass()) {
62             return $prefix.$parent->name;
63         }
64     }
65
66     private static function export($value)
67     {
68         if (!is_array($value)) {
69             return var_export($value, true);
70         }
71         $code = array();
72         foreach ($value as $k => $v) {
73             $code[] = sprintf('%s => %s', var_export($k, true), self::export($v));
74         }
75
76         return sprintf('array(%s)', implode(', ', $code));
77     }
78 }