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 |
||
| 26 | class DatabaseDriver implements DriverInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var Container |
||
| 30 | */ |
||
| 31 | private $app; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param Container $app |
||
| 35 | */ |
||
| 36 | public function __construct(Container $app = null) |
||
| 40 | |||
| 41 | View Code Duplication | public function createModel(Model $model, array $parameters) |
|
| 57 | |||
| 58 | public function getCreatedID(Model $model, $propertyName) |
||
| 70 | |||
| 71 | View Code Duplication | public function loadModel(Model $model) |
|
| 93 | |||
| 94 | View Code Duplication | public function updateModel(Model $model, array $parameters) |
|
| 115 | |||
| 116 | View Code Duplication | public function deleteModel(Model $model) |
|
| 131 | |||
| 132 | public function queryModels(Query $query) |
||
| 170 | |||
| 171 | View Code Duplication | public function totalRecords(Query $query) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Generates the tablename for the model. |
||
| 191 | * |
||
| 192 | * @param string|Model $model |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getTablename($model) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Marshals a value to storage. |
||
| 205 | * |
||
| 206 | * @param mixed $value |
||
| 207 | * |
||
| 208 | * @return mixed serialized value |
||
| 209 | */ |
||
| 210 | public function serializeValue($value) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Marshals a value for a given property from storage. |
||
| 222 | * |
||
| 223 | * @param array $property |
||
| 224 | * @param mixed $value |
||
| 225 | * |
||
| 226 | * @return mixed unserialized value |
||
| 227 | */ |
||
| 228 | public function unserializeValue(array $property, $value) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Serializes an array of values. |
||
| 251 | * |
||
| 252 | * @param array $values |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | private function serialize(array $values) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Unserializes an array of values. |
||
| 267 | * |
||
| 268 | * @param array $values |
||
| 269 | * @param array $properties model properties |
||
| 270 | * |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | private function unserialize(array $values, array $properties) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns a prefixed select statement. |
||
| 286 | * |
||
| 287 | * @param string $columns |
||
| 288 | * @param string $tablename |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | private function prefixSelect($columns, $tablename) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Returns a prefixed where statement. |
||
| 304 | * |
||
| 305 | * @param string $columns |
||
| 306 | * @param string $tablename |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | private function prefixWhere(array $where, $tablename) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns a prefixed sort statement. |
||
| 332 | * |
||
| 333 | * @param string $columns |
||
| 334 | * @param string $tablename |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | private function prefixSort(array $sort, $tablename) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Prefix columns with tablename that contains only |
||
| 349 | * alphanumeric/underscores/*. |
||
| 350 | * |
||
| 351 | * @param string $column |
||
| 352 | * @param string $tablename |
||
| 353 | * |
||
| 354 | * @return string prefixed column |
||
| 355 | */ |
||
| 356 | private function prefixColumn($column, $tablename) |
||
| 364 | } |
||
| 365 |
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.