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 |
||
17 | class VersionStore |
||
18 | { |
||
19 | const KEY_VERSION = 1; |
||
20 | const KEY_CHILDREN = 2; |
||
21 | |||
22 | /** |
||
23 | * @var CacheInterface |
||
24 | */ |
||
25 | private $cache; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $childInvalidationConstraint; |
||
31 | |||
32 | /** |
||
33 | * @param CacheInterface $cache |
||
34 | * @param string $childInvalidationConstraint |
||
35 | */ |
||
36 | public function __construct(CacheInterface $cache, $childInvalidationConstraint = '') |
||
41 | |||
42 | /** |
||
43 | * @param string $childInvalidationConstraint |
||
44 | * |
||
45 | * @return $this |
||
46 | */ |
||
47 | public function setChildInvalidationConstraint($childInvalidationConstraint) |
||
53 | |||
54 | /** |
||
55 | * @param Request $request |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function fetch(Request $request) |
||
67 | |||
68 | /** |
||
69 | * @param Request $request |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function contains(Request $request) |
||
77 | |||
78 | /** |
||
79 | * @param string $path |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function containsPath($path) |
||
87 | |||
88 | /** |
||
89 | * @param string $key |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function containsKey($key) |
||
97 | |||
98 | /** |
||
99 | * @param Request $request |
||
100 | * @param string $version |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | View Code Duplication | public function update(Request $request, $version) |
|
130 | |||
131 | /** |
||
132 | * @param Request $request |
||
133 | * @param string $version |
||
134 | * |
||
135 | * @return mixed |
||
136 | */ |
||
137 | View Code Duplication | public function register(Request $request, $version) |
|
162 | |||
163 | /** |
||
164 | * @param array $record |
||
165 | * @param string $version |
||
166 | */ |
||
167 | private function invalidateChildren(array $record, $version) |
||
184 | |||
185 | /** |
||
186 | * @param Request $request |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | private function getSegments(Request $request) |
||
206 | |||
207 | /** |
||
208 | * @param Request $request |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | private function createKey(Request $request) |
||
216 | |||
217 | /** |
||
218 | * @param string $key |
||
219 | * @param array $record |
||
220 | */ |
||
221 | private function saveRecord($key, array $record) |
||
225 | |||
226 | /** |
||
227 | * @param string $key |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | private function fetchRecord($key) |
||
235 | |||
236 | /** |
||
237 | * @param array $children |
||
238 | * @param string $version |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | private function createRecord($version = '', $children = []) |
||
246 | |||
247 | /** |
||
248 | * @param array $record |
||
249 | * @param string $version |
||
250 | */ |
||
251 | private function updateVersion(array &$record, $version) |
||
255 | |||
256 | /** |
||
257 | * @param array $record |
||
258 | * @param string $key |
||
259 | */ |
||
260 | private function addChild(array &$record, $key) |
||
264 | |||
265 | /** |
||
266 | * @param array $segments |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | private function createKeyFromSegments(array $segments) |
||
274 | |||
275 | /** |
||
276 | * @param string $key |
||
277 | * @return string |
||
278 | */ |
||
279 | private function createPathFromKey(string $key) |
||
283 | |||
284 | /** |
||
285 | * @param string $path |
||
286 | * @return string |
||
287 | */ |
||
288 | private function createKeyFromPath(string $path) |
||
292 | } |
||
293 |
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.