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 |
||
11 | class DeltaConfigCollection extends MemoryConfigCollection |
||
12 | { |
||
13 | /** |
||
14 | * Remove delta |
||
15 | */ |
||
16 | const REMOVE = 'remove'; |
||
17 | |||
18 | /** |
||
19 | * Merge delta |
||
20 | */ |
||
21 | const MERGE = 'merge'; |
||
22 | |||
23 | /** |
||
24 | * Remove all config for this class |
||
25 | */ |
||
26 | const CLEAR = 'clear'; |
||
27 | |||
28 | /** |
||
29 | * Replace all config for this class |
||
30 | */ |
||
31 | const REPLACE = 'replace'; |
||
32 | |||
33 | /** |
||
34 | * Set delta |
||
35 | */ |
||
36 | const SET = 'set'; |
||
37 | |||
38 | /** |
||
39 | * @var DeltaMiddleware |
||
40 | */ |
||
41 | protected $deltaMiddleware = null; |
||
42 | |||
43 | /** |
||
44 | * List of deltas keyed by class |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $deltas = []; |
||
49 | |||
50 | /** |
||
51 | * True if removeAll() is applied |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $deltaReset = false; |
||
56 | |||
57 | /** |
||
58 | * Construct a delta collection |
||
59 | * |
||
60 | * @param bool $trackMetadata Set to true to track metadata |
||
61 | * @param int $middlewareFlag Flag to use to disable delta middleware (optional) |
||
62 | */ |
||
63 | 4 | public function __construct($trackMetadata = false, $middlewareFlag = 0) |
|
69 | |||
70 | /** |
||
71 | * Create a delta collection from a parent collection |
||
72 | * |
||
73 | * @param ConfigCollectionInterface $parent |
||
74 | * @param int $middlewareFlag Flag to use to disable delta middleware (optional) |
||
75 | * @return static |
||
76 | */ |
||
77 | 4 | public static function createFromCollection(ConfigCollectionInterface $parent, $middlewareFlag = 0) |
|
94 | |||
95 | /** |
||
96 | * Get middleware for handling deltas |
||
97 | * |
||
98 | * @return DeltaMiddleware |
||
99 | */ |
||
100 | 4 | public function getDeltaMiddleware() |
|
104 | |||
105 | 4 | public function getMiddlewares() |
|
111 | |||
112 | /** |
||
113 | * Get deltas for the given class |
||
114 | * |
||
115 | * @param string $class |
||
116 | * @return array |
||
117 | */ |
||
118 | 4 | public function getDeltas($class) |
|
125 | |||
126 | /** |
||
127 | * Check if config should be completely reset before getting config |
||
128 | * |
||
129 | * @param string $class Class to check |
||
130 | * @return bool |
||
131 | */ |
||
132 | 4 | public function isDeltaReset($class = null) |
|
147 | |||
148 | public function unserialize($serialized) |
||
153 | |||
154 | public function __clone() |
||
159 | |||
160 | /** |
||
161 | * Restore back-links post-creation or unserialization |
||
162 | */ |
||
163 | 4 | protected function postInit() |
|
168 | |||
169 | 1 | public function set($class, $name, $data, $metadata = []) |
|
186 | |||
187 | 2 | public function remove($class, $name = null) |
|
203 | |||
204 | 3 | public function merge($class, $name, $value) |
|
218 | |||
219 | 1 | public function removeAll() |
|
224 | |||
225 | /** |
||
226 | * Remove all deltas for the given class and/or key combination |
||
227 | * |
||
228 | * @param string $class Optional class to limit clear to |
||
229 | * @param string $key Optional field to limit clear to |
||
230 | */ |
||
231 | 3 | protected function clearDeltas($class = null, $key = null) |
|
264 | |||
265 | /** |
||
266 | * Push new delta |
||
267 | * |
||
268 | * @param string $class |
||
269 | * @param array $delta |
||
270 | */ |
||
271 | 4 | protected function addDelta($class, $delta) |
|
282 | } |
||
283 |
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.