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 |
||
30 | class InMemoryDiscovery extends AbstractEditableDiscovery |
||
31 | { |
||
32 | /** |
||
33 | * @var BindingType[] |
||
34 | */ |
||
35 | private $types = array(); |
||
36 | |||
37 | /** |
||
38 | * @var Binding[] |
||
39 | */ |
||
40 | private $bindings = array(); |
||
41 | |||
42 | /** |
||
43 | * @var Binding[][] |
||
44 | */ |
||
45 | private $bindingsByTypeName = array(); |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 31 | public function addBindingType(BindingType $type) |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 4 | public function removeBindingType($typeName) |
|
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 2 | public function removeBindingTypes() |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 5 | public function hasBindingType($typeName) |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 25 | public function getBindingType($typeName) |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function hasBindingTypes() |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 3 | public function getBindingTypes() |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 20 | public function addBinding(Binding $binding) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 2 | View Code Duplication | public function removeBinding(Uuid $uuid) |
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 12 | public function findBindings($typeName, Expression $expr = null) |
|
159 | { |
||
160 | 12 | Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s'); |
|
161 | |||
162 | 11 | if (!isset($this->bindingsByTypeName[$typeName])) { |
|
163 | 3 | return array(); |
|
164 | } |
||
165 | |||
166 | 9 | $bindings = $this->bindingsByTypeName[$typeName]; |
|
167 | |||
168 | 9 | if (null !== $expr) { |
|
169 | 1 | $bindings = $this->filterBindings($bindings, $expr); |
|
170 | } |
||
171 | |||
172 | 9 | return array_values($bindings); |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | 17 | public function getBindings() |
|
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 8 | public function hasBinding(Uuid $uuid) |
|
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | 2 | public function getBinding(Uuid $uuid) |
|
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | 2 | protected function removeAllBindings() |
|
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | 1 | protected function removeBindingsThatMatch(Expression $expr) |
|
216 | { |
||
217 | 1 | foreach ($this->bindings as $uuidString => $binding) { |
|
218 | 1 | if ($expr->evaluate($binding)) { |
|
219 | 1 | unset($this->bindings[$uuidString]); |
|
220 | 1 | unset($this->bindingsByTypeName[$binding->getTypeName()][$uuidString]); |
|
221 | } |
||
222 | } |
||
223 | 1 | } |
|
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | 6 | protected function removeBindingsWithTypeName($typeName) |
|
229 | { |
||
230 | 6 | if (!isset($this->bindingsByTypeName[$typeName])) { |
|
231 | 4 | return; |
|
232 | } |
||
233 | |||
234 | 2 | foreach ($this->bindingsByTypeName[$typeName] as $binding) { |
|
235 | 2 | unset($this->bindings[$binding->getUuid()->toString()]); |
|
236 | } |
||
237 | |||
238 | 2 | unset($this->bindingsByTypeName[$typeName]); |
|
239 | 2 | } |
|
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | 3 | View Code Duplication | protected function removeBindingsWithTypeNameThatMatch($typeName, Expression $expr) |
245 | { |
||
246 | 3 | if (!isset($this->bindingsByTypeName[$typeName])) { |
|
247 | 2 | return; |
|
248 | } |
||
249 | |||
250 | 1 | foreach ($this->bindingsByTypeName[$typeName] as $uuidString => $binding) { |
|
251 | 1 | if ($expr->evaluate($binding)) { |
|
252 | 1 | unset($this->bindings[$uuidString]); |
|
253 | 1 | unset($this->bindingsByTypeName[$typeName][$uuidString]); |
|
254 | } |
||
255 | } |
||
256 | 1 | } |
|
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | 2 | protected function hasAnyBinding() |
|
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | protected function hasBindingsThatMatch(Expression $expr) |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | 2 | protected function hasBindingsWithTypeName($typeName) |
|
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | 2 | protected function hasBindingsWithTypeNameThatMatch($typeName, Expression $expr) |
|
293 | } |
||
294 |