StepRegistration   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 153
Duplicated Lines 6.54 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 4
dl 10
loc 153
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A replaceWith() 0 6 2
A registerInBuilder() 0 6 2
A getStepId() 0 4 1
A getStepFqcn() 0 4 1
A getFactory() 0 4 1
A getDescription() 0 4 1
A getBefores() 0 4 1
A getAfters() 0 4 1
A insertBeforeIfExists() 0 5 1
A insertBefore() 0 5 1
A insertAfterIfExists() 0 5 1
A insertAfter() 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
namespace PSB\Core\Pipeline;
3
4
5
use PSB\Core\ObjectBuilder\BuilderInterface;
6
use PSB\Core\Util\Guard;
7
8
class StepRegistration
9
{
10
    /**
11
     * @var string
12
     */
13
    private $stepId;
14
15
    /**
16
     * @var string
17
     */
18
    private $stepFqcn;
19
20
    /**
21
     * @var callable|null
22
     */
23
    private $factory;
24
25
    /**
26
     * @var null|string
27
     */
28
    private $description;
29
30
    /**
31
     * @var StepRegistrationDependency[]
32
     */
33
    private $befores = [];
34
35
    /**
36
     * @var StepRegistrationDependency[]
37
     */
38
    private $afters = [];
39
40
    /**
41
     * @param string        $stepId
42
     * @param string        $stepFqcn
43
     * @param callable|null $factory
44
     * @param string|null   $description
45
     */
46 32 View Code Duplication
    public function __construct($stepId, $stepFqcn, callable $factory = null, $description = null)
47
    {
48 32
        Guard::againstNullAndEmpty('stepId', $stepId);
49 31
        Guard::againstNullAndEmpty('stepClass', $stepFqcn);
50
51 30
        $this->stepId = $stepId;
52 30
        $this->stepFqcn = $stepFqcn;
53 30
        $this->factory = $factory;
54 30
        $this->description = $description;
55 30
    }
56
57
    /**
58
     * @param StepReplacement $replacement
59
     */
60 1
    public function replaceWith(StepReplacement $replacement)
61
    {
62 1
        $this->stepFqcn = $replacement->getStepFqcn();
63 1
        $this->description = $replacement->getDescription() ?: $this->description;
64 1
        $this->factory = $replacement->getFactory();
65 1
    }
66
67
    /**
68
     * @param BuilderInterface $builder
69
     */
70 2
    public function registerInBuilder(BuilderInterface $builder)
71
    {
72 2
        if ($this->factory) {
73 2
            $builder->defineSingleton($this->stepFqcn, $this->factory);
74
        }
75 2
    }
76
77
    /**
78
     * @return string
79
     */
80 17
    public function getStepId()
81
    {
82 17
        return $this->stepId;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 11
    public function getStepFqcn()
89
    {
90 11
        return $this->stepFqcn;
91
    }
92
93
    /**
94
     * @return callable|null
95
     */
96 2
    public function getFactory()
97
    {
98 2
        return $this->factory;
99
    }
100
101
    /**
102
     * @return null|string
103
     */
104 2
    public function getDescription()
105
    {
106 2
        return $this->description;
107
    }
108
109
    /**
110
     * @return StepRegistrationDependency[]
111
     */
112 14
    public function getBefores()
113
    {
114 14
        return $this->befores;
115
    }
116
117
    /**
118
     * @return StepRegistrationDependency[]
119
     */
120 13
    public function getAfters()
121
    {
122 13
        return $this->afters;
123
    }
124
125
    /**
126
     * @param string $id
127
     */
128 1
    public function insertBeforeIfExists($id)
129
    {
130 1
        Guard::againstNullAndEmpty('id', $id);
131 1
        $this->befores[] = new StepRegistrationDependency($this->stepId, $id, false);
132 1
    }
133
134
    /**
135
     * @param string $id
136
     */
137 5
    public function insertBefore($id)
138
    {
139 5
        Guard::againstNullAndEmpty('id', $id);
140 4
        $this->befores[] = new StepRegistrationDependency($this->stepId, $id, true);
141 4
    }
142
143
    /**
144
     * @param string $id
145
     */
146 1
    public function insertAfterIfExists($id)
147
    {
148 1
        Guard::againstNullAndEmpty('id', $id);
149 1
        $this->afters[] = new StepRegistrationDependency($this->stepId, $id, false);
150 1
    }
151
152
    /**
153
     * @param string $id
154
     */
155 5
    public function insertAfter($id)
156
    {
157 5
        Guard::againstNullAndEmpty('id', $id);
158 4
        $this->afters[] = new StepRegistrationDependency($this->stepId, $id, true);
159 4
    }
160
}
161