Completed
Pull Request — master (#37)
by Angelo
03:16 queued 01:16
created

ProxyMethodGenerator::generateMethodString()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7.0052

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 20
cts 21
cp 0.9524
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 16
nop 1
crap 7.0052
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator;
5
6
class ProxyMethodGenerator
7
{
8
    private static $template = '
9
        public %s function %s(%s)%s
10
        {
11
            %s$this->__call("%s", func_get_args());
12
        }
13
    ';
14
15 8
    public static function generateMethodString(\ReflectionMethod $method)
16
    {
17 8
        $static = $method->isStatic() ? 'static' : '';
18 8
        $originalReturnType = $method->getReturnType();
19
20 8
        $returnType = !$originalReturnType ? '' : ProxyReturnGenerator::generateMethodReturnType($originalReturnType, $method);
21
22 8
        $parameters = $method->getParameters();
23 8
        $arguments = [];
24 8
        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...
25 8
            foreach ($method->getParameters() as $parameter) {
26 8
                $arguments[] = ProxyArgumentGenerator::generateMethodParameter($parameter);
27
            }
28
        }
29
30 8
        $method->getReturnType();
31
32 8
        $returnStatement = 'return ';
33 8
        if (null !== $originalReturnType && self::getType($originalReturnType) === 'void') {
34
            $returnStatement = '';
35
        }
36
37 8
        return sprintf(
38 8
            self::$template,
39 8
            $static,
40 8
            $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...
41 8
            implode(', ', $arguments),
42 8
            $returnType,
43 8
            $returnStatement,
44 8
            $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...
45
        );
46
    }
47
48 2
    protected static function getType(\ReflectionType $type)
49
    {
50 2
        return (string)$type;
51
    }
52
}
53