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

FactoryDefinition::getReflectionMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\Funky;
5
6
use Psr\Container\ContainerInterface;
7
use ReflectionMethod;
8
use TheCodingMachine\Funky\Annotations\Factory;
9
use TheCodingMachine\Funky\Injections\Injection;
10
11
class FactoryDefinition extends AbstractDefinition
12
{
13
    private $aliases;
14
15 View Code Duplication
    public function __construct(ReflectionMethod $reflectionMethod, Factory $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...
16
    {
17
        if (!$reflectionMethod->isPublic()) {
18
            throw BadModifierException::mustBePublic($reflectionMethod, '@Factory');
19
        }
20
        if (!$reflectionMethod->isStatic()) {
21
            throw BadModifierException::mustBeStatic($reflectionMethod, '@Factory');
22
        }
23
24
        if ($annotation->isFromMethodName()) {
25
            $this->name = $reflectionMethod->getName();
26
        } elseif ($annotation->isFromType()) {
27
            $returnType = $reflectionMethod->getReturnType();
28
            if ($returnType === null) {
29
                throw UnknownTypeException::create($reflectionMethod);
30
            }
31
            $this->name = (string) $returnType;
32
        } else {
33
            $this->name = (string) $annotation->getName();
34
        }
35
36
        parent::__construct($reflectionMethod);
37
        $this->aliases = $annotation->getAliases();
38
    }
39
40
    /**
41
     * Returns true if the signature of the reflection method is compatible with container-interop/service-provider
42
     * factories.
43
     */
44
    public function isPsrFactory(): bool
45
    {
46
        $numberOfParameters = $this->reflectionMethod->getNumberOfParameters();
47
        if ($numberOfParameters > 1) {
48
            return false;
49
        }
50
        if ($numberOfParameters === 0) {
51
            return true;
52
        }
53
        $parameter = $this->reflectionMethod->getParameters()[0];
54
        return $parameter !== null && (string) $parameter->getType() === ContainerInterface::class;
55
    }
56
57
    public function buildFactoryCode(string $functionName) : string
58
    {
59
        $returnTypeCode = '';
60
        $returnType = $this->reflectionMethod->getReturnType();
61 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...
62
            if ($returnType->isBuiltin()) {
63
                $returnTypeCode = ': '.$this->reflectionMethod->getReturnType();
64
            } else {
65
                $returnTypeCode = ': \\'.$this->reflectionMethod->getReturnType();
66
            }
67
        }
68
69
        return sprintf(
70
            <<<EOF
71
    public static function %s(ContainerInterface \$container)%s
72
    {
73
        return %s::%s(%s);
74
    }
75
    
76
EOF
77
            ,
78
            $functionName,
79
            $returnTypeCode,
80
            '\\'.$this->reflectionMethod->getDeclaringClass()->getName(),
81
            $this->reflectionMethod->getName(),
82
            implode(', ', array_map(function (Injection $injection) {
83
                return $injection->getCode();
84
            }, $this->getInjections()))
85
        );
86
    }
87
88
    /**
89
     * Returns a list of services to be injected.
90
     *
91
     * @return Injection[]
92
     */
93
    private function getInjections(): array
94
    {
95
        return array_map([$this, 'mapParameterToInjection'], $this->getReflectionMethod()->getParameters());
96
    }
97
98
    /**
99
     * @return string[]
100
     */
101
    public function getAliases(): array
102
    {
103
        return $this->aliases;
104
    }
105
}
106