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

ParameterTemplate::doGenerate()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 27
nc 32
nop 1
dl 0
loc 38
ccs 26
cts 26
cp 1
crap 6
rs 8.439
c 0
b 0
f 0
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
    const TEMPLATE = '%s %s%s%s%s';
13
14
    /**
15
     * @param \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 (\ReflectionException $exception) {
36 10
            $defaultValue = '';
37
        }
38
39 11
        $type = $parameter->getType()
40 11
            ? (string)$parameter->getType()
41 11
            : '';
42
43 11
        $isPassedByReference = $parameter->isPassedByReference()
44 5
            ? '&'
45 11
            : '';
46
47 11
        $isVariadic = '';
48 11
        if ($parameter->isVariadic()) {
49 4
            $isVariadic = '...';
50 4
            $isPassedByReference = '';
51 4
            $defaultValue = '';
52
        }
53
54 11
        $name = '$' . $parameter->name;
55
56 11
        return sprintf(
57 11
            self::TEMPLATE,
58 11
            $type,
59 11
            $isVariadic,
60 11
            $isPassedByReference,
61 11
            $name,
62 11
            $defaultValue
63
        );
64
    }
65
}
66