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 |
||
| 30 | class ClientLocal extends AbstractClient |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var BaseTableGateway[] |
||
| 34 | */ |
||
| 35 | protected $tableGateways = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Connection |
||
| 39 | */ |
||
| 40 | protected $connection = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * ClientLocal constructor. |
||
| 44 | * |
||
| 45 | * @param $connection |
||
| 46 | */ |
||
| 47 | public function __construct($connection) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritDoc |
||
| 54 | */ |
||
| 55 | public function getTables(array $params = []) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritDoc |
||
| 62 | */ |
||
| 63 | public function getTable($tableName) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @inheritDoc |
||
| 70 | */ |
||
| 71 | public function getColumns($tableName, array $params = []) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @inheritDoc |
||
| 78 | */ |
||
| 79 | public function getColumn($tableName, $columnName) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @inheritDoc |
||
| 86 | */ |
||
| 87 | public function getEntries($tableName, array $params = []) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @inheritDoc |
||
| 96 | */ |
||
| 97 | public function getEntry($tableName, $id, array $params = []) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @inheritDoc |
||
| 107 | */ |
||
| 108 | public function getUsers(array $params = []) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @inheritDoc |
||
| 116 | */ |
||
| 117 | public function getUser($id, array $params = []) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @inheritDoc |
||
| 124 | */ |
||
| 125 | public function getGroups(array $params = []) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @inheritDoc |
||
| 132 | */ |
||
| 133 | public function getGroup($id, array $params = []) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @inheritDoc |
||
| 140 | */ |
||
| 141 | public function getGroupPrivileges($groupID) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @inheritDoc |
||
| 152 | */ |
||
| 153 | public function getFiles(array $params = []) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @inheritDoc |
||
| 160 | */ |
||
| 161 | public function getFile($id, array $params = []) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @inheritDoc |
||
| 168 | */ |
||
| 169 | public function getSettings() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @inheritDoc |
||
| 176 | */ |
||
| 177 | public function getSettingsByCollection($collectionName) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @inheritDoc |
||
| 188 | */ |
||
| 189 | public function getMessages($userId) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritDoc |
||
| 199 | */ |
||
| 200 | View Code Duplication | public function createEntry($tableName, array $data) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @inheritDoc |
||
| 218 | */ |
||
| 219 | View Code Duplication | public function updateEntry($tableName, $id, array $data) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @inheritDoc |
||
| 237 | */ |
||
| 238 | public function deleteEntry($tableName, $ids) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @inheritDoc |
||
| 254 | */ |
||
| 255 | public function createUser(array $data) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @inheritDoc |
||
| 262 | */ |
||
| 263 | public function updateUser($id, array $data) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @inheritDoc |
||
| 270 | */ |
||
| 271 | public function deleteUser($ids) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @inheritDoc |
||
| 278 | */ |
||
| 279 | public function createFile(File $file) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @inheritDoc |
||
| 288 | */ |
||
| 289 | public function updateFile($id, array $data) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @inheritDoc |
||
| 296 | */ |
||
| 297 | public function deleteFile($ids) |
||
| 301 | |||
| 302 | public function createPreferences($data) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @inheritdoc |
||
| 316 | */ |
||
| 317 | public function createBookmark($data) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @inheritdoc |
||
| 340 | */ |
||
| 341 | public function createColumn($data) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @inheritdoc |
||
| 354 | */ |
||
| 355 | public function createGroup(array $data) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @inheritdoc |
||
| 362 | */ |
||
| 363 | public function createMessage(array $data) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @inheritdoc |
||
| 418 | */ |
||
| 419 | public function sendMessage(array $data) |
||
| 423 | |||
| 424 | public function createPrivileges(array $data) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Get a table gateway for the given table name |
||
| 443 | * |
||
| 444 | * @param $tableName |
||
| 445 | * |
||
| 446 | * @return RelationalTableGateway |
||
| 447 | */ |
||
| 448 | protected function getTableGateway($tableName) |
||
| 457 | } |
||
| 458 |