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 |
||
| 11 | class ModelAdmin extends Admin implements ModelQueryable |
||
| 12 | { |
||
| 13 | use ModelQuerying; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class of Model to Manage. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $managedModel; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * ModelAdmin Icon. |
||
| 24 | * |
||
| 25 | * Font Awesome Defined Icon, eg 'user' = 'fa-user' |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $icon = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Validation Rules for onCreate, onEdit actions. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $rules = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Columns for Model. |
||
| 40 | * |
||
| 41 | * Defines which fields to show in the listing tables output. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $columns = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Columns for Model are Sortable. |
||
| 49 | * |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $sortable = true; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The Controller to be used by the Model Admin. |
||
| 56 | * |
||
| 57 | * This defaults to parent::getController() |
||
| 58 | * if it has been left undefined. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $controller = '\LaravelFlare\Flare\Admin\Models\ModelAdminController'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The Policy used for the Model Authorization logic. |
||
| 66 | * |
||
| 67 | * This class should implement the ModelAdminPoliceable which |
||
| 68 | * includes authorization checks for the create, view, edit and delete actions. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $policy = '\LaravelFlare\Flare\Permissions\ModelAdminPolicy'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The current model to be managed. |
||
| 76 | * |
||
| 77 | * @var Model |
||
| 78 | */ |
||
| 79 | public $model; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * __construct. |
||
| 83 | */ |
||
| 84 | public function __construct() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns a Model Instance. |
||
| 93 | * |
||
| 94 | * @return Model |
||
| 95 | */ |
||
| 96 | public function model() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns a New Model Instance. |
||
| 109 | * |
||
| 110 | * @return Model |
||
| 111 | */ |
||
| 112 | public function newModel() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the Managed Model Class. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getManagedModel() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set the Managed Model Class. |
||
| 135 | * |
||
| 136 | * @param string $managedModel |
||
| 137 | */ |
||
| 138 | public function setManagedModel($managedModel = null) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns the Route Paramets. |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function routeParameters() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Formats and returns the Columns. |
||
| 157 | * |
||
| 158 | * This is really gross, I'm removing it soon. |
||
| 159 | * |
||
| 160 | * @return |
||
| 161 | */ |
||
| 162 | public function getColumns() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Gets an Attribute by the provided key |
||
| 206 | * on either the current model or a provided model instance. |
||
| 207 | * |
||
| 208 | * @param string $key |
||
| 209 | * @param mixed $model |
||
| 210 | * |
||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | public function getAttribute($key, $model = false) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Determine if a get accessor exists for an attribute. |
||
| 234 | * |
||
| 235 | * @param string $key |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | public function hasGetAccessor($key) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Determines if a key resolved a related Model. |
||
| 246 | * |
||
| 247 | * @param string $key |
||
| 248 | * @param mixed $model |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function hasRelatedKey($key, $model = false) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Resolves a relation based on the key provided, |
||
| 270 | * either on the current model or a provided model instance. |
||
| 271 | * |
||
| 272 | * @param string $key |
||
| 273 | * @param mixed $model |
||
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public function relatedKey($key, $model = false) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Set a given attribute on the model. |
||
| 303 | * |
||
| 304 | * @param string $key |
||
| 305 | * @param mixed $value |
||
| 306 | */ |
||
| 307 | public function setAttribute($key, $value) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Determine if a set mutator exists for an attribute. |
||
| 320 | * |
||
| 321 | * @param string $key |
||
| 322 | * |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | public function hasSetMutator($key) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Determine if a get mutator exists for an attribute. |
||
| 332 | * |
||
| 333 | * @param string $key |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function hasGetMutator($key) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns an array of Attribute Fields ready for output. |
||
| 344 | * |
||
| 345 | * @param string $type |
||
| 346 | * |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public function outputFields($type = 'view') |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Gets the Managed Model Mapping. |
||
| 356 | * |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | public function getFields() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Gets the Managed Model Mapping. |
||
| 366 | * |
||
| 367 | * @param array $fields |
||
| 368 | */ |
||
| 369 | public function setFields($fields = []) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Determine if the Model Admin is sortable in it's list view. |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public function isSortable() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Determine if the Model Admin has Viewing Capabilities. |
||
| 386 | * |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | public function hasViewing() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Determine if the Model Admin has Creating Capabilities. |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | public function hasCreating() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Determine if the Model Admin has Cloning Capabilities. |
||
| 406 | * |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | public function hasCloning() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Determine if the Model Admin has Editting Capabilities. |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | public function hasEditting() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Determine if the Model Admin has Deleting Capabilities. |
||
| 426 | * |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function hasDeleting() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Determine if the Managed Model is using the SoftDeletes Trait. |
||
| 436 | * |
||
| 437 | * This is guarded by hasDeleting, since we shouldn't allow SoftDeleting |
||
| 438 | * without the deleting trait (even though it isn't really required). |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function hasSoftDeletes() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Determine if the Model Admin has Validating Capabilities. |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | public function hasValidating() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Determine if the Managed Model has a Trait and Contract |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | public function hasTraitAndContract($trait = null, $contract = null) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns whether the current ModelAdmin has a given trait. |
||
| 479 | * |
||
| 480 | * @param string $trait |
||
| 481 | * |
||
| 482 | * @return boolean |
||
| 483 | */ |
||
| 484 | public function hasTrait($trait = null) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Returns whether the current ModelAdmin has a given contract. |
||
| 497 | * |
||
| 498 | * @param string $contract |
||
| 499 | * |
||
| 500 | * @return boolean |
||
| 501 | */ |
||
| 502 | public function hasContract($contract = null) |
||
| 510 | } |
||
| 511 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.