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 |
||
7 | View Code Duplication | class Note extends BaseModel |
|
|
|||
8 | { |
||
9 | use QueryBuilderTrait; |
||
10 | |||
11 | protected $endpoint; |
||
12 | |||
13 | /** |
||
14 | * Retrieve all Items. |
||
15 | * |
||
16 | * @param int $order_id |
||
17 | * @param array $options |
||
18 | * |
||
19 | * @return array |
||
20 | */ |
||
21 | protected function all($order_id, $options = []) |
||
27 | |||
28 | /** |
||
29 | * Retrieve single Item. |
||
30 | * |
||
31 | * @param int $order_id |
||
32 | * @param int $note_id |
||
33 | * @param array $options |
||
34 | * |
||
35 | * @return object |
||
36 | */ |
||
37 | protected function find($order_id, $note_id, $options = []) |
||
43 | |||
44 | /** |
||
45 | * Create new Item. |
||
46 | * |
||
47 | * @param int $order_id |
||
48 | * @param array $data |
||
49 | * |
||
50 | * @return object |
||
51 | */ |
||
52 | protected function create($order_id, $data) |
||
58 | |||
59 | /** |
||
60 | * Destroy Item. |
||
61 | * |
||
62 | * @param int $order_id |
||
63 | * @param int $note_id |
||
64 | * @param array $options |
||
65 | * |
||
66 | * @return object |
||
67 | */ |
||
68 | protected function delete($order_id, $note_id, $options = []) |
||
74 | |||
75 | /** |
||
76 | * Paginate results. |
||
77 | * |
||
78 | * @param int $per_page |
||
79 | * @param int $current_page |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function paginate( |
||
93 | |||
94 | /** |
||
95 | * Count all results. |
||
96 | * |
||
97 | * @return int |
||
98 | */ |
||
99 | protected function count($order_id) |
||
105 | |||
106 | /** |
||
107 | * Store data. |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public function save($order_id) |
||
117 | } |
||
118 |
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.