Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 /*
|
Chris@14
|
4 * This file is part of the Symfony package.
|
Chris@14
|
5 *
|
Chris@14
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
7 *
|
Chris@14
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
9 * file that was distributed with this source code.
|
Chris@14
|
10 */
|
Chris@14
|
11
|
Chris@14
|
12 namespace Symfony\Component\DependencyInjection\LazyProxy;
|
Chris@14
|
13
|
Chris@14
|
14 /**
|
Chris@14
|
15 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@14
|
16 *
|
Chris@14
|
17 * @internal
|
Chris@14
|
18 */
|
Chris@14
|
19 class ProxyHelper
|
Chris@14
|
20 {
|
Chris@14
|
21 /**
|
Chris@14
|
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
|
Chris@14
|
23 */
|
Chris@14
|
24 public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionParameter $p = null, $noBuiltin = false)
|
Chris@14
|
25 {
|
Chris@14
|
26 if ($p instanceof \ReflectionParameter) {
|
Chris@14
|
27 if (method_exists($p, 'getType')) {
|
Chris@14
|
28 $type = $p->getType();
|
Chris@14
|
29 } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $p, $type)) {
|
Chris@14
|
30 $name = $type = $type[1];
|
Chris@14
|
31
|
Chris@14
|
32 if ('callable' === $name || 'array' === $name) {
|
Chris@14
|
33 return $noBuiltin ? null : $name;
|
Chris@14
|
34 }
|
Chris@14
|
35 }
|
Chris@14
|
36 } else {
|
Chris@14
|
37 $type = method_exists($r, 'getReturnType') ? $r->getReturnType() : null;
|
Chris@14
|
38 }
|
Chris@14
|
39 if (!$type) {
|
Chris@14
|
40 return;
|
Chris@14
|
41 }
|
Chris@17
|
42 if (!\is_string($type)) {
|
Chris@14
|
43 $name = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
|
Chris@14
|
44
|
Chris@14
|
45 if ($type->isBuiltin()) {
|
Chris@14
|
46 return $noBuiltin ? null : $name;
|
Chris@14
|
47 }
|
Chris@14
|
48 }
|
Chris@14
|
49 $lcName = strtolower($name);
|
Chris@14
|
50 $prefix = $noBuiltin ? '' : '\\';
|
Chris@14
|
51
|
Chris@14
|
52 if ('self' !== $lcName && 'parent' !== $lcName) {
|
Chris@14
|
53 return $prefix.$name;
|
Chris@14
|
54 }
|
Chris@14
|
55 if (!$r instanceof \ReflectionMethod) {
|
Chris@14
|
56 return;
|
Chris@14
|
57 }
|
Chris@14
|
58 if ('self' === $lcName) {
|
Chris@14
|
59 return $prefix.$r->getDeclaringClass()->name;
|
Chris@14
|
60 }
|
Chris@14
|
61 if ($parent = $r->getDeclaringClass()->getParentClass()) {
|
Chris@14
|
62 return $prefix.$parent->name;
|
Chris@14
|
63 }
|
Chris@14
|
64 }
|
Chris@14
|
65 }
|