Completed
Pull Request — master (#393)
by
unknown
06:11
created

MethodGenerator::fromReflectionWithoutDocBlock()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
ccs 20
cts 20
cp 1
rs 8.5806
cc 4
eloc 21
nc 6
nop 1
crap 4
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
    /**
34
     * @see \Zend\Code\Generator\MethodGenerator::fromReflection
35
     */
36 19
    private static function fromReflectionWithoutDocBlock(MethodReflection $reflectionMethod)
37
    {
38 19
        $method         = new static();
39 19
        $declaringClass = $reflectionMethod->getDeclaringClass();
40
41 19
        $method->setSourceContent($reflectionMethod->getContents(false));
42 19
        $method->setSourceDirty(false);
43 19
        $method->setReturnType(self::extractReturnTypeFromMethodReflection($reflectionMethod));
44
45 19
        $method->setFinal($reflectionMethod->isFinal());
46
47 19
        if ($reflectionMethod->isPrivate()) {
48 1
            $method->setVisibility(self::VISIBILITY_PRIVATE);
49 19
        } elseif ($reflectionMethod->isProtected()) {
50 1
            $method->setVisibility(self::VISIBILITY_PROTECTED);
51
        } else {
52 19
            $method->setVisibility(self::VISIBILITY_PUBLIC);
53
        }
54
55 19
        $method->setInterface($declaringClass->isInterface());
56 19
        $method->setStatic($reflectionMethod->isStatic());
57 19
        $method->setReturnsReference($reflectionMethod->returnsReference());
58 19
        $method->setName($reflectionMethod->getName());
59
60 19
        foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
61 5
            $method->setParameter(ParameterGenerator::fromReflection($reflectionParameter));
62
        }
63
64 19
        $method->setBody(static::clearBodyIndention($reflectionMethod->getBody()));
65
66 19
        return $method;
67
    }
68
69
    /**
70
     * @see \Zend\Code\Generator\MethodGenerator::extractReturnTypeFromMethodReflection
71
     */
72 19
    private static function extractReturnTypeFromMethodReflection(MethodReflection $methodReflection)
73
    {
74 19
        $returnType = method_exists($methodReflection, 'getReturnType')
75 19
            ? $methodReflection->getReturnType()
76 19
            : null;
77
78 19
        if (! $returnType) {
79 7
            return null;
80
        }
81
82 13
        if (! method_exists($returnType, 'getName')) {
83
            return self::expandLiteralType((string) $returnType, $methodReflection);
84
        }
85
86 13
        return ($returnType->allowsNull() ? '?' : '')
87 13
            . self::expandLiteralType($returnType->getName(), $methodReflection);
88
    }
89
90
    /**
91
     * @see \Zend\Code\Generator\MethodGenerator::expandLiteralType
92
     */
93 13
    private static function expandLiteralType($literalReturnType, ReflectionMethod $methodReflection)
94
    {
95 13
        if ('self' === strtolower($literalReturnType)) {
96 1
            return $methodReflection->getDeclaringClass()->getName();
97
        }
98
99 12
        if ('parent' === strtolower($literalReturnType)) {
100 1
            return $methodReflection->getDeclaringClass()->getParentClass()->getName();
101
        }
102
103 11
        return $literalReturnType;
104
    }
105
}
106