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 |
||
9 | class ServiceConfig implements InjectorConfig |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private $config; |
||
15 | |||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | private $sharedByDefault = true; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $shared = []; |
||
25 | |||
26 | 4 | public function __construct(array $config) |
|
30 | |||
31 | /** |
||
32 | * @param Injector $injector |
||
33 | * @return void |
||
34 | */ |
||
35 | 4 | public function apply(Injector $injector) |
|
74 | |||
75 | /** |
||
76 | * @param string $name |
||
77 | * @return bool |
||
78 | */ |
||
79 | 4 | private function isShared($name) |
|
87 | |||
88 | /** |
||
89 | * @param array $services |
||
90 | * @return void |
||
91 | */ |
||
92 | 4 | View Code Duplication | private function applyAliases(Injector $injector, array $services) |
101 | |||
102 | /** |
||
103 | * @param array $services |
||
104 | * @return void |
||
105 | */ |
||
106 | 4 | View Code Duplication | private function applyDelegates(Injector $injector, array $services) |
115 | |||
116 | /** |
||
117 | * @param array $delegators |
||
118 | * @return void |
||
119 | */ |
||
120 | 4 | private function applyDelegators(Injector $injector, array $delegators) |
|
130 | |||
131 | /** |
||
132 | * Create a chained prepare() |
||
133 | * |
||
134 | * @param string $service |
||
135 | * @param string[] $delegators |
||
136 | * @return callable |
||
137 | */ |
||
138 | 4 | private function createDelegator($service, array $delegators) |
|
145 | |||
146 | /** |
||
147 | * Create a reducer for a chained prepare() |
||
148 | * |
||
149 | * @param string $service |
||
150 | * @return callable |
||
151 | */ |
||
152 | 4 | private function delegatorReducer(Injector $injector, $service) |
|
163 | |||
164 | /** |
||
165 | * Curry the delegator to only require a container |
||
166 | * |
||
167 | * @param callable $delegator that will be ultimately called |
||
168 | * @param string $service name of service being prepared |
||
169 | * @param callable $callable that returns the instance |
||
170 | * @return callable |
||
171 | */ |
||
172 | 4 | private function curryDelegator(callable $delegator, $service, callable $callable) |
|
178 | |||
179 | /** |
||
180 | * Returns a function that always returns the same value |
||
181 | * |
||
182 | * Also known as a "kestrel" or "k combinator". |
||
183 | * |
||
184 | * @param mixed $x |
||
185 | * @return callable |
||
186 | */ |
||
187 | 4 | private function k($x) |
|
193 | |||
194 | /** |
||
195 | * @param array $values |
||
196 | * @return callable[] |
||
197 | */ |
||
198 | 4 | private function kAll(array $values) |
|
207 | } |
||
208 |
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.