Completed
Push — master ( 7ee214...32fd8d )
by Alberto
19s
created

MethodTemplate::doGenerate()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 32
nop 1
dl 0
loc 36
ccs 25
cts 25
cp 1
crap 7
rs 6.7272
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator\Template;
5
6
/**
7
 * Class MethodTemplate
8
 * @package Moka\Generator\Template
9
 */
10
class MethodTemplate implements TemplateInterface
11
{
12
    use VisibilityTrait;
13
14
    const TEMPLATE = '
15
        %s %s function %s(%s)%s
16
        {
17
            %s $this->__call("%s", func_get_args());
18
        }
19
    ';
20
21
    /**
22
     * @param \ReflectionMethod $method
23
     * @return string
24
     */
25 11
    public static function generate(\Reflector $method): string
26
    {
27 11
        return static::doGenerate($method);
28
    }
29
30
    /**
31
     * @param \ReflectionMethod $method
32
     * @return string
33
     */
34 11
    protected static function doGenerate(\ReflectionMethod $method): string
35
    {
36 11
        $visibility = static::getVisibility($method);
37
38 11
        $static = $method->isStatic() ? 'static' : '';
39
40 11
        $parameters = $method->getParameters();
41 11
        $parametersCode = [];
42 11
        if (is_array($parameters)) {
43 11
            foreach ($parameters as $parameter) {
44 10
                $parametersCode[] = ParameterTemplate::generate($parameter);
45
            }
46
        }
47
48 11
        $originalReturnType = $method->getReturnType();
49 11
        $returnType = $originalReturnType
50 5
            ? ReturnTypeTemplate::generate($method)
51 11
            : '';
52
53 11
        $returnStatement = null === $originalReturnType || 'void' !== (string)$originalReturnType
54 11
            ? 'return'
55 11
            : '';
56
57 11
        $methodName = $method->name;
58
59 11
        return sprintf(
60 11
            self::TEMPLATE,
61 11
            $visibility,
62 11
            $static,
63 11
            $methodName,
64 11
            implode(',', $parametersCode),
65 11
            $returnType,
66 11
            $returnStatement,
67 11
            $methodName
68
        );
69
    }
70
}
71