Completed
Push — master ( 693288...ff001c )
by Andreas
10s
created

ServiceServiceProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 21.36 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 22
loc 103
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A register() 0 6 2
B registerService() 0 25 3
A addMethodCalls() 0 6 2
A createFactoryFactory() 9 9 1
A resolveArguments() 13 13 2

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\Exception\NotClassDefinitionException;
8
use TomPHP\ContainerConfigurator\ServiceConfig;
9
use TomPHP\ContainerConfigurator\ServiceDefinition;
10
11
final class ServiceServiceProvider extends AbstractServiceProvider
12
{
13
    /**
14
     * @var array
15
     */
16
    private $config;
17
18
    /**
19
     * @api
20
     *
21
     * @param ServiceConfig $config
22
     */
23
    public function __construct(ServiceConfig $config)
24
    {
25
        $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...
26
        $this->provides = $config->getKeys();
27
    }
28
29
    public function register()
30
    {
31
        foreach ($this->config as $config) {
32
            $this->registerService($config);
33
        }
34
    }
35
36
    /**
37
     * @param ServiceDefinition $definition
38
     *
39
     * @return void
40
     *
41
     * @throws NotClassDefinitionException
42
     */
43
    private function registerService(ServiceDefinition $definition)
44
    {
45
        if ($definition->isFactory()) {
46
            $this->getContainer()->add(
47
                $definition->getName(),
48
                $this->createFactoryFactory($definition),
49
                $definition->isSingleton()
50
            );
51
52
            return;
53
        }
54
55
        $service = $this->getContainer()->add(
56
            $definition->getName(),
57
            $definition->getClass(),
58
            $definition->isSingleton()
59
        );
60
61
        if (!$service instanceof ClassDefinition) {
62
            throw NotClassDefinitionException::fromServiceName($definition->getName());
63
        }
64
65
        $service->withArguments($definition->getArguments());
66
        $this->addMethodCalls($service, $definition);
67
    }
68
69
    /**
70
     * @param ClassDefinition   $service
71
     * @param ServiceDefinition $definition
72
     */
73
    private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition)
74
    {
75
        foreach ($definition->getMethods() as $method => $args) {
76
            $service->withMethodCall($method, $args);
77
        }
78
    }
79
80
    /**
81
     * @param ServiceDefinition $definition
82
     *
83
     * @return \Closure
84
     */
85 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...
86
    {
87
        return function () use ($definition) {
88
            $className = $definition->getClass();
89
            $factory = new $className();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
90
91
            return $factory(...$this->resolveArguments($definition->getArguments()));
92
        };
93
    }
94
95
    /**
96
     * @param array $arguments
97
     *
98
     * @return array
99
     */
100 View Code Duplication
    private function resolveArguments(array $arguments)
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...
101
    {
102
        return array_map(
103
            function ($argument) {
104
                if ($this->container->has($argument)) {
105
                    return $this->container->get($argument);
106
                }
107
108
                return $argument;
109
            },
110
            $arguments
111
        );
112
    }
113
}
114