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 ClientLocal 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 ClientLocal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ClientLocal extends AbstractClient |
||
32 | { |
||
33 | /** |
||
34 | * @var BaseTableGateway[] |
||
35 | */ |
||
36 | protected $tableGateways = []; |
||
37 | |||
38 | /** |
||
39 | * @var Connection |
||
40 | */ |
||
41 | protected $connection = null; |
||
42 | |||
43 | /** |
||
44 | * ClientLocal constructor. |
||
45 | * |
||
46 | * @param $connection |
||
47 | */ |
||
48 | public function __construct($connection) |
||
52 | |||
53 | /** |
||
54 | * @inheritDoc |
||
55 | */ |
||
56 | public function getTables(array $params = []) |
||
60 | |||
61 | /** |
||
62 | * @inheritDoc |
||
63 | */ |
||
64 | public function getTable($tableName) |
||
68 | |||
69 | /** |
||
70 | * @inheritDoc |
||
71 | */ |
||
72 | public function getColumns($tableName, array $params = []) |
||
76 | |||
77 | /** |
||
78 | * @inheritDoc |
||
79 | */ |
||
80 | public function getColumn($tableName, $columnName) |
||
84 | |||
85 | /** |
||
86 | * @inheritDoc |
||
87 | */ |
||
88 | public function getEntries($tableName, array $params = []) |
||
94 | |||
95 | /** |
||
96 | * @inheritDoc |
||
97 | */ |
||
98 | public function getEntry($tableName, $id, array $params = []) |
||
105 | |||
106 | /** |
||
107 | * @inheritDoc |
||
108 | */ |
||
109 | public function getUsers(array $params = []) |
||
114 | |||
115 | /** |
||
116 | * @inheritDoc |
||
117 | */ |
||
118 | public function getUser($id, array $params = []) |
||
122 | |||
123 | /** |
||
124 | * @inheritDoc |
||
125 | */ |
||
126 | public function getGroups(array $params = []) |
||
130 | |||
131 | /** |
||
132 | * @inheritDoc |
||
133 | */ |
||
134 | public function getGroup($id, array $params = []) |
||
138 | |||
139 | /** |
||
140 | * @inheritDoc |
||
141 | */ |
||
142 | public function getGroupPrivileges($groupID) |
||
150 | |||
151 | /** |
||
152 | * @inheritDoc |
||
153 | */ |
||
154 | public function getFiles(array $params = []) |
||
158 | |||
159 | /** |
||
160 | * @inheritDoc |
||
161 | */ |
||
162 | public function getFile($id, array $params = []) |
||
166 | |||
167 | /** |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | public function getSettings() |
||
174 | |||
175 | /** |
||
176 | * @inheritDoc |
||
177 | */ |
||
178 | public function getSettingsByCollection($collectionName) |
||
186 | |||
187 | /** |
||
188 | * @inheritDoc |
||
189 | */ |
||
190 | public function getMessages($userId) |
||
197 | |||
198 | /** |
||
199 | * @inheritDoc |
||
200 | */ |
||
201 | View Code Duplication | public function createEntry($tableName, array $data) |
|
216 | |||
217 | /** |
||
218 | * @inheritDoc |
||
219 | */ |
||
220 | View Code Duplication | public function updateEntry($tableName, $id, array $data) |
|
235 | |||
236 | /** |
||
237 | * @inheritDoc |
||
238 | */ |
||
239 | public function deleteEntry($tableName, $ids) |
||
252 | |||
253 | /** |
||
254 | * @inheritDoc |
||
255 | */ |
||
256 | public function createUser(array $data) |
||
260 | |||
261 | /** |
||
262 | * @inheritDoc |
||
263 | */ |
||
264 | public function updateUser($id, array $data) |
||
268 | |||
269 | /** |
||
270 | * @inheritDoc |
||
271 | */ |
||
272 | public function deleteUser($ids) |
||
276 | |||
277 | /** |
||
278 | * @inheritDoc |
||
279 | */ |
||
280 | public function createFile(File $file) |
||
286 | |||
287 | /** |
||
288 | * @inheritDoc |
||
289 | */ |
||
290 | public function updateFile($id, array $data) |
||
294 | |||
295 | /** |
||
296 | * @inheritDoc |
||
297 | */ |
||
298 | public function deleteFile($ids) |
||
302 | |||
303 | public function createPreferences($data) |
||
314 | |||
315 | /** |
||
316 | * @inheritdoc |
||
317 | */ |
||
318 | public function createBookmark($data) |
||
338 | |||
339 | /** |
||
340 | * @inheritdoc |
||
341 | */ |
||
342 | public function createColumn($data) |
||
352 | |||
353 | /** |
||
354 | * @inheritdoc |
||
355 | */ |
||
356 | public function createGroup(array $data) |
||
360 | |||
361 | /** |
||
362 | * @inheritdoc |
||
363 | */ |
||
364 | public function createMessage(array $data) |
||
416 | |||
417 | /** |
||
418 | * @inheritdoc |
||
419 | */ |
||
420 | public function sendMessage(array $data) |
||
424 | |||
425 | public function createPrivileges(array $data) |
||
441 | |||
442 | public function createTable($name, array $data = []) |
||
473 | |||
474 | /** |
||
475 | * Get a table gateway for the given table name |
||
476 | * |
||
477 | * @param $tableName |
||
478 | * |
||
479 | * @return RelationalTableGateway |
||
480 | */ |
||
481 | protected function getTableGateway($tableName) |
||
490 | } |
||
491 |