Completed
Pull Request — master (#37)
by Alberto
06:07
created

ProxyParameterTemplate::doGenerate()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 27
nc 32
nop 1
dl 0
loc 38
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 ProxyParameterTemplate
8
 * @package Moka\Generator\Template
9
 */
10
class ProxyParameterTemplate implements ProxyTemplateInterface
11
{
12
    const TEMPLATE = '%s %s%s%s%s';
13
14
    /**
15
     * @param \ReflectionParameter $parameter
16
     * @return string
17
     */
18
    public static function generate(\Reflector $parameter): string
19
    {
20
        return static::doGenerate($parameter);
1 ignored issue
show
Compatibility introduced by
$parameter of type object<Reflector> is not a sub-type of object<ReflectionParameter>. It seems like you assume a concrete implementation of the interface Reflector to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
21
    }
22
23
    /**
24
     * @param \ReflectionParameter $parameter
25
     * @return string
26
     */
27
    protected static function doGenerate(\ReflectionParameter $parameter): string
28
    {
29
        try {
30
            $defaultValue = !$parameter->allowsNull()
31
                ? var_export($parameter->getDefaultValue(), true)
32
                : 'null';
33
34
            $defaultValue = '=' . $defaultValue;
35
        } catch (\ReflectionException $exception) {
36
            $defaultValue = '';
37
        }
38
39
        $type = $parameter->getType()
40
            ? (string)$parameter->getType()
41
            : '';
42
43
        $isPassedByReference = $parameter->isPassedByReference()
44
            ? '&'
45
            : '';
46
47
        $isVariadic = '';
48
        if ($parameter->isVariadic()) {
49
            $isVariadic = '...';
50
            $isPassedByReference = '';
51
            $defaultValue = '';
52
        }
53
54
        $name = '$' . $parameter->getName();
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
55
56
        return sprintf(
57
            self::TEMPLATE,
58
            $type,
59
            $isVariadic,
60
            $isPassedByReference,
61
            $name,
62
            $defaultValue
63
        );
64
    }
65
}
66