Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | * Entiy Title . |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $entityTitle; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Plural Entity Title. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $pluralEntityTitle; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Validation Rules for onCreate, onEdit actions. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $rules = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Columns for Model. |
||
| 47 | * |
||
| 48 | * Defines which fields to show in the listing tables output. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $columns = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Map Model Attributes to FieldTypes with |
||
| 56 | * additional parameters which will be output |
||
| 57 | * as fields when viewing, editing or adding |
||
| 58 | * a new model entry. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $fields = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Columns for Model are Sortable. |
||
| 66 | * |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | protected $sortable = true; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The Controller to be used by the Model Admin. |
||
| 73 | * |
||
| 74 | * This defaults to parent::getController() |
||
| 75 | * if it has been left undefined. |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $controller = '\LaravelFlare\Flare\Admin\Models\ModelAdminController'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The Policy used for the Model Authorization logic. |
||
| 83 | * |
||
| 84 | * This class should implement the ModelAdminPoliceable which |
||
| 85 | * includes authorization checks for the create, view, edit and delete actions. |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $policy = '\LaravelFlare\Flare\Permissions\ModelAdminPolicy'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The current model to be managed. |
||
| 93 | * |
||
| 94 | * @var Model |
||
| 95 | */ |
||
| 96 | public $model; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * __construct. |
||
| 100 | */ |
||
| 101 | public function __construct() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns a Model Instance. |
||
| 110 | * |
||
| 111 | * @return Model |
||
| 112 | */ |
||
| 113 | public function model() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns a New Model Instance. |
||
| 126 | * |
||
| 127 | * @return Model |
||
| 128 | */ |
||
| 129 | public function newModel() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the Managed Model Class. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getManagedModel() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Set the Managed Model Class. |
||
| 152 | * |
||
| 153 | * @param string $managedModel |
||
| 154 | */ |
||
| 155 | public function setManagedModel($managedModel = null) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the Entity Title. |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function getEntityTitle() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Set Entity Title. |
||
| 176 | * |
||
| 177 | * @param string $entityTitle |
||
| 178 | */ |
||
| 179 | public function setTitle($entityTitle = null) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Plural Entity Title. |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getPluralEntityTitle() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Set Plural Title. |
||
| 200 | * |
||
| 201 | * @param string $pluralEntityTitle |
||
| 202 | */ |
||
| 203 | public function setPluralTitle($pluralEntityTitle = null) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Returns the Route Paramets. |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public function routeParameters() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Formats and returns the Columns. |
||
| 222 | * |
||
| 223 | * This is really gross, I'm removing it soon. |
||
| 224 | * |
||
| 225 | * @return |
||
| 226 | */ |
||
| 227 | public function getColumns() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return the Validation Rules. |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function getRules() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return the Create Model Entry Validation Rules. |
||
| 281 | * |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | public function getCreateRules() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return the Update Model Entry Validation Rules. |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function getUpdateRules() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Gets an Attribute by the provided key |
||
| 301 | * on either the current model or a provided model instance. |
||
| 302 | * |
||
| 303 | * @param string $key |
||
| 304 | * @param mixed $model |
||
| 305 | * |
||
| 306 | * @return mixed |
||
| 307 | */ |
||
| 308 | public function getAttribute($key, $model = false) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Determine if a get accessor exists for an attribute. |
||
| 341 | * |
||
| 342 | * @param string $key |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | public function hasGetAccessor($key) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Determines if a key resolved a related Model. |
||
| 353 | * |
||
| 354 | * @param string $key |
||
| 355 | * @param mixed $model |
||
| 356 | * |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | public function hasRelatedKey($key, $model = false) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Resolves a relation based on the key provided, |
||
| 377 | * either on the current model or a provided model instance. |
||
| 378 | * |
||
| 379 | * @param string $key |
||
| 380 | * @param mixed $model |
||
| 381 | * |
||
| 382 | * @return mixed |
||
| 383 | */ |
||
| 384 | public function relatedKey($key, $model = false) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Set a given attribute on the model. |
||
| 410 | * |
||
| 411 | * @param string $key |
||
| 412 | * @param mixed $value |
||
| 413 | */ |
||
| 414 | public function setAttribute($key, $value) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Determine if a set mutator exists for an attribute. |
||
| 427 | * |
||
| 428 | * @param string $key |
||
| 429 | * |
||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | public function hasSetMutator($key) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Determine if a get mutator exists for an attribute. |
||
| 439 | * |
||
| 440 | * @param string $key |
||
| 441 | * |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | public function hasGetMutator($key) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Returns an array of Attribute Fields ready for output. |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | public function outputFields() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Gets the Managed Model Mapping. |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | public function getFields() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Sets the Managed Model Mapping. |
||
| 473 | * |
||
| 474 | * @param array $fields |
||
| 475 | */ |
||
| 476 | public function setFields($fields = []) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Format the provided Attribute Fields into a more usable format. |
||
| 485 | */ |
||
| 486 | protected function formatFields() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Determine if the Model Admin is sortable in it's list view. |
||
| 499 | * |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | public function isSortable() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Determine if the Model Admin is sortable by a defined key / column. |
||
| 509 | * |
||
| 510 | * @param string $key |
||
| 511 | * |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | public function isSortableBy($key) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Determine if the Model Admin has Viewing Capabilities. |
||
| 536 | * |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | public function hasViewing() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Determine if the Model Admin has Creating Capabilities. |
||
| 546 | * |
||
| 547 | * @return bool |
||
| 548 | */ |
||
| 549 | public function hasCreating() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Determine if the Model Admin has Cloning Capabilities. |
||
| 556 | * |
||
| 557 | * @return bool |
||
| 558 | */ |
||
| 559 | public function hasCloning() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Determine if the Model Admin has Editing Capabilities. |
||
| 566 | * |
||
| 567 | * @return bool |
||
| 568 | */ |
||
| 569 | public function hasEditing() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Determine if the Model Admin has Deleting Capabilities. |
||
| 576 | * |
||
| 577 | * @return bool |
||
| 578 | */ |
||
| 579 | public function hasDeleting() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Determine if the Managed Model is using the SoftDeletes Trait. |
||
| 586 | * |
||
| 587 | * This is guarded by hasDeleting, since we shouldn't allow SoftDeleting |
||
| 588 | * without the deleting trait (even though it isn't really required). |
||
| 589 | * |
||
| 590 | * @return bool |
||
| 591 | */ |
||
| 592 | public function hasSoftDeleting() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Determine if the Model Admin has Validating Capabilities. |
||
| 607 | * |
||
| 608 | * @return bool |
||
| 609 | */ |
||
| 610 | public function hasValidating() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Determine if the Managed Model has a Trait and Contract. |
||
| 617 | * |
||
| 618 | * @return bool |
||
| 619 | */ |
||
| 620 | public function hasTraitAndContract($trait = null, $contract = null) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Returns whether the current ModelAdmin has a given trait. |
||
| 627 | * |
||
| 628 | * @param string $trait |
||
| 629 | * |
||
| 630 | * @return bool |
||
| 631 | */ |
||
| 632 | public function hasTrait($trait = null) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Returns whether the current ModelAdmin has a given contract. |
||
| 643 | * |
||
| 644 | * @param string $contract |
||
| 645 | * |
||
| 646 | * @return bool |
||
| 647 | */ |
||
| 648 | public function hasContract($contract = null) |
||
| 656 | } |
||
| 657 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.