Completed
Push — master ( 95a6e8...b8af6d )
by Filipe
07:12
created

ConstructorInspector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 19.48 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 15
loc 77
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefinition() 0 8 1
A updateDefinition() 15 20 4
A getArguments() 0 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
/**
4
 * This file is part of slick/di package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Di\DependencyInspector;
11
12
use Slick\Common\Base;
13
use Slick\Common\Inspector;
14
use Slick\Di\Definition\Object as ObjectDefinition;
15
16
/**
17
 * Constructor Inspector for dependency definition
18
 *
19
 * @package Slick\Di\DependencyInspector
20
 * @author  Filipe Silva <[email protected]>
21
 *
22
 * @property ObjectDefinition $definition The definition to work with
23
 *
24
 * @method ObjectDefinition getDefinition() Returns current definition
25
 * @method bool isSatisfiable() Check if construction can be achieved
26
 */
27
class ConstructorInspector extends Base
28
{
29
30
    /**
31
     * @readwrite
32
     * @var ObjectDefinition
33
     */
34
    protected $definition;
35
36
    /**
37
     * @read
38
     * @var Parameter[]
39
     */
40
    protected $arguments;
41
42
    /**
43
     * @read
44
     * @var bool
45
     */
46
    protected $satisfiable = true;
47
48
    /**
49
     * @param ObjectDefinition $definition
50
     * @return $this|self|ConstructorInspector
51
     */
52 14
    public function setDefinition(ObjectDefinition $definition)
53
    {
54 14
        $this->definition = $definition;
55 14
        $this->arguments = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<Sli...cyInspector\Parameter>> of property $arguments.

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...
56 14
        $this->satisfiable = true;
57 14
        $this->updateDefinition();
58 14
        return $this;
59
    }
60
61
    /**
62
     * Set definition constructor arguments
63
     */
64 14
    protected function updateDefinition()
65
    {
66 14
        $arguments = [];
67 14 View Code Duplication
        foreach ($this->getArguments() as $param) {
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...
68 12
            if ($this->definition->getContainer()->has($param->getId())) {
69 12
                $arguments[$param->name] = $this->definition
70 12
                    ->getContainer()
71 12
                    ->get($param->getId());
72 12
                continue;
73
            }
74
75 12
            if (!$param->isOptional()) {
76 2
                $this->satisfiable = false;
77 2
                return;
78
            }
79
80 12
            $arguments[$param->name] = $param->default;
81 14
        }
82 14
        $this->definition->constructArgs = $arguments;
83 14
    }
84
85
    /**
86
     * Gets constructor arguments
87
     *
88
     * @return Parameter[]
89
     */
90 14
    protected function getArguments()
91
    {
92 14
        if (is_null($this->arguments)) {
93 14
            $methodInspector = new MethodInspector(
94
                [
95 14
                    'name' => '__construct',
96 14
                    'definition' => $this->definition
97 14
                ]
98 14
            );
99 14
            $this->arguments = $methodInspector->getArguments();
100 14
        }
101 14
        return $this->arguments;
102
    }
103
}
104