Complex classes like ModelAdmin 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 ModelAdmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class ModelAdmin extends Admin implements ModelQueryable |
||
14 | { |
||
15 | use ModelQuerying; |
||
16 | use ModelSaving; |
||
17 | |||
18 | /** |
||
19 | * Class of Model to Manage. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $managedModel; |
||
24 | |||
25 | /** |
||
26 | * ModelAdmin Icon. |
||
27 | * |
||
28 | * Font Awesome Defined Icon, eg 'user' = 'fa-user' |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $icon = ''; |
||
33 | |||
34 | /** |
||
35 | * Validation Rules for onCreate, onEdit actions. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $rules = []; |
||
40 | |||
41 | /** |
||
42 | * Columns for Model. |
||
43 | * |
||
44 | * Defines which fields to show in the listing tables output. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $columns = []; |
||
49 | |||
50 | /** |
||
51 | * Map Model Attributes to AttributeTypes with |
||
52 | * additional parameters which will be output |
||
53 | * as fields when viewing, editting or adding |
||
54 | * a new model entry. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $fields = []; |
||
59 | |||
60 | /** |
||
61 | * Columns for Model are Sortable. |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $sortable = true; |
||
66 | |||
67 | /** |
||
68 | * The Controller to be used by the Model Admin. |
||
69 | * |
||
70 | * This defaults to parent::getController() |
||
71 | * if it has been left undefined. |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $controller = '\LaravelFlare\Flare\Admin\Models\ModelAdminController'; |
||
76 | |||
77 | /** |
||
78 | * The Policy used for the Model Authorization logic. |
||
79 | * |
||
80 | * This class should implement the ModelAdminPoliceable which |
||
81 | * includes authorization checks for the create, view, edit and delete actions. |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $policy = '\LaravelFlare\Flare\Permissions\ModelAdminPolicy'; |
||
86 | |||
87 | /** |
||
88 | * The current model to be managed. |
||
89 | * |
||
90 | * @var Model |
||
91 | */ |
||
92 | public $model; |
||
93 | |||
94 | /** |
||
95 | * __construct. |
||
96 | */ |
||
97 | public function __construct() |
||
105 | |||
106 | /** |
||
107 | * Returns a Model Instance. |
||
108 | * |
||
109 | * @return Model |
||
110 | */ |
||
111 | public function model() |
||
121 | |||
122 | /** |
||
123 | * Returns a New Model Instance. |
||
124 | * |
||
125 | * @return Model |
||
126 | */ |
||
127 | public function newModel() |
||
133 | |||
134 | /** |
||
135 | * Returns the Managed Model Class. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getManagedModel() |
||
147 | |||
148 | /** |
||
149 | * Set the Managed Model Class. |
||
150 | * |
||
151 | * @param string $managedModel |
||
152 | */ |
||
153 | public function setManagedModel($managedModel = null) |
||
157 | |||
158 | /** |
||
159 | * Returns the Route Paramets. |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function routeParameters() |
||
169 | |||
170 | /** |
||
171 | * Formats and returns the Columns. |
||
172 | * |
||
173 | * This is really gross, I'm removing it soon. |
||
174 | * |
||
175 | * @return |
||
176 | */ |
||
177 | public function getColumns() |
||
218 | |||
219 | /** |
||
220 | * Gets an Attribute by the provided key |
||
221 | * on either the current model or a provided model instance. |
||
222 | * |
||
223 | * @param string $key |
||
224 | * @param mixed $model |
||
225 | * |
||
226 | * @return mixed |
||
227 | */ |
||
228 | public function getAttribute($key, $model = false) |
||
246 | |||
247 | /** |
||
248 | * Determine if a get accessor exists for an attribute. |
||
249 | * |
||
250 | * @param string $key |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function hasGetAccessor($key) |
||
258 | |||
259 | /** |
||
260 | * Determines if a key resolved a related Model. |
||
261 | * |
||
262 | * @param string $key |
||
263 | * @param mixed $model |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function hasRelatedKey($key, $model = false) |
||
282 | |||
283 | /** |
||
284 | * Resolves a relation based on the key provided, |
||
285 | * either on the current model or a provided model instance. |
||
286 | * |
||
287 | * @param string $key |
||
288 | * @param mixed $model |
||
289 | * |
||
290 | * @return mixed |
||
291 | */ |
||
292 | public function relatedKey($key, $model = false) |
||
315 | |||
316 | /** |
||
317 | * Set a given attribute on the model. |
||
318 | * |
||
319 | * @param string $key |
||
320 | * @param mixed $value |
||
321 | */ |
||
322 | public function setAttribute($key, $value) |
||
332 | |||
333 | /** |
||
334 | * Determine if a set mutator exists for an attribute. |
||
335 | * |
||
336 | * @param string $key |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | public function hasSetMutator($key) |
||
344 | |||
345 | /** |
||
346 | * Determine if a get mutator exists for an attribute. |
||
347 | * |
||
348 | * @param string $key |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | public function hasGetMutator($key) |
||
356 | |||
357 | /** |
||
358 | * Returns an array of Attribute Fields ready for output. |
||
359 | * |
||
360 | * @param string $type |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | public function outputFields($type = 'view') |
||
368 | |||
369 | /** |
||
370 | * Gets the Managed Model Mapping. |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | public function getFields() |
||
380 | |||
381 | /** |
||
382 | * Sets the Managed Model Mapping. |
||
383 | * |
||
384 | * @param array $fields |
||
385 | */ |
||
386 | public function setFields($fields = []) |
||
392 | |||
393 | /** |
||
394 | * Format the provided Attribute Fields into a more usable format. |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | protected function formatFields() |
||
408 | |||
409 | /** |
||
410 | * Determine if the Model Admin is sortable in it's list view. |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | public function isSortable() |
||
418 | |||
419 | /** |
||
420 | * Determine if the Model Admin is sortable by a defined key / column. |
||
421 | * |
||
422 | * @param string $key |
||
423 | * |
||
424 | * @return bool |
||
425 | */ |
||
426 | public function isSortableBy($key) |
||
445 | |||
446 | /** |
||
447 | * Determine if the Model Admin has Viewing Capabilities. |
||
448 | * |
||
449 | * @return bool |
||
450 | */ |
||
451 | public function hasViewing() |
||
455 | |||
456 | /** |
||
457 | * Determine if the Model Admin has Creating Capabilities. |
||
458 | * |
||
459 | * @return bool |
||
460 | */ |
||
461 | public function hasCreating() |
||
465 | |||
466 | /** |
||
467 | * Determine if the Model Admin has Cloning Capabilities. |
||
468 | * |
||
469 | * @return bool |
||
470 | */ |
||
471 | public function hasCloning() |
||
475 | |||
476 | /** |
||
477 | * Determine if the Model Admin has Editting Capabilities. |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | public function hasEditting() |
||
485 | |||
486 | /** |
||
487 | * Determine if the Model Admin has Deleting Capabilities. |
||
488 | * |
||
489 | * @return bool |
||
490 | */ |
||
491 | public function hasDeleting() |
||
495 | |||
496 | /** |
||
497 | * Determine if the Managed Model is using the SoftDeletes Trait. |
||
498 | * |
||
499 | * This is guarded by hasDeleting, since we shouldn't allow SoftDeleting |
||
500 | * without the deleting trait (even though it isn't really required). |
||
501 | * |
||
502 | * @return bool |
||
503 | */ |
||
504 | public function hasSoftDeleting() |
||
518 | |||
519 | /** |
||
520 | * Determine if the Model Admin has Validating Capabilities. |
||
521 | * |
||
522 | * @return bool |
||
523 | */ |
||
524 | public function hasValidating() |
||
528 | |||
529 | /** |
||
530 | * Determine if the Managed Model has a Trait and Contract |
||
531 | * |
||
532 | * @return bool |
||
533 | */ |
||
534 | public function hasTraitAndContract($trait = null, $contract = null) |
||
538 | |||
539 | /** |
||
540 | * Returns whether the current ModelAdmin has a given trait. |
||
541 | * |
||
542 | * @param string $trait |
||
543 | * |
||
544 | * @return boolean |
||
545 | */ |
||
546 | public function hasTrait($trait = null) |
||
554 | |||
555 | /** |
||
556 | * Returns whether the current ModelAdmin has a given contract. |
||
557 | * |
||
558 | * @param string $contract |
||
559 | * |
||
560 | * @return boolean |
||
561 | */ |
||
562 | public function hasContract($contract = null) |
||
570 | } |
||
571 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.