Complex classes like EloquentDatatable 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 EloquentDatatable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class EloquentDatatable |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Eloquent model. |
||
| 17 | * |
||
| 18 | * @var \Illuminate\Database\Eloquent\Model |
||
| 19 | */ |
||
| 20 | protected $model; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Eloquent model underlying table. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $table = ''; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Datatable columns. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $colomns; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Form implementation. |
||
| 38 | * |
||
| 39 | * @var \Kris\LaravelFormBuilder\Form|null |
||
| 40 | */ |
||
| 41 | protected $form = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Datatable columns to display. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $colomnsDisplay = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Datatable columns orderable state. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $columnsOrderable = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Datatable extra options. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $datatableOptions = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Datatable order data (0 can be an integer to represents the column's number or it can be a string that references the column's name). |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $defaultOrder = [[0, 'desc']]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * EloquentDatatable constructor. |
||
| 73 | * |
||
| 74 | * @param \Illuminate\Database\Eloquent\Model|null $model |
||
| 75 | */ |
||
| 76 | 8 | public function __construct(Model $model = null) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Model setter. |
||
| 85 | * |
||
| 86 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | 4 | public function setModel(Model $model) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Add column to datatable. |
||
| 97 | * |
||
| 98 | * @param string $name |
||
| 99 | * @param \Closure|null $closure |
||
| 100 | * @param string|\Symfony\Component\Translation\TranslatorInterface $translation |
||
| 101 | * @param bool $orderable |
||
| 102 | * @return $this |
||
| 103 | */ |
||
| 104 | 8 | public function add($name, $closure = null, $translation = '', $orderable = true) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Add translation column. |
||
| 123 | * |
||
| 124 | * @param string $name |
||
| 125 | * @param string|\Symfony\Component\Translation\TranslatorInterface $translation |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | 8 | public function addTranslation($name, $translation) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Add orderable column. |
||
| 135 | * |
||
| 136 | * @param string $name |
||
| 137 | * @param bool $translation |
||
|
|
|||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | 8 | public function addOrderable($name, $orderable) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Generate specified columns for current datatable. |
||
| 147 | * |
||
| 148 | * @return mixed |
||
| 149 | */ |
||
| 150 | 4 | public function generateColomns() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Return Datatable base query. |
||
| 184 | * |
||
| 185 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 186 | */ |
||
| 187 | 4 | protected function baseQuery() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Set DT_RowClass for given datatable. |
||
| 194 | * |
||
| 195 | * @param \Chumper\Datatable\Engines\QueryEngine $datatable |
||
| 196 | * @return \Chumper\Datatable\Engines\QueryEngine |
||
| 197 | */ |
||
| 198 | 4 | public function setClassRow($datatable) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Generate rendered HTML view for current datatable. |
||
| 209 | * |
||
| 210 | * @param string $template |
||
| 211 | * @param string $route |
||
| 212 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 213 | */ |
||
| 214 | 8 | public function generateHtmlRender($template = 'datatable-builder::part.datatable', $route = '') |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Add default actions to datatable. |
||
| 227 | * |
||
| 228 | * @param string $template |
||
| 229 | * @param string $route |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | 8 | public function addDefaultAction($template = 'datatable-builder::form.components.datatable.actions', $route = '') |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Add filters form to current datatable. |
||
| 246 | * |
||
| 247 | * @param string $template |
||
| 248 | * @return string |
||
| 249 | * @throws \Exception |
||
| 250 | * @throws \Throwable |
||
| 251 | */ |
||
| 252 | 8 | protected function addFilter($template = 'datatable-builder::form.components.datatable.filter') |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Add default options to current datatable. |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | 8 | protected function addOptions() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Get controller name for action based on current route. |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | 8 | protected function getControllerNameForAction() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Add the fields filters form here. |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | public function filters() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Apply filters by default on each fields of setted model. |
||
| 331 | * |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | 4 | public function applyFilters() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Compile all added columns and build datatable. |
||
| 348 | * |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | abstract public function build(); |
||
| 352 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.