| Total Complexity | 42 |
| Total Lines | 379 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ControllerSetting 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.
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 ControllerSetting, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | trait ControllerSetting |
||
| 18 | { |
||
| 19 | |||
| 20 | private $data = []; |
||
| 21 | |||
| 22 | private function defaultData() { |
||
| 35 | } |
||
| 36 | |||
| 37 | public function style(callable $style) { |
||
| 38 | $this->data['style'] = $style; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function javascript(callable $javascript) { |
||
| 42 | $this->data['javascript'] = $javascript; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param callable $condition |
||
| 47 | */ |
||
| 48 | public function hideButtonDetailWhen(callable $condition) { |
||
| 49 | $this->data['hide_button_detail'] = $condition; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param callable $condition |
||
| 54 | */ |
||
| 55 | public function hideButtonEditWhen(callable $condition) { |
||
| 56 | $this->data['hide_button_edit'] = $condition; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param callable $condition |
||
| 61 | */ |
||
| 62 | public function hideButtonDeleteWhen(callable $condition) { |
||
| 63 | $this->data['hide_button_delete'] = $condition; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param $enable boolean |
||
| 68 | */ |
||
| 69 | public function setButtonLimitPage($enable) { |
||
| 70 | $this->data['button_limit_page'] = $enable; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function setPermalink($path) |
||
| 74 | { |
||
| 75 | $this->data['permalink'] = $path; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function disableSoftDelete() |
||
| 79 | { |
||
| 80 | $this->data['disable_soft_delete'] = true; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param Closure $callbackQuery |
||
| 85 | */ |
||
| 86 | public function hookIndexQuery(Closure $callbackQuery) |
||
| 87 | { |
||
| 88 | $this->data['hook_index_query'] = $callbackQuery; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param Closure $callback |
||
| 93 | */ |
||
| 94 | public function hookAfterInsert(Closure $callback) |
||
| 95 | { |
||
| 96 | $this->data['hook_after_insert'] = $callback; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param Closure $callback |
||
| 101 | */ |
||
| 102 | public function hookBeforeInsert(Closure $callback) |
||
| 103 | { |
||
| 104 | $this->data['hook_before_insert'] = $callback; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param Closure $callback |
||
| 109 | */ |
||
| 110 | public function hookBeforeUpdate(Closure $callback) |
||
| 111 | { |
||
| 112 | $this->data['hook_before_update'] = $callback; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param Closure $callback |
||
| 117 | */ |
||
| 118 | public function hookAfterUpdate(Closure $callback) |
||
| 119 | { |
||
| 120 | $this->data['hook_after_update'] = $callback; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param Closure $callbackQuery |
||
| 125 | */ |
||
| 126 | public function hookSearchQuery(Closure $callbackQuery) |
||
| 127 | { |
||
| 128 | $this->data['hook_search_query'] = $callbackQuery; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $label |
||
| 133 | * @param string $actionURL |
||
| 134 | * @param string $fontAwesome_icon |
||
| 135 | * @param ButtonColor $color |
||
| 136 | * @param string $attributes |
||
| 137 | */ |
||
| 138 | public function addIndexActionButton($label, $actionURL, $fontAwesome_icon=null, $color=null, $attributes = null) |
||
| 139 | { |
||
| 140 | $color = ($color)?:ButtonColor::DARK_BLUE; |
||
| 141 | |||
| 142 | $model = new IndexActionButtonModel(); |
||
| 143 | $model->setLabel($label); |
||
| 144 | $model->setIcon($fontAwesome_icon); |
||
| 145 | $model->setColor($color); |
||
| 146 | $model->setUrl($actionURL); |
||
| 147 | $model->setAttributes($attributes); |
||
| 148 | |||
| 149 | $this->data['index_action_button'][] = $model; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function setOrderBy($field, $sort = 'desc') |
||
| 153 | { |
||
| 154 | $this->data['order_by'] = [$field, $sort]; |
||
| 155 | return $this; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param boolean $enable |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | public function setSearchForm($enable) { |
||
| 163 | $this->data['search_form'] = $enable; |
||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param boolean $button_save |
||
| 169 | * @return ControllerSetting |
||
| 170 | */ |
||
| 171 | public function setButtonSave($button_save) |
||
| 172 | { |
||
| 173 | $this->data['button_save'] = $button_save; |
||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param boolean $button_cancel |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function setButtonCancel($button_cancel) |
||
| 182 | { |
||
| 183 | $this->data['button_cancel'] = $button_cancel; |
||
| 184 | return $this; |
||
| 185 | } |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * @param $label |
||
| 190 | * @param callable|string $url_target |
||
| 191 | * @param callable|string $condition_callback |
||
| 192 | * @param $fontAwesome_icon |
||
| 193 | * @param ButtonColor|string $color |
||
| 194 | */ |
||
| 195 | public function addActionButton($label, $url_target, $condition_callback=null, $fontAwesome_icon=null, $color=null, $confirmation = false) |
||
| 196 | { |
||
| 197 | $new = new AddActionButtonModel(); |
||
| 198 | $new->setLabel($label); |
||
| 199 | $new->setIcon($fontAwesome_icon?:"fa fa-bars"); |
||
| 200 | $new->setColor($color?:"primary"); |
||
| 201 | $new->setUrl($url_target); |
||
| 202 | $new->setCondition($condition_callback); |
||
| 203 | $new->setConfirmation($confirmation); |
||
| 204 | $this->data['add_action_button'][] = $new; |
||
| 205 | } |
||
| 206 | /** |
||
| 207 | * @param mixed $button_edit |
||
| 208 | * @param null $condition_callback |
||
| 209 | * @return ControllerSetting |
||
| 210 | */ |
||
| 211 | public function setButtonEdit($button_edit, $condition_callback = null) |
||
| 212 | { |
||
| 213 | $this->data['button_edit'] = $button_edit; |
||
| 214 | $this->data['button_edit_callback'] = $condition_callback; |
||
| 215 | return $this; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param mixed $button_detail |
||
| 220 | * @param null $condition_callback |
||
| 221 | * @return ControllerSetting |
||
| 222 | */ |
||
| 223 | public function setButtonDetail($button_detail, $condition_callback = null) |
||
| 224 | { |
||
| 225 | $this->data['button_detail'] = $button_detail; |
||
| 226 | $this->data['button_detail_callback'] = $condition_callback; |
||
| 227 | return $this; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param mixed $button_delete |
||
| 232 | * @param null $condition_callback |
||
| 233 | * @return ControllerSetting |
||
| 234 | */ |
||
| 235 | public function setButtonDelete($button_delete, $condition_callback = null) |
||
| 236 | { |
||
| 237 | $this->data['button_delete'] = $button_delete; |
||
| 238 | $this->data['button_delete_callback'] = $condition_callback; |
||
| 239 | return $this; |
||
| 240 | } |
||
| 241 | |||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $alert_message |
||
| 246 | * @return ControllerSetting |
||
| 247 | */ |
||
| 248 | public function setAlertMessage($alert_message) |
||
| 249 | { |
||
| 250 | $this->data['alert_message'] = $alert_message; |
||
| 251 | return $this; |
||
| 252 | } |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $html_or_view |
||
| 257 | * @return ControllerSetting |
||
| 258 | */ |
||
| 259 | public function setBeforeIndexTable($html_or_view) |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param mixed $html_or_view |
||
| 267 | * @return ControllerSetting |
||
| 268 | */ |
||
| 269 | public function setAfterIndexTable($html_or_view) |
||
| 270 | { |
||
| 271 | $this->data['after_index_table'] = $html_or_view; |
||
| 272 | return $this; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param callable|string $html_or_view |
||
| 277 | * @return ControllerSetting |
||
| 278 | */ |
||
| 279 | public function setBeforeDetailForm($html_or_view) |
||
| 280 | { |
||
| 281 | $this->data['before_detail_form'] = $html_or_view; |
||
| 282 | return $this; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param callable|string $html_or_view |
||
| 287 | * @return ControllerSetting |
||
| 288 | */ |
||
| 289 | public function setAfterDetailForm($html_or_view) |
||
| 290 | { |
||
| 291 | $this->data['after_detail_form'] = $html_or_view; |
||
| 292 | return $this; |
||
| 293 | } |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * @param mixed $page_title |
||
| 298 | * @return ControllerSetting |
||
| 299 | */ |
||
| 300 | public function setPageTitle($page_title) |
||
| 301 | { |
||
| 302 | $this->data['page_title'] = $page_title; |
||
| 303 | return $this; |
||
| 304 | } |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * @param mixed $icon |
||
| 309 | * @return ControllerSetting |
||
| 310 | */ |
||
| 311 | public function setPageIcon($icon = "fa fa-table") |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param mixed $table |
||
| 319 | * @return ControllerSetting |
||
| 320 | */ |
||
| 321 | public function setTable($table) |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param int $limit |
||
| 329 | * @return ControllerSetting |
||
| 330 | */ |
||
| 331 | public function setLimit($limit) |
||
| 332 | { |
||
| 333 | $this->data['limit'] = $limit; |
||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param bool $button_filter |
||
| 339 | * @return ControllerSetting |
||
| 340 | */ |
||
| 341 | public function setButtonFilter($button_filter) |
||
| 342 | { |
||
| 343 | $this->data['button_filter'] = $button_filter; |
||
| 344 | return $this; |
||
| 345 | } |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * @param bool $button_add_more |
||
| 350 | * @return ControllerSetting |
||
| 351 | */ |
||
| 352 | public function setButtonAddMore($button_add_more) |
||
| 353 | { |
||
| 354 | $this->data['button_add_more'] = $button_add_more; |
||
| 355 | return $this; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param bool $button_add |
||
| 360 | * @return ControllerSetting |
||
| 361 | */ |
||
| 362 | public function setButtonAdd($button_add) |
||
| 363 | { |
||
| 364 | $this->data['button_add'] = $button_add; |
||
| 365 | return $this; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $head_html |
||
| 370 | * @return ControllerSetting |
||
| 371 | */ |
||
| 372 | public function setHeadHtml($head_html) |
||
| 373 | { |
||
| 374 | $this->data['head_html'] = $head_html; |
||
| 375 | return $this; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $bottom_html |
||
| 380 | * @return ControllerSetting |
||
| 381 | */ |
||
| 382 | public function setBottomHtml($bottom_html) |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param SidebarStyle|string $sidebar_style |
||
| 390 | * @return ControllerSetting |
||
| 391 | */ |
||
| 392 | public function setSidebarStyle($sidebar_style) |
||
| 393 | { |
||
| 394 | $this->data['sidebar_style'] = $sidebar_style; |
||
| 395 | return $this; |
||
| 396 | } |
||
| 397 | |||
| 398 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.