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 |
||
15 | trait HasColumns |
||
16 | { |
||
17 | /** |
||
18 | * Set columnDefs option value. |
||
19 | * |
||
20 | * @param mixed $value |
||
21 | * @return $this |
||
22 | * @see https://datatables.net/reference/option/columnDefs |
||
23 | */ |
||
24 | View Code Duplication | public function columnDefs($value) |
|
38 | |||
39 | /** |
||
40 | * Add a columnDef option. |
||
41 | * |
||
42 | * @param mixed $value |
||
43 | * @return $this |
||
44 | * @see https://datatables.net/reference/option/columnDefs |
||
45 | */ |
||
46 | View Code Duplication | public function addColumnDef($value) |
|
60 | |||
61 | /** |
||
62 | * Set columns option value. |
||
63 | * |
||
64 | * @param array $columns |
||
65 | * @return $this |
||
66 | * @see https://datatables.net/reference/option/columns |
||
67 | */ |
||
68 | public function columns(array $columns) |
||
98 | |||
99 | /** |
||
100 | * Set title attribute of an array if not set. |
||
101 | * |
||
102 | * @param string $title |
||
103 | * @param array $attributes |
||
104 | * @return array |
||
105 | */ |
||
106 | public function setTitle($title, array $attributes) |
||
114 | |||
115 | /** |
||
116 | * Convert string into a readable title. |
||
117 | * |
||
118 | * @param string $title |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getQualifiedTitle($title) |
||
125 | |||
126 | /** |
||
127 | * Add a column in collection usingsl attributes. |
||
128 | * |
||
129 | * @param array $attributes |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function addColumn(array $attributes) |
||
138 | |||
139 | /** |
||
140 | * Add a Column object at the beginning of collection. |
||
141 | * |
||
142 | * @param \Yajra\DataTables\Html\Column $column |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function addBefore(Column $column) |
||
151 | |||
152 | /** |
||
153 | * Add a column at the beginning of collection using attributes. |
||
154 | * |
||
155 | * @param array $attributes |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function addColumnBefore(array $attributes) |
||
164 | |||
165 | /** |
||
166 | * Add a Column object in collection. |
||
167 | * |
||
168 | * @param \Yajra\DataTables\Html\Column $column |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function add(Column $column) |
||
177 | |||
178 | /** |
||
179 | * Get collection of columns. |
||
180 | * |
||
181 | * @return \Illuminate\Support\Collection |
||
182 | */ |
||
183 | public function getColumns() |
||
187 | |||
188 | /** |
||
189 | * Remove column by name. |
||
190 | * |
||
191 | * @param array $names |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function removeColumn(...$names) |
||
204 | } |
||
205 |
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.