1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManager\Generator; |
6
|
|
|
|
7
|
|
|
use ReflectionMethod; |
8
|
|
|
use Zend\Code\Generator\MethodGenerator as ZendMethodGenerator; |
9
|
|
|
use Zend\Code\Generator\ParameterGenerator; |
10
|
|
|
use Zend\Code\Reflection\MethodReflection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Method generator that fixes minor quirks in ZF2's method generator |
14
|
|
|
* |
15
|
|
|
* @author Marco Pivetta <[email protected]> |
16
|
|
|
* @license MIT |
17
|
|
|
*/ |
18
|
|
|
class MethodGenerator extends ZendMethodGenerator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
19 |
|
public static function fromReflection(MethodReflection $reflectionMethod) : self |
24
|
|
|
{ |
25
|
|
|
/* @var $method self */ |
26
|
19 |
|
$method = self::fromReflectionWithoutDocBlock($reflectionMethod); |
27
|
|
|
|
28
|
19 |
|
$method->setInterface(false); |
29
|
|
|
|
30
|
19 |
|
return $method; |
31
|
|
|
} |
32
|
|
|
|
33
|
19 |
|
public static function fromReflectionWithoutDocBlock(MethodReflection $reflectionMethod) |
34
|
|
|
{ |
35
|
19 |
|
$method = new static(); |
36
|
19 |
|
$declaringClass = $reflectionMethod->getDeclaringClass(); |
37
|
|
|
|
38
|
19 |
|
$method->setSourceContent($reflectionMethod->getContents(false)); |
39
|
19 |
|
$method->setSourceDirty(false); |
40
|
19 |
|
$method->setReturnType(self::extractReturnTypeFromMethodReflection($reflectionMethod)); |
41
|
|
|
|
42
|
19 |
|
$method->setFinal($reflectionMethod->isFinal()); |
43
|
|
|
|
44
|
19 |
|
if ($reflectionMethod->isPrivate()) { |
45
|
|
|
$method->setVisibility(self::VISIBILITY_PRIVATE); |
46
|
19 |
|
} elseif ($reflectionMethod->isProtected()) { |
47
|
|
|
$method->setVisibility(self::VISIBILITY_PROTECTED); |
48
|
|
|
} else { |
49
|
19 |
|
$method->setVisibility(self::VISIBILITY_PUBLIC); |
50
|
|
|
} |
51
|
|
|
|
52
|
19 |
|
$method->setInterface($declaringClass->isInterface()); |
53
|
19 |
|
$method->setStatic($reflectionMethod->isStatic()); |
54
|
19 |
|
$method->setReturnsReference($reflectionMethod->returnsReference()); |
55
|
19 |
|
$method->setName($reflectionMethod->getName()); |
56
|
|
|
|
57
|
19 |
|
foreach ($reflectionMethod->getParameters() as $reflectionParameter) { |
58
|
5 |
|
$method->setParameter(ParameterGenerator::fromReflection($reflectionParameter)); |
59
|
|
|
} |
60
|
|
|
|
61
|
19 |
|
$method->setBody(static::clearBodyIndention($reflectionMethod->getBody())); |
62
|
|
|
|
63
|
19 |
|
return $method; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param MethodReflection $methodReflection |
68
|
|
|
* |
69
|
|
|
* @return null|string |
70
|
|
|
*/ |
71
|
19 |
|
private static function extractReturnTypeFromMethodReflection(MethodReflection $methodReflection) |
72
|
|
|
{ |
73
|
19 |
|
$returnType = method_exists($methodReflection, 'getReturnType') |
74
|
19 |
|
? $methodReflection->getReturnType() |
75
|
19 |
|
: null; |
76
|
|
|
|
77
|
19 |
|
if (! $returnType) { |
78
|
6 |
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
13 |
|
if (! method_exists($returnType, 'getName')) { |
82
|
|
|
return self::expandLiteralType((string) $returnType, $methodReflection); |
83
|
|
|
} |
84
|
|
|
|
85
|
13 |
|
return ($returnType->allowsNull() ? '?' : '') |
86
|
13 |
|
. self::expandLiteralType($returnType->getName(), $methodReflection); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $literalReturnType |
91
|
|
|
* @param ReflectionMethod $methodReflection |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
13 |
|
private static function expandLiteralType($literalReturnType, ReflectionMethod $methodReflection) |
96
|
|
|
{ |
97
|
13 |
|
if ('self' === strtolower($literalReturnType)) { |
98
|
1 |
|
return $methodReflection->getDeclaringClass()->getName(); |
99
|
|
|
} |
100
|
|
|
|
101
|
12 |
|
if ('parent' === strtolower($literalReturnType)) { |
102
|
1 |
|
return $methodReflection->getDeclaringClass()->getParentClass()->getName(); |
103
|
|
|
} |
104
|
|
|
|
105
|
11 |
|
return $literalReturnType; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|