ParameterTemplate   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 4 1
B doGenerate() 0 38 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator\Template;
5
6
/**
7
 * Class ParameterTemplate
8
 * @package Moka\Generator\Template
9
 */
10
class ParameterTemplate implements TemplateInterface
11
{
12
    private const TEMPLATE = '%s %s%s%s%s';
13
14
    /**
15
     * @param \Reflector|\ReflectionParameter $parameter
16
     * @return string
17
     */
18 11
    public static function generate(\Reflector $parameter): string
19
    {
20 11
        return static::doGenerate($parameter);
21
    }
22
23
    /**
24
     * @param \ReflectionParameter $parameter
25
     * @return string
26
     */
27 11
    protected static function doGenerate(\ReflectionParameter $parameter): string
28
    {
29
        try {
30 11
            $defaultValue = !$parameter->allowsNull()
31 11
                ? var_export($parameter->getDefaultValue(), true)
32 11
                : 'null';
33
34 11
            $defaultValue = '=' . $defaultValue;
35 10
        } catch (\Exception $exception) {
36 10
            $defaultValue = '';
37
        }
38
39 11
        $type = $parameter->getType()
40 11
            ? $parameter->getType()->getName()
41 11
            : '';
42
43 11
        $isPassedByReference = $parameter->isPassedByReference()
44 5
            ? '&'
45 11
            : '';
46
47 11
        $isVariadic = '';
48 11
        if ($parameter->isVariadic()) {
49 5
            $isVariadic = '...';
50 5
            $isPassedByReference = '';
51 5
            $defaultValue = '';
52
        }
53
54 11
        $name = '$' . $parameter->name;
55
56 11
        return sprintf(
57 11
            self::TEMPLATE,
58
            $type,
59
            $isVariadic,
60
            $isPassedByReference,
61
            $name,
62
            $defaultValue
63
        );
64
    }
65
}
66