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 |
||
| 33 | class ClientLocal extends AbstractClient |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var BaseTableGateway[] |
||
| 37 | */ |
||
| 38 | protected $tableGateways = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Connection |
||
| 42 | */ |
||
| 43 | protected $connection = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * ClientLocal constructor. |
||
| 47 | * |
||
| 48 | * @param $connection |
||
| 49 | */ |
||
| 50 | public function __construct($connection) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @inheritDoc |
||
| 57 | */ |
||
| 58 | public function getTables(array $params = []) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritDoc |
||
| 65 | */ |
||
| 66 | public function getTable($tableName) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @inheritDoc |
||
| 73 | */ |
||
| 74 | public function getColumns($tableName, array $params = []) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @inheritDoc |
||
| 81 | */ |
||
| 82 | public function getColumn($tableName, $columnName) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritDoc |
||
| 89 | */ |
||
| 90 | public function getItems($tableName, array $params = []) |
||
| 91 | { |
||
| 92 | $tableGateway = $this->getTableGateway($tableName); |
||
| 93 | |||
| 94 | return $this->createResponseFromData($tableGateway->getItems($params)); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @inheritDoc |
||
| 99 | */ |
||
| 100 | public function getItem($tableName, $id, array $params = []) |
||
| 101 | { |
||
| 102 | // @TODO: Dynamic ID |
||
| 103 | return $this->getItems($tableName, array_merge($params, [ |
||
| 104 | 'id' => $id |
||
| 105 | ])); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @inheritDoc |
||
| 110 | */ |
||
| 111 | public function getUsers(array $params = []) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @inheritDoc |
||
| 119 | */ |
||
| 120 | public function getUser($id, array $params = []) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @inheritDoc |
||
| 127 | */ |
||
| 128 | public function getGroups(array $params = []) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @inheritDoc |
||
| 135 | */ |
||
| 136 | public function getGroup($id, array $params = []) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritDoc |
||
| 143 | */ |
||
| 144 | public function getGroupPrivileges($groupID) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @inheritDoc |
||
| 155 | */ |
||
| 156 | public function getFiles(array $params = []) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @inheritDoc |
||
| 163 | */ |
||
| 164 | public function getFile($id, array $params = []) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @inheritDoc |
||
| 171 | */ |
||
| 172 | public function getSettings() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @inheritDoc |
||
| 179 | */ |
||
| 180 | public function getSettingsByCollection($collectionName) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @inheritDoc |
||
| 191 | */ |
||
| 192 | public function getMessages($userId) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @inheritDoc |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function createItem($tableName, array $data) |
|
| 206 | { |
||
| 207 | $tableGateway = $this->getTableGateway($tableName); |
||
| 208 | $data = $this->processData($tableName, $data); |
||
| 209 | |||
| 210 | foreach($data as $key => $value) { |
||
| 211 | if ($value instanceof File) { |
||
| 212 | $data[$key] = $this->processFile($value); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | $newRecord = $tableGateway->manageRecordUpdate($tableName, $data); |
||
| 217 | |||
| 218 | return $this->getItem($tableName, $newRecord[$tableGateway->primaryKeyFieldName]); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @inheritDoc |
||
| 223 | */ |
||
| 224 | View Code Duplication | public function updateItem($tableName, $id, array $data) |
|
| 225 | { |
||
| 226 | $tableGateway = $this->getTableGateway($tableName); |
||
| 227 | $data = $this->processData($tableName, $data); |
||
| 228 | |||
| 229 | foreach($data as $key => $value) { |
||
| 230 | if ($value instanceof File) { |
||
| 231 | $data[$key] = $this->processFile($value); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | $updatedRecord = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id])); |
||
| 236 | |||
| 237 | return $this->getItem($tableName, $updatedRecord[$tableGateway->primaryKeyFieldName]); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @inheritDoc |
||
| 242 | */ |
||
| 243 | public function deleteItem($tableName, $ids) |
||
| 244 | { |
||
| 245 | // @TODO: Accept EntryCollection and Entry |
||
| 246 | $tableGateway = $this->getTableGateway($tableName); |
||
| 247 | |||
| 248 | if (!is_array($ids)) { |
||
| 249 | $ids = [$ids]; |
||
| 250 | } |
||
| 251 | |||
| 252 | return $tableGateway->delete(function($delete) use ($ids) { |
||
| 253 | return $delete->where->in('id', $ids); |
||
| 254 | }); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @inheritDoc |
||
| 259 | */ |
||
| 260 | public function createUser(array $data) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @inheritDoc |
||
| 267 | */ |
||
| 268 | public function updateUser($id, array $data) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @inheritDoc |
||
| 275 | */ |
||
| 276 | public function deleteUser($ids) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @inheritDoc |
||
| 283 | */ |
||
| 284 | public function createFile(File $file) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @inheritDoc |
||
| 293 | */ |
||
| 294 | public function updateFile($id, $data) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @inheritDoc |
||
| 305 | */ |
||
| 306 | public function deleteFile($ids) |
||
| 310 | |||
| 311 | public function createPreferences($data) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @inheritdoc |
||
| 325 | */ |
||
| 326 | public function createBookmark($data) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @inheritdoc |
||
| 349 | */ |
||
| 350 | public function getBookmark($id) |
||
| 351 | { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @inheritdoc |
||
| 357 | */ |
||
| 358 | public function getBookmarks($userId = null) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @inheritdoc |
||
| 372 | */ |
||
| 373 | public function createColumn($data) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @inheritdoc |
||
| 386 | */ |
||
| 387 | public function createGroup(array $data) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @inheritdoc |
||
| 394 | */ |
||
| 395 | public function createMessage(array $data) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @inheritdoc |
||
| 450 | */ |
||
| 451 | public function sendMessage(array $data) |
||
| 455 | |||
| 456 | View Code Duplication | public function createPrivileges(array $data) |
|
| 472 | |||
| 473 | public function createTable($name, array $data = []) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @inheritdoc |
||
| 507 | */ |
||
| 508 | public function createColumnUIOptions(array $data) |
||
| 555 | |||
| 556 | View Code Duplication | public function getPreferences($table, $user) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * @inheritdoc |
||
| 575 | */ |
||
| 576 | public function deleteBookmark($id) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @inheritdoc |
||
| 583 | */ |
||
| 584 | View Code Duplication | public function deleteColumn($name, $table) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * @inheritdoc |
||
| 604 | */ |
||
| 605 | public function deleteGroup($id) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @inheritdoc |
||
| 612 | */ |
||
| 613 | View Code Duplication | public function deleteTable($name) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Get a table gateway for the given table name |
||
| 633 | * |
||
| 634 | * @param $tableName |
||
| 635 | * |
||
| 636 | * @return RelationalTableGateway |
||
| 637 | */ |
||
| 638 | protected function getTableGateway($tableName) |
||
| 647 | } |
||
| 648 |