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 AssetAdmin 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 AssetAdmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class AssetAdmin extends LeftAndMain implements PermissionProvider |
||
48 | { |
||
49 | private static $url_segment = 'assets'; |
||
50 | |||
51 | private static $url_rule = '/$Action/$ID'; |
||
52 | |||
53 | private static $menu_title = 'Files'; |
||
54 | |||
55 | private static $menu_icon_class = 'font-icon-image'; |
||
56 | |||
57 | private static $tree_class = Folder::class; |
||
58 | |||
59 | private static $url_handlers = [ |
||
60 | // Legacy redirect for SS3-style detail view |
||
61 | 'EditForm/field/File/item/$FileID/$Action' => 'legacyRedirectForEditView', |
||
62 | // Pass all URLs to the index, for React to unpack |
||
63 | 'show/$FolderID/edit/$FileID' => 'index', |
||
64 | // API access points with structured data |
||
65 | 'POST api/createFile' => 'apiCreateFile', |
||
66 | 'POST api/uploadFile' => 'apiUploadFile', |
||
67 | 'GET api/history' => 'apiHistory', |
||
68 | 'fileEditForm/$ID' => 'fileEditForm', |
||
69 | 'fileInsertForm/$ID' => 'fileInsertForm', |
||
70 | 'fileEditorLinkForm/$ID' => 'fileEditorLinkForm', |
||
71 | 'fileHistoryForm/$ID/$VersionID' => 'fileHistoryForm', |
||
72 | 'folderCreateForm/$ParentID' => 'folderCreateForm', |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * Amount of results showing on a single page. |
||
77 | * |
||
78 | * @config |
||
79 | * @var int |
||
80 | */ |
||
81 | private static $page_length = 50; |
||
82 | |||
83 | /** |
||
84 | * @config |
||
85 | * @see Upload->allowedMaxFileSize |
||
86 | * @var int |
||
87 | */ |
||
88 | private static $allowed_max_file_size; |
||
89 | |||
90 | /** |
||
91 | * @config |
||
92 | * |
||
93 | * @var int |
||
94 | */ |
||
95 | private static $max_history_entries = 100; |
||
96 | |||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | private static $allowed_actions = array( |
||
101 | 'legacyRedirectForEditView', |
||
102 | 'apiCreateFile', |
||
103 | 'apiUploadFile', |
||
104 | 'apiHistory', |
||
105 | 'folderCreateForm', |
||
106 | 'fileEditForm', |
||
107 | 'fileHistoryForm', |
||
108 | 'addToCampaignForm', |
||
109 | 'fileInsertForm', |
||
110 | 'fileEditorLinkForm', |
||
111 | 'schema', |
||
112 | 'fileSelectForm', |
||
113 | 'fileSearchForm', |
||
114 | ); |
||
115 | |||
116 | private static $required_permission_codes = 'CMS_ACCESS_AssetAdmin'; |
||
117 | |||
118 | /** |
||
119 | * Retina thumbnail image (native size: 176) |
||
120 | * |
||
121 | * @config |
||
122 | * @var int |
||
123 | */ |
||
124 | private static $thumbnail_width = 352; |
||
125 | |||
126 | /** |
||
127 | * Retina thumbnail height (native size: 132) |
||
128 | * |
||
129 | * @config |
||
130 | * @var int |
||
131 | */ |
||
132 | private static $thumbnail_height = 264; |
||
133 | |||
134 | /** |
||
135 | * Safely limit max inline thumbnail size to 200kb |
||
136 | * |
||
137 | * @config |
||
138 | * @var int |
||
139 | */ |
||
140 | private static $max_thumbnail_bytes = 200000; |
||
141 | |||
142 | /** |
||
143 | * Set up the controller |
||
144 | */ |
||
145 | public function init() |
||
146 | { |
||
147 | parent::init(); |
||
148 | |||
149 | $module = ModuleLoader::getModule('silverstripe/asset-admin'); |
||
150 | Requirements::add_i18n_javascript($module->getResourcePath('client/lang'), false, true); |
||
151 | Requirements::javascript($module->getResourcePath("client/dist/js/bundle.js")); |
||
152 | Requirements::css($module->getResourcePath("client/dist/styles/bundle.css")); |
||
153 | |||
154 | CMSBatchActionHandler::register('delete', DeleteAssets::class, Folder::class); |
||
155 | } |
||
156 | |||
157 | public function getClientConfig() |
||
158 | { |
||
159 | $baseLink = $this->Link(); |
||
160 | return array_merge(parent::getClientConfig(), [ |
||
161 | 'reactRouter' => true, |
||
162 | 'createFileEndpoint' => [ |
||
163 | 'url' => Controller::join_links($baseLink, 'api/createFile'), |
||
164 | 'method' => 'post', |
||
165 | 'payloadFormat' => 'urlencoded', |
||
166 | ], |
||
167 | 'uploadFileEndpoint' => [ |
||
168 | 'url' => Controller::join_links($baseLink, 'api/uploadFile'), |
||
169 | 'method' => 'post', |
||
170 | 'payloadFormat' => 'urlencoded', |
||
171 | ], |
||
172 | 'historyEndpoint' => [ |
||
173 | 'url' => Controller::join_links($baseLink, 'api/history'), |
||
174 | 'method' => 'get', |
||
175 | 'responseFormat' => 'json', |
||
176 | ], |
||
177 | 'limit' => $this->config()->page_length, |
||
178 | 'form' => [ |
||
179 | 'fileEditForm' => [ |
||
180 | 'schemaUrl' => $this->Link('schema/fileEditForm') |
||
181 | ], |
||
182 | 'fileInsertForm' => [ |
||
183 | 'schemaUrl' => $this->Link('schema/fileInsertForm') |
||
184 | ], |
||
185 | 'remoteEditForm' => [ |
||
186 | 'schemaUrl' => LeftAndMain::singleton() |
||
187 | ->Link('Modals/remoteEditFormSchema'), |
||
188 | ], |
||
189 | 'remoteCreateForm' => [ |
||
190 | 'schemaUrl' => LeftAndMain::singleton() |
||
191 | ->Link('methodSchema/Modals/remoteCreateForm') |
||
192 | ], |
||
193 | 'fileSelectForm' => [ |
||
194 | 'schemaUrl' => $this->Link('schema/fileSelectForm') |
||
195 | ], |
||
196 | 'addToCampaignForm' => [ |
||
197 | 'schemaUrl' => $this->Link('schema/addToCampaignForm') |
||
198 | ], |
||
199 | 'fileHistoryForm' => [ |
||
200 | 'schemaUrl' => $this->Link('schema/fileHistoryForm') |
||
201 | ], |
||
202 | 'fileSearchForm' => [ |
||
203 | 'schemaUrl' => $this->Link('schema/fileSearchForm') |
||
204 | ], |
||
205 | 'folderCreateForm' => [ |
||
206 | 'schemaUrl' => $this->Link('schema/folderCreateForm') |
||
207 | ], |
||
208 | 'fileEditorLinkForm' => [ |
||
209 | 'schemaUrl' => $this->Link('schema/fileEditorLinkForm'), |
||
210 | ], |
||
211 | ], |
||
212 | ]); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Creates a single file based on a form-urlencoded upload. |
||
217 | * |
||
218 | * @param HTTPRequest $request |
||
219 | * @return HTTPRequest|HTTPResponse |
||
220 | */ |
||
221 | public function apiCreateFile(HTTPRequest $request) |
||
301 | |||
302 | /** |
||
303 | * Upload a new asset for a pre-existing record. Returns the asset tuple. |
||
304 | * |
||
305 | * Note that conflict resolution is as follows: |
||
306 | * - If uploading a file with the same extension, we simply keep the same filename, |
||
307 | * and overwrite any existing files (same name + sha = don't duplicate). |
||
308 | * - If uploading a new file with a different extension, then the filename will |
||
309 | * be replaced, and will be checked for uniqueness against other File dataobjects. |
||
310 | * |
||
311 | * @param HTTPRequest $request Request containing vars 'ID' of parent record ID, |
||
312 | * and 'Name' as form filename value |
||
313 | * @return HTTPRequest|HTTPResponse |
||
314 | */ |
||
315 | public function apiUploadFile(HTTPRequest $request) |
||
396 | |||
397 | /** |
||
398 | * Returns a JSON array for history of a given file ID. Returns a list of all the history. |
||
399 | * |
||
400 | * @param HTTPRequest $request |
||
401 | * @return HTTPResponse |
||
402 | */ |
||
403 | public function apiHistory(HTTPRequest $request) |
||
491 | |||
492 | /** |
||
493 | * Redirects 3.x style detail links to new 4.x style routing. |
||
494 | * |
||
495 | * @param HTTPRequest $request |
||
496 | */ |
||
497 | public function legacyRedirectForEditView($request) |
||
505 | |||
506 | /** |
||
507 | * Given a file return the CMS link to edit it |
||
508 | * |
||
509 | * @param File $file |
||
510 | * @return string |
||
511 | */ |
||
512 | public function getFileEditLink($file) |
||
525 | |||
526 | /** |
||
527 | * Get an asset renamer for the given filename. |
||
528 | * |
||
529 | * @param string $filename Path name |
||
530 | * @return AssetNameGenerator |
||
531 | */ |
||
532 | protected function getNameGenerator($filename) |
||
537 | |||
538 | /** |
||
539 | * @todo Implement on client |
||
540 | * |
||
541 | * @param bool $unlinked |
||
542 | * @return ArrayList |
||
543 | */ |
||
544 | public function breadcrumbs($unlinked = false) |
||
548 | |||
549 | |||
550 | /** |
||
551 | * Don't include class namespace in auto-generated CSS class |
||
552 | */ |
||
553 | public function baseCSSClasses() |
||
557 | |||
558 | public function providePermissions() |
||
569 | |||
570 | /** |
||
571 | * Build a form scaffolder for this model |
||
572 | * |
||
573 | * NOTE: Volatile api. May be moved to {@see LeftAndMain} |
||
574 | * |
||
575 | * @param File $file |
||
576 | * @return FormFactory |
||
577 | */ |
||
578 | public function getFormFactory(File $file) |
||
591 | |||
592 | /** |
||
593 | * The form is used to generate a form schema, |
||
594 | * as well as an intermediary object to process data through API endpoints. |
||
595 | * Since it's used directly on API endpoints, it does not have any form actions. |
||
596 | * It handles both {@link File} and {@link Folder} records. |
||
597 | * |
||
598 | * @param int $id |
||
599 | * @return Form |
||
600 | */ |
||
601 | public function getFileEditForm($id) |
||
605 | |||
606 | /** |
||
607 | * Get file edit form |
||
608 | * |
||
609 | * @param HTTPRequest $request |
||
610 | * @return Form |
||
611 | */ |
||
612 | View Code Duplication | public function fileEditForm($request = null) |
|
626 | |||
627 | /** |
||
628 | * The form is used to generate a form schema, |
||
629 | * as well as an intermediary object to process data through API endpoints. |
||
630 | * Since it's used directly on API endpoints, it does not have any form actions. |
||
631 | * It handles both {@link File} and {@link Folder} records. |
||
632 | * |
||
633 | * @param int $id |
||
634 | * @return Form |
||
635 | */ |
||
636 | public function getFileInsertForm($id) |
||
640 | |||
641 | /** |
||
642 | * Get file insert media form |
||
643 | * |
||
644 | * @param HTTPRequest $request |
||
645 | * @return Form |
||
646 | */ |
||
647 | View Code Duplication | public function fileInsertForm($request = null) |
|
661 | |||
662 | /** |
||
663 | * The form used to generate a form schema, since it's used directly on API endpoints, |
||
664 | * it does not have any form actions. |
||
665 | * |
||
666 | * @param $id |
||
667 | * @return Form |
||
668 | */ |
||
669 | public function getFileEditorLinkForm($id) |
||
673 | |||
674 | /** |
||
675 | * Get the file insert link form |
||
676 | * |
||
677 | * @param HTTPRequest $request |
||
678 | * @return Form |
||
679 | */ |
||
680 | View Code Duplication | public function fileEditorLinkForm($request = null) |
|
694 | |||
695 | /** |
||
696 | * Abstract method for generating a form for a file |
||
697 | * |
||
698 | * @param int $id Record ID |
||
699 | * @param string $name Form name |
||
700 | * @param array $context Form context |
||
701 | * @return Form |
||
702 | */ |
||
703 | protected function getAbstractFileForm($id, $name, $context = []) |
||
742 | |||
743 | /** |
||
744 | * Get form for selecting a file |
||
745 | * |
||
746 | * @return Form |
||
747 | */ |
||
748 | public function fileSelectForm() |
||
755 | |||
756 | /** |
||
757 | * Get form for selecting a file |
||
758 | * |
||
759 | * @param int $id ID of the record being selected |
||
760 | * @return Form |
||
761 | */ |
||
762 | public function getFileSelectForm($id) |
||
766 | |||
767 | /** |
||
768 | * @param array $context |
||
769 | * @return Form |
||
770 | * @throws InvalidArgumentException |
||
771 | */ |
||
772 | public function getFileHistoryForm($context) |
||
819 | |||
820 | /** |
||
821 | * Gets a JSON schema representing the current edit form. |
||
822 | * |
||
823 | * WARNING: Experimental API. |
||
824 | * |
||
825 | * @param HTTPRequest $request |
||
826 | * @return HTTPResponse |
||
827 | */ |
||
828 | public function schema($request) |
||
851 | |||
852 | /** |
||
853 | * Get file history form |
||
854 | * |
||
855 | * @param HTTPRequest $request |
||
856 | * @return Form |
||
857 | */ |
||
858 | public function fileHistoryForm($request = null) |
||
881 | |||
882 | /** |
||
883 | * @param array $data |
||
884 | * @param Form $form |
||
885 | * @return HTTPResponse |
||
886 | */ |
||
887 | public function createfolder($data, $form) |
||
922 | |||
923 | /** |
||
924 | * @param array $data |
||
925 | * @param Form $form |
||
926 | * @return HTTPResponse |
||
927 | */ |
||
928 | public function save($data, $form) |
||
932 | |||
933 | /** |
||
934 | * @param array $data |
||
935 | * @param Form $form |
||
936 | * @return HTTPResponse |
||
937 | */ |
||
938 | public function publish($data, $form) |
||
942 | |||
943 | /** |
||
944 | * Update thisrecord |
||
945 | * |
||
946 | * @param array $data |
||
947 | * @param Form $form |
||
948 | * @param bool $doPublish |
||
949 | * @return HTTPResponse |
||
950 | */ |
||
951 | protected function saveOrPublish($data, $form, $doPublish = false) |
||
1000 | |||
1001 | public function unpublish($data, $form) |
||
1025 | |||
1026 | /** |
||
1027 | * @param File $file |
||
1028 | * |
||
1029 | * @return array |
||
1030 | */ |
||
1031 | public function getObjectFromData(File $file) |
||
1101 | |||
1102 | /** |
||
1103 | * Action handler for adding pages to a campaign |
||
1104 | * |
||
1105 | * @param array $data |
||
1106 | * @param Form $form |
||
1107 | * @return DBHTMLText|HTTPResponse |
||
1108 | */ |
||
1109 | public function addtocampaign($data, $form) |
||
1125 | |||
1126 | /** |
||
1127 | * Url handler for add to campaign form |
||
1128 | * |
||
1129 | * @param HTTPRequest $request |
||
1130 | * @return Form |
||
1131 | */ |
||
1132 | public function addToCampaignForm($request) |
||
1138 | |||
1139 | /** |
||
1140 | * @param int $id |
||
1141 | * @return Form |
||
1142 | */ |
||
1143 | public function getAddToCampaignForm($id) |
||
1177 | |||
1178 | /** |
||
1179 | * @return Upload |
||
1180 | */ |
||
1181 | protected function getUpload() |
||
1191 | |||
1192 | /** |
||
1193 | * Get response for successfully updated record |
||
1194 | * |
||
1195 | * @param File $record |
||
1196 | * @param Form $form |
||
1197 | * @return HTTPResponse |
||
1198 | */ |
||
1199 | protected function getRecordUpdatedResponse($record, $form) |
||
1206 | |||
1207 | /** |
||
1208 | * @param HTTPRequest $request |
||
1209 | * @return Form |
||
1210 | */ |
||
1211 | public function folderCreateForm($request = null) |
||
1226 | |||
1227 | /** |
||
1228 | * Returns the form to be used for creating a new folder |
||
1229 | * @param $parentId |
||
1230 | * @return Form |
||
1231 | */ |
||
1232 | public function getFolderCreateForm($parentId = 0) |
||
1245 | |||
1246 | /** |
||
1247 | * Scaffold a search form. |
||
1248 | * Note: This form does not submit to itself, but rather uses the apiReadFolder endpoint |
||
1249 | * (to be replaced with graphql) |
||
1250 | * |
||
1251 | * @return Form |
||
1252 | */ |
||
1253 | public function fileSearchForm() |
||
1258 | |||
1259 | /** |
||
1260 | * Allow search form to be accessible to schema |
||
1261 | * |
||
1262 | * @return Form |
||
1263 | */ |
||
1264 | public function getFileSearchform() |
||
1268 | } |
||
1269 |
This check marks private properties in classes that are never used. Those properties can be removed.