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 |
||
| 29 | class ClientLocal extends AbstractClient |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var BaseTableGateway[] |
||
| 33 | */ |
||
| 34 | protected $tableGateways = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Connection |
||
| 38 | */ |
||
| 39 | protected $connection = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * ClientLocal constructor. |
||
| 43 | * |
||
| 44 | * @param $connection |
||
| 45 | */ |
||
| 46 | public function __construct($connection) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritDoc |
||
| 53 | */ |
||
| 54 | public function getTables(array $params = []) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @inheritDoc |
||
| 61 | */ |
||
| 62 | public function getTable($tableName) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritDoc |
||
| 69 | */ |
||
| 70 | public function getColumns($tableName, array $params = []) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @inheritDoc |
||
| 77 | */ |
||
| 78 | public function getColumn($tableName, $columnName) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @inheritDoc |
||
| 85 | */ |
||
| 86 | public function getEntries($tableName, array $params = []) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @inheritDoc |
||
| 95 | */ |
||
| 96 | public function getEntry($tableName, $id, array $params = []) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @inheritDoc |
||
| 106 | */ |
||
| 107 | public function getUsers(array $params = []) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @inheritDoc |
||
| 115 | */ |
||
| 116 | public function getUser($id, array $params = []) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritDoc |
||
| 123 | */ |
||
| 124 | public function getGroups(array $params = []) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @inheritDoc |
||
| 131 | */ |
||
| 132 | public function getGroup($id, array $params = []) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @inheritDoc |
||
| 139 | */ |
||
| 140 | public function getGroupPrivileges($groupID) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @inheritDoc |
||
| 151 | */ |
||
| 152 | public function getFiles(array $params = []) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @inheritDoc |
||
| 159 | */ |
||
| 160 | public function getFile($id, array $params = []) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritDoc |
||
| 167 | */ |
||
| 168 | public function getSettings() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @inheritDoc |
||
| 175 | */ |
||
| 176 | public function getSettingsByCollection($collectionName) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @inheritDoc |
||
| 187 | */ |
||
| 188 | public function getMessages($userId) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @inheritDoc |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function createEntry($tableName, array $data) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @inheritDoc |
||
| 217 | */ |
||
| 218 | View Code Duplication | public function updateEntry($tableName, $id, array $data) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritDoc |
||
| 236 | */ |
||
| 237 | public function deleteEntry($tableName, $ids) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @inheritDoc |
||
| 253 | */ |
||
| 254 | public function createUser(array $data) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @inheritDoc |
||
| 261 | */ |
||
| 262 | public function updateUser($id, array $data) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @inheritDoc |
||
| 269 | */ |
||
| 270 | public function deleteUser($ids) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritDoc |
||
| 277 | */ |
||
| 278 | public function createFile(File $file) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @inheritDoc |
||
| 287 | */ |
||
| 288 | public function updateFile($id, array $data) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @inheritDoc |
||
| 295 | */ |
||
| 296 | public function deleteFile($ids) |
||
| 300 | |||
| 301 | public function createPreferences($data) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @inheritdoc |
||
| 315 | */ |
||
| 316 | public function createBookmark($data) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @inheritdoc |
||
| 339 | */ |
||
| 340 | public function createColumn($data) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @inheritdoc |
||
| 353 | */ |
||
| 354 | public function createGroup(array $data) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @inheritdoc |
||
| 361 | */ |
||
| 362 | public function createMessage(array $data) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @inheritdoc |
||
| 417 | */ |
||
| 418 | public function sendMessage(array $data) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get a table gateway for the given table name |
||
| 425 | * |
||
| 426 | * @param $tableName |
||
| 427 | * |
||
| 428 | * @return RelationalTableGateway |
||
| 429 | */ |
||
| 430 | protected function getTableGateway($tableName) |
||
| 439 | } |
||
| 440 |