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 |
||
| 17 | class ModelAdmin extends Admin implements ModelWriteable, ModelQueryable, ModelValidatable, ModelCloneable |
||
| 18 | { |
||
| 19 | use ModelWriting; |
||
| 20 | use ModelQuerying; |
||
| 21 | use ModelValidating; |
||
| 22 | use ModelCloning; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class of Model to Manage. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $managedModel; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * ModelAdmin Icon. |
||
| 33 | * |
||
| 34 | * Font Awesome Defined Icon, eg 'user' = 'fa-user' |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $icon = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Validation Rules for onCreate, onEdit actions. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $rules = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Columns for Model. |
||
| 49 | * |
||
| 50 | * Defines which fields to show in the listing tables output. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $columns = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Map Model Attributes to AttributeTypes with |
||
| 58 | * additional parameters which will be output |
||
| 59 | * as fields when viewing, editting or adding |
||
| 60 | * a new model entry. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $fields = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Columns for Model are Sortable. |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | protected $sortable = true; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The Controller to be used by the Model Admin. |
||
| 75 | * |
||
| 76 | * This defaults to parent::getController() |
||
| 77 | * if it has been left undefined. |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $controller = '\LaravelFlare\Flare\Admin\Models\ModelAdminController'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The Policy used for the Model Authorization logic. |
||
| 85 | * |
||
| 86 | * This class should implement the ModelAdminPoliceable which |
||
| 87 | * includes authorization checks for the create, view, edit and delete actions. |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $policy = '\LaravelFlare\Flare\Permissions\ModelAdminPolicy'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The current model to be managed. |
||
| 95 | * |
||
| 96 | * @var Model |
||
| 97 | */ |
||
| 98 | public $model; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * __construct. |
||
| 102 | */ |
||
| 103 | public function __construct() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns a Model Instance. |
||
| 112 | * |
||
| 113 | * @return Model |
||
| 114 | */ |
||
| 115 | public function model() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns a New Model Instance. |
||
| 128 | * |
||
| 129 | * @return Model |
||
| 130 | */ |
||
| 131 | public function newModel() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the Managed Model Class. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getManagedModel() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set the Managed Model Class. |
||
| 154 | * |
||
| 155 | * @param string $managedModel |
||
| 156 | */ |
||
| 157 | public function setManagedModel($managedModel = null) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns the Route Paramets. |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | public function routeParameters() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Formats and returns the Columns. |
||
| 176 | * |
||
| 177 | * This is really gross, I'm removing it soon. |
||
| 178 | * |
||
| 179 | * @return |
||
| 180 | */ |
||
| 181 | public function getColumns() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Gets an Attribute by the provided key |
||
| 225 | * on either the current model or a provided model instance. |
||
| 226 | * |
||
| 227 | * @param string $key |
||
| 228 | * @param mixed $model |
||
| 229 | * |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | public function getAttribute($key, $model = false) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Determine if a get accessor exists for an attribute. |
||
| 253 | * |
||
| 254 | * @param string $key |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function hasGetAccessor($key) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Determines if a key resolved a related Model. |
||
| 265 | * |
||
| 266 | * @param string $key |
||
| 267 | * @param mixed $model |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function hasRelatedKey($key, $model = false) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Resolves a relation based on the key provided, |
||
| 289 | * either on the current model or a provided model instance. |
||
| 290 | * |
||
| 291 | * @param string $key |
||
| 292 | * @param mixed $model |
||
| 293 | * |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | public function relatedKey($key, $model = false) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Set a given attribute on the model. |
||
| 322 | * |
||
| 323 | * @param string $key |
||
| 324 | * @param mixed $value |
||
| 325 | */ |
||
| 326 | public function setAttribute($key, $value) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Determine if a set mutator exists for an attribute. |
||
| 339 | * |
||
| 340 | * @param string $key |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function hasSetMutator($key) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Determine if a get mutator exists for an attribute. |
||
| 351 | * |
||
| 352 | * @param string $key |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | public function hasGetMutator($key) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Returns an array of Attribute Fields ready for output. |
||
| 363 | * |
||
| 364 | * @param string $type |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | public function outputFields($type = 'view') |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Gets the Managed Model Mapping. |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | public function getFields() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Gets the Managed Model Mapping. |
||
| 385 | * |
||
| 386 | * @param array $fields |
||
| 387 | */ |
||
| 388 | public function setFields($fields = []) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Determine if the Model Admin is sortable in it's list view. |
||
| 395 | * |
||
| 396 | * @param string $key |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | public function isSortable() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Determine if the Managed Model is using the SoftDeletes Trait. |
||
| 407 | * |
||
| 408 | * @return bool |
||
| 409 | */ |
||
| 410 | public function hasSoftDeletes() |
||
| 420 | } |
||
| 421 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.