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 DatabaseDriver 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 DatabaseDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class DatabaseDriver implements DriverInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var QueryBuilder |
||
| 29 | */ |
||
| 30 | private $connection; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Container |
||
| 34 | */ |
||
| 35 | private $container; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Sets the database connection. |
||
| 39 | * |
||
| 40 | * @param QueryBuilder $db |
||
| 41 | * |
||
| 42 | * @return self |
||
| 43 | */ |
||
| 44 | public function setConnection(QueryBuilder $db) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Gets the database connection. |
||
| 53 | * |
||
| 54 | * @throws DriverException when the connection has not been set yet |
||
| 55 | * |
||
| 56 | * @return QueryBuilder |
||
| 57 | */ |
||
| 58 | public function getConnection() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @deprecated |
||
| 73 | * |
||
| 74 | * Sets the DI container |
||
| 75 | * |
||
| 76 | * @param Container $container |
||
| 77 | * |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | public function setContainer(Container $container) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @deprecated |
||
| 89 | * |
||
| 90 | * Gets the DI container |
||
| 91 | * |
||
| 92 | * @return Container |
||
| 93 | */ |
||
| 94 | public function getContainer() |
||
| 98 | |||
| 99 | View Code Duplication | public function createModel(Model $model, array $parameters) |
|
| 115 | |||
| 116 | public function getCreatedID(Model $model, $propertyName) |
||
| 128 | |||
| 129 | View Code Duplication | public function loadModel(Model $model) |
|
| 151 | |||
| 152 | View Code Duplication | public function updateModel(Model $model, array $parameters) |
|
| 173 | |||
| 174 | View Code Duplication | public function deleteModel(Model $model) |
|
| 189 | |||
| 190 | public function queryModels(Query $query) |
||
| 230 | |||
| 231 | public function totalRecords(Query $query) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Marshals a value to storage. |
||
| 252 | * |
||
| 253 | * @param mixed $value |
||
| 254 | * |
||
| 255 | * @return mixed serialized value |
||
| 256 | */ |
||
| 257 | public function serializeValue($value) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Serializes an array of values. |
||
| 269 | * |
||
| 270 | * @param array $values |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | private function serialize(array $values) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Unserializes an array of values. |
||
| 285 | * |
||
| 286 | * @param array $values |
||
| 287 | * @param array $properties model properties |
||
| 288 | * |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | private function unserialize(array $values, array $properties) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Returns a prefixed select statement. |
||
| 304 | * |
||
| 305 | * @param string $columns |
||
| 306 | * @param string $tablename |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | private function prefixSelect($columns, $tablename) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns a prefixed where statement. |
||
| 322 | * |
||
| 323 | * @param string $columns |
||
| 324 | * @param string $tablename |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | private function prefixWhere(array $where, $tablename) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Returns a prefixed sort statement. |
||
| 350 | * |
||
| 351 | * @param string $columns |
||
| 352 | * @param string $tablename |
||
| 353 | * |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | private function prefixSort(array $sort, $tablename) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Prefix columns with tablename that contains only |
||
| 367 | * alphanumeric/underscores/*. |
||
| 368 | * |
||
| 369 | * @param string $column |
||
| 370 | * @param string $tablename |
||
| 371 | * |
||
| 372 | * @return string prefixed column |
||
| 373 | */ |
||
| 374 | private function prefixColumn($column, $tablename) |
||
| 382 | } |
||
| 383 |
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.