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:
Complex classes like KeyValueStoreDiscovery often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use KeyValueStoreDiscovery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class KeyValueStoreDiscovery extends AbstractEditableDiscovery |
||
32 | { |
||
33 | /** |
||
34 | * @var KeyValueStore |
||
35 | */ |
||
36 | private $store; |
||
37 | |||
38 | /** |
||
39 | * Stores the integer key that will be used when adding the next binding type. |
||
40 | * |
||
41 | * Synchronized with the entry "::nextKey" in the store. |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | private $nextKey; |
||
46 | |||
47 | /** |
||
48 | * Stores an integer "key" for each binding type name. |
||
49 | * |
||
50 | * Contains each key only once. |
||
51 | * |
||
52 | * Synchronized with the entry "::keysByTypeName" in the store. |
||
53 | * |
||
54 | * @var int[] |
||
55 | */ |
||
56 | private $keysByTypeName; |
||
57 | |||
58 | /** |
||
59 | * Stores the binding type for each key. |
||
60 | * |
||
61 | * Synchronized with the entries "t:<key>" in the store. |
||
62 | * |
||
63 | * @var BindingType[] |
||
64 | */ |
||
65 | private $typesByKey = array(); |
||
66 | |||
67 | /** |
||
68 | * Stores the bindings for each key. |
||
69 | * |
||
70 | * Synchronized with the entries "b:<key>" in the store. |
||
71 | * |
||
72 | * @var Binding[][] |
||
73 | */ |
||
74 | private $bindingsByKey = array(); |
||
75 | |||
76 | /** |
||
77 | * Creates a new discovery. |
||
78 | * |
||
79 | * @param KeyValueStore $store The key-value store used to |
||
80 | * store the bindings and the |
||
81 | * binding types. |
||
82 | * @param BindingInitializer[] $initializers The binding initializers to |
||
83 | * apply to newly created or |
||
84 | * unserialized bindings. |
||
85 | */ |
||
86 | 86 | public function __construct(KeyValueStore $store, array $initializers = array()) |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 54 | public function addBindingType(BindingType $type) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 8 | public function removeBindingType($typeName) |
|
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | 4 | public function removeBindingTypes() |
|
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | 10 | public function hasBindingType($typeName) |
|
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | 40 | public function getBindingType($typeName) |
|
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | public function hasBindingTypes() |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | 6 | public function getBindingTypes() |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 32 | public function addBinding(Binding $binding) |
|
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | 20 | public function findBindings($typeName, Expression $expr = null) |
|
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | 30 | public function getBindings() |
|
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | 4 | protected function removeAllBindings() |
|
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | */ |
||
298 | 2 | protected function removeBindingsThatMatch(Expression $expr) |
|
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | 6 | protected function removeBindingsWithTypeName($typeName) |
|
336 | |||
337 | /** |
||
338 | * {@inheritdoc} |
||
339 | */ |
||
340 | 6 | protected function removeBindingsWithTypeNameThatMatch($typeName, Expression $expr) |
|
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | 4 | protected function hasAnyBinding() |
|
381 | |||
382 | /** |
||
383 | * {@inheritdoc} |
||
384 | */ |
||
385 | protected function hasBindingsThatMatch(Expression $expr) |
||
399 | |||
400 | /** |
||
401 | * {@inheritdoc} |
||
402 | */ |
||
403 | 8 | protected function hasBindingsWithTypeName($typeName) |
|
413 | |||
414 | 4 | protected function hasBindingsWithTypeNameThatMatch($typeName, Expression $expr) |
|
434 | |||
435 | 30 | private function loadAllBindings() |
|
452 | |||
453 | 34 | private function loadBindingsForKey($key, $initialize = true) |
|
461 | |||
462 | 6 | private function reindexBindingsForKey($key) |
|
466 | |||
467 | 6 | private function syncBindingsForKey($key) |
|
476 | } |
||
477 |
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.