ServiceServiceProvider   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 143
Duplicated Lines 6.29 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 9
loc 143
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A register() 0 6 2
B registerService() 0 34 4
A addMethodCalls() 0 6 2
A createAliasFactory() 0 6 1
A createFactoryFactory() 9 9 1
A injectContainer() 0 11 2
A resolveArguments() 0 17 3

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
namespace TomPHP\ContainerConfigurator\League;
4
5
use League\Container\Definition\ClassDefinition;
6
use League\Container\ServiceProvider\AbstractServiceProvider;
7
use TomPHP\ContainerConfigurator\Configurator;
8
use TomPHP\ContainerConfigurator\Exception\NotClassDefinitionException;
9
use TomPHP\ContainerConfigurator\ServiceConfig;
10
use TomPHP\ContainerConfigurator\ServiceDefinition;
11
12
/**
13
 * @internal
14
 */
15
final class ServiceServiceProvider extends AbstractServiceProvider
16
{
17
    /**
18
     * @var array
19
     */
20
    private $config;
21
22
    /**
23
     * @param ServiceConfig $config
24
     */
25
    public function __construct(ServiceConfig $config)
26
    {
27
        $this->config   = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<TomPHP\ContainerC...igurator\ServiceConfig> is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
        $this->provides = $config->getKeys();
29
    }
30
31
    public function register()
32
    {
33
        foreach ($this->config as $config) {
34
            $this->registerService($config);
35
        }
36
    }
37
38
    /**
39
     * @param ServiceDefinition $definition
40
     *
41
     * @throws NotClassDefinitionException
42
     *
43
     * @return void
44
     */
45
    private function registerService(ServiceDefinition $definition)
46
    {
47
        if ($definition->isFactory()) {
48
            $this->getContainer()->add(
49
                $definition->getName(),
50
                $this->createFactoryFactory($definition),
51
                $definition->isSingleton()
52
            );
53
54
            return;
55
        }
56
57
        if ($definition->isAlias()) {
58
            $this->getContainer()->add(
59
                $definition->getName(),
60
                $this->createAliasFactory($definition)
61
            );
62
63
            return;
64
        }
65
66
        $service = $this->getContainer()->add(
67
            $definition->getName(),
68
            $definition->getClass(),
69
            $definition->isSingleton()
70
        );
71
72
        if (!$service instanceof ClassDefinition) {
73
            throw NotClassDefinitionException::fromServiceName($definition->getName());
74
        }
75
76
        $service->withArguments($this->injectContainer($definition->getArguments()));
77
        $this->addMethodCalls($service, $definition);
78
    }
79
80
    /**
81
     * @param ClassDefinition   $service
82
     * @param ServiceDefinition $definition
83
     */
84
    private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition)
85
    {
86
        foreach ($definition->getMethods() as $method => $args) {
87
            $service->withMethodCall($method, $this->injectContainer($args));
88
        }
89
    }
90
91
    /**
92
     * @param ServiceDefinition $definition
93
     *
94
     * @return \Closure
95
     */
96
    private function createAliasFactory(ServiceDefinition $definition)
97
    {
98
        return function () use ($definition) {
99
            return $this->getContainer()->get($definition->getClass());
100
        };
101
    }
102
103
    /**
104
     * @param ServiceDefinition $definition
105
     *
106
     * @return \Closure
107
     */
108 View Code Duplication
    private function createFactoryFactory(ServiceDefinition $definition)
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...
109
    {
110
        return function () use ($definition) {
111
            $className = $definition->getClass();
112
            $factory   = new $className();
113
114
            return $factory(...$this->resolveArguments($definition->getArguments()));
115
        };
116
    }
117
118
    /**
119
     * @param array $arguments
120
     *
121
     * @return array
122
     */
123
    private function injectContainer(array $arguments)
124
    {
125
        return array_map(
126
            function ($argument) {
127
                return ($argument === Configurator::container())
128
                    ? $this->container
129
                    : $argument;
130
            },
131
            $arguments
132
        );
133
    }
134
135
    /**
136
     * @param array $arguments
137
     *
138
     * @return array
139
     */
140
    private function resolveArguments(array $arguments)
141
    {
142
        return array_map(
143
            function ($argument) {
144
                if ($argument === Configurator::container()) {
145
                    return $this->container;
146
                }
147
148
                if ($this->container->has($argument)) {
149
                    return $this->container->get($argument);
150
                }
151
152
                return $argument;
153
            },
154
            $arguments
155
        );
156
    }
157
}
158