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