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 |
||
| 43 | abstract class ModelAdmin extends LeftAndMain { |
||
| 44 | |||
| 45 | private static $url_rule = '/$ModelClass/$Action'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * List of all managed {@link DataObject}s in this interface. |
||
| 49 | * |
||
| 50 | * Simple notation with class names only: |
||
| 51 | * <code> |
||
| 52 | * array('MyObjectClass','MyOtherObjectClass') |
||
| 53 | * </code> |
||
| 54 | * |
||
| 55 | * Extended notation with options (e.g. custom titles): |
||
| 56 | * <code> |
||
| 57 | * array( |
||
| 58 | * 'MyObjectClass' => array('title' => "Custom title") |
||
| 59 | * ) |
||
| 60 | * </code> |
||
| 61 | * |
||
| 62 | * Available options: |
||
| 63 | * - 'title': Set custom titles for the tabs or dropdown names |
||
| 64 | * |
||
| 65 | * @config |
||
| 66 | * @var array|string |
||
| 67 | */ |
||
| 68 | private static $managed_models = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Override menu_priority so that ModelAdmin CMSMenu objects |
||
| 72 | * are grouped together directly above the Help menu item. |
||
| 73 | * @var float |
||
| 74 | */ |
||
| 75 | private static $menu_priority = -0.5; |
||
| 76 | |||
| 77 | private static $menu_icon = 'framework/admin/client/src/sprites/menu-icons/16x16/db.png'; |
||
| 78 | |||
| 79 | private static $allowed_actions = array( |
||
| 80 | 'ImportForm', |
||
| 81 | 'SearchForm', |
||
| 82 | ); |
||
| 83 | |||
| 84 | private static $url_handlers = array( |
||
| 85 | '$ModelClass/$Action' => 'handleAction' |
||
| 86 | ); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var String |
||
| 90 | */ |
||
| 91 | protected $modelClass; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Change this variable if you don't want the Import from CSV form to appear. |
||
| 95 | * This variable can be a boolean or an array. |
||
| 96 | * If array, you can list className you want the form to appear on. i.e. array('myClassOne','myClasstwo') |
||
| 97 | */ |
||
| 98 | public $showImportForm = true; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * List of all {@link DataObject}s which can be imported through |
||
| 102 | * a subclass of {@link BulkLoader} (mostly CSV data). |
||
| 103 | * By default {@link CsvBulkLoader} is used, assuming a standard mapping |
||
| 104 | * of column names to {@link DataObject} properties/relations. |
||
| 105 | * |
||
| 106 | * e.g. "BlogEntry" => "BlogEntryCsvBulkLoader" |
||
| 107 | * |
||
| 108 | * @config |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | private static $model_importers = null; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Amount of results showing on a single page. |
||
| 115 | * |
||
| 116 | * @config |
||
| 117 | * @var int |
||
| 118 | */ |
||
| 119 | private static $page_length = 30; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Initialize the model admin interface. Sets up embedded jquery libraries and requisite plugins. |
||
| 123 | */ |
||
| 124 | protected function init() { |
||
| 143 | |||
| 144 | public function Link($action = null) { |
||
| 148 | |||
| 149 | public function getEditForm($id = null, $fields = null) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Define which fields are used in the {@link getEditForm} GridField export. |
||
| 188 | * By default, it uses the summary fields from the model definition. |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | public function getExportFields() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return SearchContext |
||
| 198 | */ |
||
| 199 | public function getSearchContext() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return Form |
||
| 213 | */ |
||
| 214 | public function SearchForm() { |
||
| 237 | |||
| 238 | public function getList() { |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns managed models' create, search, and import forms |
||
| 256 | * @uses SearchContext |
||
| 257 | * @uses SearchFilter |
||
| 258 | * @return SS_List of forms |
||
| 259 | */ |
||
| 260 | protected function getManagedModelTabs() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sanitise a model class' name for inclusion in a link |
||
| 278 | * |
||
| 279 | * @param string $class |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | protected function sanitiseClassName($class) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Unsanitise a model class' name from a URL param |
||
| 288 | * |
||
| 289 | * @param string $class |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | protected function unsanitiseClassName($class) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return array Map of class name to an array of 'title' (see {@link $managed_models}) |
||
| 298 | */ |
||
| 299 | public function getManagedModels() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns all importers defined in {@link self::$model_importers}. |
||
| 326 | * If none are defined, we fall back to {@link self::managed_models} |
||
| 327 | * with a default {@link CsvBulkLoader} class. In this case the column names of the first row |
||
| 328 | * in the CSV file are assumed to have direct mappings to properties on the object. |
||
| 329 | * |
||
| 330 | * @return array Map of model class names to importer instances |
||
| 331 | */ |
||
| 332 | public function getModelImporters() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Generate a CSV import form for a single {@link DataObject} subclass. |
||
| 353 | * |
||
| 354 | * @return Form |
||
| 355 | */ |
||
| 356 | public function ImportForm() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Imports the submitted CSV file based on specifications given in |
||
| 422 | * {@link self::model_importers}. |
||
| 423 | * Redirects back with a success/failure message. |
||
| 424 | * |
||
| 425 | * @todo Figure out ajax submission of files via jQuery.form plugin |
||
| 426 | * |
||
| 427 | * @param array $data |
||
| 428 | * @param Form $form |
||
| 429 | * @param SS_HTTPRequest $request |
||
| 430 | */ |
||
| 431 | public function import($data, $form, $request) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param bool $unlinked |
||
| 479 | * @return ArrayList |
||
| 480 | */ |
||
| 481 | public function Breadcrumbs($unlinked = false) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * overwrite the static page_length of the admin panel, |
||
| 500 | * should be called in the project _config file. |
||
| 501 | * |
||
| 502 | * @deprecated 4.0 Use "ModelAdmin.page_length" config setting |
||
| 503 | */ |
||
| 504 | public static function set_page_length($length){ |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Return the static page_length of the admin, default as 30 |
||
| 511 | * |
||
| 512 | * @deprecated 4.0 Use "ModelAdmin.page_length" config setting |
||
| 513 | */ |
||
| 514 | public static function get_page_length(){ |
||
| 518 | |||
| 519 | } |
||
| 520 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: