Completed
Pull Request — master (#393)
by
unknown
03:10
created

MethodGenerator::fromReflection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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
    public static function fromReflectionWithoutBodyAndDocBlock(MethodReflection $reflectionMethod) : self
26
    {
27
        $method = new static();
28
29
        $method->setSourceContent($reflectionMethod->getContents(false));
30
        $method->setSourceDirty(false);
31
        $method->setReturnType(self::extractReturnTypeFromMethodReflection($reflectionMethod));
32
33
        $method->setFinal($reflectionMethod->isFinal());
34
35
        if ($reflectionMethod->isPrivate()) {
36
            $method->setVisibility(self::VISIBILITY_PRIVATE);
37
        } elseif ($reflectionMethod->isProtected()) {
38
            $method->setVisibility(self::VISIBILITY_PROTECTED);
39
        } else {
40
            $method->setVisibility(self::VISIBILITY_PUBLIC);
41
        }
42
43
        $method->setInterface(false);
44
        $method->setStatic($reflectionMethod->isStatic());
45
        $method->setReturnsReference($reflectionMethod->returnsReference());
46
        $method->setName($reflectionMethod->getName());
47
48
        foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
49
            $method->setParameter(ParameterGenerator::fromReflection($reflectionParameter));
50
        }
51
52
        return $method;
53
    }
54
55
    /**
56
     * @see \Zend\Code\Generator\MethodGenerator::extractReturnTypeFromMethodReflection
57
     */
58
    private static function extractReturnTypeFromMethodReflection(MethodReflection $methodReflection)
59
    {
60
        $returnType = method_exists($methodReflection, 'getReturnType')
61
            ? $methodReflection->getReturnType()
62
            : null;
63
64
        if (! $returnType) {
65
            return null;
66
        }
67
68
        if (! method_exists($returnType, 'getName')) {
69
            return self::expandLiteralType((string) $returnType, $methodReflection);
70
        }
71
72
        return ($returnType->allowsNull() ? '?' : '')
73
            . self::expandLiteralType($returnType->getName(), $methodReflection);
74
    }
75
76
    /**
77
     * @see \Zend\Code\Generator\MethodGenerator::expandLiteralType
78
     */
79
    private static function expandLiteralType($literalReturnType, ReflectionMethod $methodReflection)
80
    {
81
        if ('self' === strtolower($literalReturnType)) {
82
            return $methodReflection->getDeclaringClass()->getName();
83
        }
84
85
        if ('parent' === strtolower($literalReturnType)) {
86
            return $methodReflection->getDeclaringClass()->getParentClass()->getName();
87
        }
88
89
        return $literalReturnType;
90
    }
91
}
92