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 |
||
8 | class WidgetGroup |
||
9 | { |
||
10 | use ViewExpressionTrait; |
||
11 | |||
12 | /** |
||
13 | * The widget group name. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name; |
||
18 | |||
19 | /** |
||
20 | * The application wrapper. |
||
21 | * |
||
22 | * @var ApplicationWrapperContract |
||
23 | */ |
||
24 | protected $app; |
||
25 | |||
26 | /** |
||
27 | * The array of widgets to display in this group. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $widgets = []; |
||
32 | |||
33 | /** |
||
34 | * The position of a widget in this group. |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $position = 100; |
||
39 | |||
40 | /** |
||
41 | * The separator to display between widgets in the group. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $separator = ''; |
||
46 | |||
47 | /** |
||
48 | * Id that is going to be issued to the next widget when it's added to the group. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $nextWidgetId = 1; |
||
53 | |||
54 | /** |
||
55 | * A callback that defines extra markup that wraps every widget in the group. |
||
56 | * |
||
57 | * @var callable |
||
58 | */ |
||
59 | protected $wrapCallback; |
||
60 | |||
61 | /** |
||
62 | * @param $name |
||
63 | * @param ApplicationWrapperContract $app |
||
64 | */ |
||
65 | public function __construct($name, ApplicationWrapperContract $app) |
||
71 | |||
72 | /** |
||
73 | * Display all widgets from this group in correct order. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function display() |
||
97 | |||
98 | /** |
||
99 | * Remove a widget by its id. |
||
100 | * |
||
101 | * @param int $id |
||
102 | */ |
||
103 | View Code Duplication | public function removeById($id) |
|
115 | |||
116 | /** |
||
117 | * Remove all widgets with $name from the group. |
||
118 | * |
||
119 | * @param string $name |
||
120 | */ |
||
121 | View Code Duplication | public function removeByName($name) |
|
131 | |||
132 | /** |
||
133 | * Remove all widgets from $position from the group. |
||
134 | * |
||
135 | * @param int|string $position |
||
136 | */ |
||
137 | public function removeByPosition($position) |
||
143 | |||
144 | /** |
||
145 | * Remove all widgets from the group. |
||
146 | */ |
||
147 | public function removeAll() |
||
151 | |||
152 | /** |
||
153 | * Set widget position. |
||
154 | * |
||
155 | * @param int $position |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function position($position) |
||
165 | |||
166 | /** |
||
167 | * Add a widget to the group. |
||
168 | */ |
||
169 | public function addWidget() |
||
173 | |||
174 | /** |
||
175 | * Add an async widget to the group. |
||
176 | */ |
||
177 | public function addAsyncWidget() |
||
181 | |||
182 | /** |
||
183 | * Getter for position. |
||
184 | * |
||
185 | * @return int |
||
186 | */ |
||
187 | public function getPosition() |
||
191 | |||
192 | /** |
||
193 | * Set a separator to display between widgets in the group. |
||
194 | * |
||
195 | * @param string $separator |
||
196 | * |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function setSeparator($separator) |
||
205 | |||
206 | /** |
||
207 | * Setter for $this->wrapCallback. |
||
208 | * |
||
209 | * @param callable $callable |
||
210 | * |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function wrap(callable $callable) |
||
219 | |||
220 | /** |
||
221 | * Check if there are any widgets in the group. |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function any() |
||
229 | |||
230 | /** |
||
231 | * Check if there are no widgets in the group. |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function isEmpty() |
||
239 | |||
240 | /** |
||
241 | * Count the number of widgets in this group. |
||
242 | * |
||
243 | * @return int |
||
244 | */ |
||
245 | public function count() |
||
254 | |||
255 | /** |
||
256 | * Add a widget with a given type to the array. |
||
257 | * |
||
258 | * @param string $type |
||
259 | * @param array $arguments |
||
260 | * |
||
261 | * @return int |
||
262 | */ |
||
263 | protected function addWidgetWithType($type, array $arguments = []) |
||
281 | |||
282 | /** |
||
283 | * Display a widget according to its type. |
||
284 | * |
||
285 | * @param $widget |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | protected function displayWidget($widget) |
||
295 | |||
296 | /** |
||
297 | * Reset the position property back to the default. |
||
298 | * So it does not affect the next widget. |
||
299 | */ |
||
300 | protected function resetPosition() |
||
304 | |||
305 | /** |
||
306 | * Wraps widget content in a special markup defined by $this->wrap(). |
||
307 | * |
||
308 | * @param string $content |
||
309 | * @param int $index |
||
310 | * @param int $total |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | protected function performWrap($content, $index, $total) |
||
324 | } |
||
325 |