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 |
||
| 12 | class ModelAdmin extends Admin implements ModelQueryable |
||
| 13 | { |
||
| 14 | use ModelQuerying; |
||
| 15 | use ModelSaving; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Class of Model to Manage. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $managedModel; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * ModelAdmin Icon. |
||
| 26 | * |
||
| 27 | * Font Awesome Defined Icon, eg 'user' = 'fa-user' |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $icon = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Validation Rules for onCreate, onEdit actions. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $rules = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Columns for Model. |
||
| 42 | * |
||
| 43 | * Defines which fields to show in the listing tables output. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $columns = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Map Model Attributes to AttributeTypes with |
||
| 51 | * additional parameters which will be output |
||
| 52 | * as fields when viewing, editting or adding |
||
| 53 | * a new model entry. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $fields = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Columns for Model are Sortable. |
||
| 61 | * |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $sortable = true; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The Controller to be used by the Model Admin. |
||
| 68 | * |
||
| 69 | * This defaults to parent::getController() |
||
| 70 | * if it has been left undefined. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $controller = '\LaravelFlare\Flare\Admin\Models\ModelAdminController'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The Policy used for the Model Authorization logic. |
||
| 78 | * |
||
| 79 | * This class should implement the ModelAdminPoliceable which |
||
| 80 | * includes authorization checks for the create, view, edit and delete actions. |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $policy = '\LaravelFlare\Flare\Permissions\ModelAdminPolicy'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The current model to be managed. |
||
| 88 | * |
||
| 89 | * @var Model |
||
| 90 | */ |
||
| 91 | public $model; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * __construct. |
||
| 95 | */ |
||
| 96 | public function __construct() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns a Model Instance. |
||
| 105 | * |
||
| 106 | * @return Model |
||
| 107 | */ |
||
| 108 | public function model() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns a New Model Instance. |
||
| 121 | * |
||
| 122 | * @return Model |
||
| 123 | */ |
||
| 124 | public function newModel() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the Managed Model Class. |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getManagedModel() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set the Managed Model Class. |
||
| 147 | * |
||
| 148 | * @param string $managedModel |
||
| 149 | */ |
||
| 150 | public function setManagedModel($managedModel = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns the Route Paramets. |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function routeParameters() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Formats and returns the Columns. |
||
| 169 | * |
||
| 170 | * This is really gross, I'm removing it soon. |
||
| 171 | * |
||
| 172 | * @return |
||
| 173 | */ |
||
| 174 | public function getColumns() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Gets an Attribute by the provided key |
||
| 218 | * on either the current model or a provided model instance. |
||
| 219 | * |
||
| 220 | * @param string $key |
||
| 221 | * @param mixed $model |
||
| 222 | * |
||
| 223 | * @return mixed |
||
| 224 | */ |
||
| 225 | public function getAttribute($key, $model = false) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Determine if a get accessor exists for an attribute. |
||
| 246 | * |
||
| 247 | * @param string $key |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function hasGetAccessor($key) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Determines if a key resolved a related Model. |
||
| 258 | * |
||
| 259 | * @param string $key |
||
| 260 | * @param mixed $model |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | public function hasRelatedKey($key, $model = false) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Resolves a relation based on the key provided, |
||
| 282 | * either on the current model or a provided model instance. |
||
| 283 | * |
||
| 284 | * @param string $key |
||
| 285 | * @param mixed $model |
||
| 286 | * |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | public function relatedKey($key, $model = false) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set a given attribute on the model. |
||
| 315 | * |
||
| 316 | * @param string $key |
||
| 317 | * @param mixed $value |
||
| 318 | */ |
||
| 319 | public function setAttribute($key, $value) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Determine if a set mutator exists for an attribute. |
||
| 332 | * |
||
| 333 | * @param string $key |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function hasSetMutator($key) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Determine if a get mutator exists for an attribute. |
||
| 344 | * |
||
| 345 | * @param string $key |
||
| 346 | * |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | public function hasGetMutator($key) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Returns an array of Attribute Fields ready for output. |
||
| 356 | * |
||
| 357 | * @param string $type |
||
| 358 | * |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | public function outputFields($type = 'view') |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Gets the Managed Model Mapping. |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | public function getFields() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Gets the Managed Model Mapping. |
||
| 378 | * |
||
| 379 | * @param array $fields |
||
| 380 | */ |
||
| 381 | public function setFields($fields = []) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Determine if the Model Admin is sortable in it's list view. |
||
| 388 | * |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | public function isSortable() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Determine if the Model Admin has Viewing Capabilities. |
||
| 398 | * |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | public function hasViewing() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Determine if the Model Admin has Creating Capabilities. |
||
| 408 | * |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | public function hasCreating() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Determine if the Model Admin has Cloning Capabilities. |
||
| 418 | * |
||
| 419 | * @return bool |
||
| 420 | */ |
||
| 421 | public function hasCloning() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Determine if the Model Admin has Editting Capabilities. |
||
| 428 | * |
||
| 429 | * @return bool |
||
| 430 | */ |
||
| 431 | public function hasEditting() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Determine if the Model Admin has Deleting Capabilities. |
||
| 438 | * |
||
| 439 | * @return bool |
||
| 440 | */ |
||
| 441 | public function hasDeleting() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Determine if the Managed Model is using the SoftDeletes Trait. |
||
| 448 | * |
||
| 449 | * This is guarded by hasDeleting, since we shouldn't allow SoftDeleting |
||
| 450 | * without the deleting trait (even though it isn't really required). |
||
| 451 | * |
||
| 452 | * @return bool |
||
| 453 | */ |
||
| 454 | public function hasSoftDeleting() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Determine if the Model Admin has Validating Capabilities. |
||
| 471 | * |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | public function hasValidating() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Determine if the Managed Model has a Trait and Contract |
||
| 481 | * |
||
| 482 | * @return bool |
||
| 483 | */ |
||
| 484 | public function hasTraitAndContract($trait = null, $contract = null) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Returns whether the current ModelAdmin has a given trait. |
||
| 491 | * |
||
| 492 | * @param string $trait |
||
| 493 | * |
||
| 494 | * @return boolean |
||
| 495 | */ |
||
| 496 | public function hasTrait($trait = null) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Returns whether the current ModelAdmin has a given contract. |
||
| 507 | * |
||
| 508 | * @param string $contract |
||
| 509 | * |
||
| 510 | * @return boolean |
||
| 511 | */ |
||
| 512 | public function hasContract($contract = null) |
||
| 520 | } |
||
| 521 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.