MethodTemplate   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 4 1
B doGenerate() 0 38 8
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator\Template;
5
6
use Moka\Exception\InvalidArgumentException;
7
8
/**
9
 * Class MethodTemplate
10
 * @package Moka\Generator\Template
11
 */
12
class MethodTemplate implements TemplateInterface
13
{
14
    use VisibilityTrait;
15
16
    private const TEMPLATE = '
17
        %s %s function %s %s(%s)%s
18
        {
19
            %s $this->__call("%s", func_get_args());
20
        }
21
    ';
22
23
    /**
24
     * @param \Reflector|\ReflectionMethod $method
25
     * @return string
26
     * @throws \RuntimeException
27
     * @throws InvalidArgumentException
28
     */
29 11
    public static function generate(\Reflector $method): string
30
    {
31 11
        return static::doGenerate($method);
32
    }
33
34
    /**
35
     * @param \ReflectionMethod $method
36
     * @return string
37
     * @throws \RuntimeException
38
     * @throws InvalidArgumentException
39
     */
40 11
    protected static function doGenerate(\ReflectionMethod $method): string
41
    {
42 11
        $visibility = static::getVisibility($method);
43
44 11
        $static = $method->isStatic() ? 'static' : '';
45 11
        $reference = $method->returnsReference() ? '&' : '';
46
47 11
        $parameters = $method->getParameters();
48 11
        $parametersCode = [];
49 11
        if (\is_array($parameters)) {
50 11
            foreach ($parameters as $parameter) {
51 10
                $parametersCode[] = ParameterTemplate::generate($parameter);
52
            }
53
        }
54
55 11
        $originalReturnType = $method->getReturnType();
56 11
        $returnType = $originalReturnType
57 5
            ? ReturnTypeTemplate::generate($method)
58 11
            : '';
59
60 11
        $returnStatement = null === $originalReturnType || 'void' !== $originalReturnType->getName()
61 11
            ? 'return'
62 11
            : '';
63
64 11
        $methodName = $method->name;
65
66 11
        return sprintf(
67 11
            self::TEMPLATE,
68
            $visibility,
69
            $static,
70
            $reference,
71
            $methodName,
72 11
            implode(',', $parametersCode),
73
            $returnType,
74
            $returnStatement,
75
            $methodName
76
        );
77
    }
78
}
79