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
|
|
|
* Similar to fromReflection() but without copying the method body and phpdoc. |
22
|
|
|
* |
23
|
|
|
* @see \Zend\Code\Generator\MethodGenerator::fromReflection |
24
|
|
|
*/ |
25
|
19 |
|
public static function fromReflectionWithoutBodyAndDocBlock(MethodReflection $reflectionMethod) : self |
26
|
|
|
{ |
27
|
19 |
|
$method = new static(); |
28
|
|
|
|
29
|
19 |
|
$method->setSourceContent($reflectionMethod->getContents(false)); |
30
|
19 |
|
$method->setSourceDirty(false); |
31
|
19 |
|
$method->setReturnType(self::extractReturnTypeFromMethodReflection($reflectionMethod)); |
32
|
|
|
|
33
|
19 |
|
$method->setFinal($reflectionMethod->isFinal()); |
34
|
|
|
|
35
|
19 |
|
if ($reflectionMethod->isPrivate()) { |
36
|
1 |
|
$method->setVisibility(self::VISIBILITY_PRIVATE); |
37
|
19 |
|
} elseif ($reflectionMethod->isProtected()) { |
38
|
1 |
|
$method->setVisibility(self::VISIBILITY_PROTECTED); |
39
|
|
|
} else { |
40
|
19 |
|
$method->setVisibility(self::VISIBILITY_PUBLIC); |
41
|
|
|
} |
42
|
|
|
|
43
|
19 |
|
$method->setInterface(false); |
44
|
19 |
|
$method->setStatic($reflectionMethod->isStatic()); |
45
|
19 |
|
$method->setReturnsReference($reflectionMethod->returnsReference()); |
46
|
19 |
|
$method->setName($reflectionMethod->getName()); |
47
|
|
|
|
48
|
19 |
|
foreach ($reflectionMethod->getParameters() as $reflectionParameter) { |
49
|
5 |
|
$method->setParameter(ParameterGenerator::fromReflection($reflectionParameter)); |
50
|
|
|
} |
51
|
|
|
|
52
|
19 |
|
return $method; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @see \Zend\Code\Generator\MethodGenerator::extractReturnTypeFromMethodReflection |
57
|
|
|
*/ |
58
|
19 |
|
private static function extractReturnTypeFromMethodReflection(MethodReflection $methodReflection) |
59
|
|
|
{ |
60
|
19 |
|
$returnType = method_exists($methodReflection, 'getReturnType') |
61
|
19 |
|
? $methodReflection->getReturnType() |
62
|
19 |
|
: null; |
63
|
|
|
|
64
|
19 |
|
if (! $returnType) { |
65
|
7 |
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
13 |
|
if (! method_exists($returnType, 'getName')) { |
69
|
|
|
return self::expandLiteralType((string) $returnType, $methodReflection); |
70
|
|
|
} |
71
|
|
|
|
72
|
13 |
|
return ($returnType->allowsNull() ? '?' : '') |
73
|
13 |
|
. self::expandLiteralType($returnType->getName(), $methodReflection); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @see \Zend\Code\Generator\MethodGenerator::expandLiteralType |
78
|
|
|
*/ |
79
|
13 |
|
private static function expandLiteralType($literalReturnType, ReflectionMethod $methodReflection) |
80
|
|
|
{ |
81
|
13 |
|
if ('self' === strtolower($literalReturnType)) { |
82
|
1 |
|
return $methodReflection->getDeclaringClass()->getName(); |
83
|
|
|
} |
84
|
|
|
|
85
|
12 |
|
if ('parent' === strtolower($literalReturnType)) { |
86
|
1 |
|
return $methodReflection->getDeclaringClass()->getParentClass()->getName(); |
87
|
|
|
} |
88
|
|
|
|
89
|
11 |
|
return $literalReturnType; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|