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:
| 1 | <?php |
||
| 11 | View Code Duplication | class BricksStorage extends AbstractStorage |
|
|
|
|||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @return array |
||
| 15 | */ |
||
| 16 | public function getBricks() |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Add a brick |
||
| 23 | * |
||
| 24 | * @param $postValues |
||
| 25 | * |
||
| 26 | * @throws \Exception |
||
| 27 | */ |
||
| 28 | public function addBrick($postValues) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Get a brick by its slug |
||
| 41 | * |
||
| 42 | * @param $slug |
||
| 43 | * |
||
| 44 | * @return \stdClass |
||
| 45 | */ |
||
| 46 | public function getBrickBySlug($slug) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Save changes to a brick |
||
| 60 | * |
||
| 61 | * @param $slug |
||
| 62 | * @param $postValues |
||
| 63 | * |
||
| 64 | * @throws \Exception |
||
| 65 | */ |
||
| 66 | public function saveBrick($slug, $postValues) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Delete a brick by its slug |
||
| 82 | * |
||
| 83 | * @param $slug |
||
| 84 | * |
||
| 85 | * @throws \Exception |
||
| 86 | */ |
||
| 87 | public function deleteBrickBySlug($slug) |
||
| 100 | } |
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.