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

ProxyMethodTemplate::doGenerate()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 32
nop 1
dl 0
loc 33
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 ProxyMethodTemplate
8
 * @package Moka\Generator\Template
9
 */
10
class ProxyMethodTemplate implements ProxyTemplateInterface
11
{
12
    const TEMPLATE = '
13
        public %s function %s(%s)%s
14
        {
15
            %s $this->__call("%s", func_get_args());
16
        }
17
    ';
18
19
    /**
20
     * @param \ReflectionMethod $method
21
     * @return string
22
     */
23
    public static function generate(\Reflector $method): string
24
    {
25
        return static::doGenerate($method);
1 ignored issue
show
Compatibility introduced by
$method of type object<Reflector> is not a sub-type of object<ReflectionMethod>. 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...
26
    }
27
28
    /**
29
     * @param \ReflectionMethod $method
30
     * @return string
31
     */
32
    protected static function doGenerate(\ReflectionMethod $method): string
33
    {
34
        $static = $method->isStatic() ? 'static' : '';
35
36
        $parameters = $method->getParameters();
37
        $parametersCode = [];
38
        if ($parameters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parameters of type ReflectionParameter[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39
            foreach ($parameters as $parameter) {
40
                $parametersCode[] = ProxyParameterTemplate::generate($parameter);
41
            }
42
        }
43
44
        $originalReturnType = $method->getReturnType();
45
        $returnType = $originalReturnType
46
            ? ProxyReturnTypeTemplate::generate($method)
47
            : '';
48
49
        $returnStatement = null === $originalReturnType || 'void' !== (string)$originalReturnType
50
            ? 'return'
51
            : '';
52
53
        $methodName = $method->getName();
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
54
55
        return sprintf(
56
            self::TEMPLATE,
57
            $static,
58
            $methodName,
59
            implode(',', $parametersCode),
60
            $returnType,
61
            $returnStatement,
62
            $methodName
63
        );
64
    }
65
}
66