Completed
Push — master ( 5b7dfb...3e297a )
by David
12s
created

ExtensionDefinition   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 90
Duplicated Lines 33.33 %

Importance

Changes 0
Metric Value
dl 30
loc 90
rs 10
c 0
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 22 22 6
C buildExtensionCode() 7 51 8
A getInjections() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
namespace TheCodingMachine\Funky;
5
6
use ReflectionMethod;
7
use TheCodingMachine\Funky\Annotations\Extension;
8
use TheCodingMachine\Funky\Injections\Injection;
9
10
class ExtensionDefinition extends AbstractDefinition
11
{
12 View Code Duplication
    public function __construct(ReflectionMethod $reflectionMethod, Extension $annotation)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
    {
14
        if (!$reflectionMethod->isPublic()) {
15
            throw BadModifierException::mustBePublic($reflectionMethod, '@Extension');
16
        }
17
        if (!$reflectionMethod->isStatic()) {
18
            throw BadModifierException::mustBeStatic($reflectionMethod, '@Extension');
19
        }
20
21
        if ($annotation->isFromMethodName()) {
22
            $this->name = $reflectionMethod->getName();
23
        } elseif ($annotation->isFromType()) {
24
            $returnType = $reflectionMethod->getReturnType();
25
            if ($returnType === null) {
26
                throw UnknownTypeException::create($reflectionMethod);
27
            }
28
            $this->name = (string) $returnType;
29
        } else {
30
            $this->name = (string) $annotation->getName();
31
        }
32
33
        parent::__construct($reflectionMethod);
34
    }
35
36
    public function buildExtensionCode(string $functionName) : string
37
    {
38
        $returnTypeCode = '';
39
        $returnType = $this->reflectionMethod->getReturnType();
40 View Code Duplication
        if ($returnType) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
            if ($returnType->isBuiltin()) {
42
                $returnTypeCode = ': '.$this->reflectionMethod->getReturnType();
43
            } else {
44
                $returnTypeCode = ': \\'.$this->reflectionMethod->getReturnType();
45
            }
46
        }
47
48
        $parameters = $this->reflectionMethod->getParameters();
49
50
        $previousParameterCode = '';
51
        $previousTypeCode = '';
52
        if (count($parameters) >= 1) {
53
            $previousParameter = $this->reflectionMethod->getParameters()[0];
54
            $previousType = $previousParameter->getType();
55
            if ($previousType) {
56
                if ($previousType->isBuiltin()) {
57
                    $previousTypeCode = (string) $previousType.' ';
58
                } else {
59
                    $previousTypeCode = '\\'.(string) $previousType.' ';
60
                }
61
            }
62
            $previousParameterCode = ', '.$previousTypeCode.'$previous';
63
            if ($previousParameter->isDefaultValueAvailable()) {
64
                $previousParameterCode .= ' = '.var_export($previousParameter->getDefaultValue(), true);
65
            }
66
        }
67
68
69
        return sprintf(
70
            <<<EOF
71
    public static function %s(ContainerInterface \$container%s)%s
72
    {
73
        return %s::%s(%s%s);
74
    }
75
    
76
EOF
77
            ,
78
            $functionName,
79
            $previousParameterCode,
80
            $returnTypeCode,
81
            '\\'.$this->reflectionMethod->getDeclaringClass()->getName(),
82
            $this->reflectionMethod->getName(),
83
            $previousParameterCode ? '$previous': '',
84
            implode('', array_map(function (Injection $injection) {
85
                return ', '.$injection->getCode();
86
            }, $this->getInjections()))
87
        );
88
    }
89
90
    /**
91
     * Returns a list of services to be injected.
92
     *
93
     * @return Injection[]
94
     */
95
    private function getInjections(): array
96
    {
97
        $parameters = $this->getReflectionMethod()->getParameters();
98
        array_shift($parameters);
99
        return array_map([$this, 'mapParameterToInjection'], $parameters);
100
    }
101
}
102