Complex classes like UploadField 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 UploadField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class UploadField extends FormField |
||
| 49 | { |
||
| 50 | use FileUploadReceiver; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private static $allowed_actions = array( |
||
| 56 | 'upload', |
||
| 57 | 'attach', |
||
| 58 | 'handleItem', |
||
| 59 | 'handleSelect', |
||
| 60 | 'fileexists' |
||
| 61 | ); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private static $url_handlers = array( |
||
| 67 | 'item/$ID' => 'handleItem', |
||
| 68 | 'select' => 'handleSelect', |
||
| 69 | '$Action!' => '$Action', |
||
| 70 | ); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Template to use for the file button widget |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $templateFileButtons = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Template to use for the edit form |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $templateFileEdit = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Config for this field used in the front-end javascript |
||
| 88 | * (will be merged into the config of the javascript file upload plugin). |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $ufConfig = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Front end config defaults |
||
| 96 | * |
||
| 97 | * @config |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private static $defaultConfig = array( |
||
| 101 | /** |
||
| 102 | * Automatically upload the file once selected |
||
| 103 | * |
||
| 104 | * @var boolean |
||
| 105 | */ |
||
| 106 | 'autoUpload' => true, |
||
| 107 | /** |
||
| 108 | * Restriction on number of files that may be set for this field. Set to null to allow |
||
| 109 | * unlimited. If record has a has_one and allowedMaxFileNumber is null, it will be set to 1. |
||
| 110 | * The resulting value will be set to maxNumberOfFiles |
||
| 111 | * |
||
| 112 | * @var integer |
||
| 113 | */ |
||
| 114 | 'allowedMaxFileNumber' => null, |
||
| 115 | /** |
||
| 116 | * Can the user upload new files, or just select from existing files. |
||
| 117 | * String values are interpreted as permission codes. |
||
| 118 | * |
||
| 119 | * @var boolean|string |
||
| 120 | */ |
||
| 121 | 'canUpload' => true, |
||
| 122 | /** |
||
| 123 | * Can the user attach files from the assets archive on the site? |
||
| 124 | * String values are interpreted as permission codes. |
||
| 125 | * |
||
| 126 | * @var boolean|string |
||
| 127 | */ |
||
| 128 | 'canAttachExisting' => "CMS_ACCESS_AssetAdmin", |
||
| 129 | /** |
||
| 130 | * Shows the target folder for new uploads in the field UI. |
||
| 131 | * Disable to keep the internal filesystem structure hidden from users. |
||
| 132 | * |
||
| 133 | * @var boolean|string |
||
| 134 | */ |
||
| 135 | 'canPreviewFolder' => true, |
||
| 136 | /** |
||
| 137 | * Indicate a change event to the containing form if an upload |
||
| 138 | * or file edit/delete was performed. |
||
| 139 | * |
||
| 140 | * @var boolean |
||
| 141 | */ |
||
| 142 | 'changeDetection' => true, |
||
| 143 | /** |
||
| 144 | * Maximum width of the preview thumbnail |
||
| 145 | * |
||
| 146 | * @var integer |
||
| 147 | */ |
||
| 148 | 'previewMaxWidth' => 80, |
||
| 149 | /** |
||
| 150 | * Maximum height of the preview thumbnail |
||
| 151 | * |
||
| 152 | * @var integer |
||
| 153 | */ |
||
| 154 | 'previewMaxHeight' => 60, |
||
| 155 | /** |
||
| 156 | * javascript template used to display uploading files |
||
| 157 | * |
||
| 158 | * @see javascript/UploadField_uploadtemplate.js |
||
| 159 | * @var string |
||
| 160 | */ |
||
| 161 | 'uploadTemplateName' => 'ss-uploadfield-uploadtemplate', |
||
| 162 | /** |
||
| 163 | * javascript template used to display already uploaded files |
||
| 164 | * |
||
| 165 | * @see javascript/UploadField_downloadtemplate.js |
||
| 166 | * @var string |
||
| 167 | */ |
||
| 168 | 'downloadTemplateName' => 'ss-uploadfield-downloadtemplate', |
||
| 169 | /** |
||
| 170 | * Show a warning when overwriting a file. |
||
| 171 | * This requires Upload->replaceFile config to be set to true, otherwise |
||
| 172 | * files will be renamed instead of overwritten |
||
| 173 | * |
||
| 174 | * @see Upload |
||
| 175 | * @var boolean |
||
| 176 | */ |
||
| 177 | 'overwriteWarning' => true |
||
| 178 | ); |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var String Folder to display in "Select files" list. |
||
| 182 | * Defaults to listing all files regardless of folder. |
||
| 183 | * The folder path should be relative to the webroot. |
||
| 184 | * See {@link FileField->folderName} to set the upload target instead. |
||
| 185 | * @example admin/folder/subfolder |
||
| 186 | */ |
||
| 187 | protected $displayFolderName; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * FieldList $fields or string $name (of a method on File to provide a fields) for the EditForm |
||
| 191 | * @example 'getCMSFields' |
||
| 192 | * |
||
| 193 | * @var FieldList|string |
||
| 194 | */ |
||
| 195 | protected $fileEditFields = null; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * FieldList $actions or string $name (of a method on File to provide a actions) for the EditForm |
||
| 199 | * @example 'getCMSActions' |
||
| 200 | * |
||
| 201 | * @var FieldList|string |
||
| 202 | */ |
||
| 203 | protected $fileEditActions = null; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Validator (eg RequiredFields) or string $name (of a method on File to provide a Validator) for the EditForm |
||
| 207 | * @example 'getCMSValidator' |
||
| 208 | * |
||
| 209 | * @var RequiredFields|string |
||
| 210 | */ |
||
| 211 | protected $fileEditValidator = null; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Construct a new UploadField instance |
||
| 215 | * |
||
| 216 | * @param string $name The internal field name, passed to forms. |
||
| 217 | * @param string $title The field label. |
||
| 218 | * @param SS_List $items If no items are defined, the field will try to auto-detect an existing relation on |
||
| 219 | * @link $record}, with the same name as the field name. |
||
| 220 | */ |
||
| 221 | public function __construct($name, $title = null, SS_List $items = null) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set name of template used for Buttons on each file (replace, edit, remove, delete) (without path or extension) |
||
| 239 | * |
||
| 240 | * @param string $template |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | public function setTemplateFileButtons($template) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function getTemplateFileButtons() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Set name of template used for the edit (inline & popup) of a file file (without path or extension) |
||
| 259 | * |
||
| 260 | * @param string $template |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | public function setTemplateFileEdit($template) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function getTemplateFileEdit() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Determine if the target folder for new uploads in is visible the field UI. |
||
| 279 | * |
||
| 280 | * @return boolean |
||
| 281 | */ |
||
| 282 | public function canPreviewFolder() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Determine if the target folder for new uploads in is visible the field UI. |
||
| 293 | * Disable to keep the internal filesystem structure hidden from users. |
||
| 294 | * |
||
| 295 | * @param boolean|string $canPreviewFolder Either a boolean flag, or a |
||
| 296 | * required permission code |
||
| 297 | * @return UploadField Self reference |
||
| 298 | */ |
||
| 299 | public function setCanPreviewFolder($canPreviewFolder) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Determine if the field should show a warning when overwriting a file. |
||
| 306 | * This requires Upload->replaceFile config to be set to true, otherwise |
||
| 307 | * files will be renamed instead of overwritten (although the warning will |
||
| 308 | * still be displayed) |
||
| 309 | * |
||
| 310 | * @return boolean |
||
| 311 | */ |
||
| 312 | public function getOverwriteWarning() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Determine if the field should show a warning when overwriting a file. |
||
| 319 | * This requires Upload->replaceFile config to be set to true, otherwise |
||
| 320 | * files will be renamed instead of overwritten (although the warning will |
||
| 321 | * still be displayed) |
||
| 322 | * |
||
| 323 | * @param boolean $overwriteWarning |
||
| 324 | * @return UploadField Self reference |
||
| 325 | */ |
||
| 326 | public function setOverwriteWarning($overwriteWarning) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $name |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function setDisplayFolderName($name) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return String |
||
| 343 | */ |
||
| 344 | public function getDisplayFolderName() |
||
| 348 | |||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * Retrieves a customised list of all File records to ensure they are |
||
| 353 | * properly viewable when rendered in the field template. |
||
| 354 | * |
||
| 355 | * @return SS_List[ViewableData_Customised] |
||
| 356 | */ |
||
| 357 | public function getCustomisedItems() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Customises a file with additional details suitable for rendering in the |
||
| 368 | * UploadField.ss template |
||
| 369 | * |
||
| 370 | * @param ViewableData|AssetContainer $file |
||
| 371 | * @return ViewableData_Customised |
||
| 372 | */ |
||
| 373 | protected function customiseFile(AssetContainer $file) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Assign a front-end config variable for the upload field |
||
| 389 | * |
||
| 390 | * @see https://github.com/blueimp/jQuery-File-Upload/wiki/Options for the list of front end options available |
||
| 391 | * |
||
| 392 | * @param string $key |
||
| 393 | * @param mixed $val |
||
| 394 | * @return UploadField self reference |
||
| 395 | */ |
||
| 396 | public function setConfig($key, $val) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Gets a front-end config variable for the upload field |
||
| 404 | * |
||
| 405 | * @see https://github.com/blueimp/jQuery-File-Upload/wiki/Options for the list of front end options available |
||
| 406 | * |
||
| 407 | * @param string $key |
||
| 408 | * @return mixed |
||
| 409 | */ |
||
| 410 | public function getConfig($key) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Determine if the field should automatically upload the file. |
||
| 420 | * |
||
| 421 | * @return boolean |
||
| 422 | */ |
||
| 423 | public function getAutoUpload() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Determine if the field should automatically upload the file |
||
| 430 | * |
||
| 431 | * @param boolean $autoUpload |
||
| 432 | * @return UploadField Self reference |
||
| 433 | */ |
||
| 434 | public function setAutoUpload($autoUpload) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Determine maximum number of files allowed to be attached |
||
| 441 | * Defaults to 1 for has_one and null (unlimited) for |
||
| 442 | * many_many and has_many relations. |
||
| 443 | * |
||
| 444 | * @return integer|null Maximum limit, or null for no limit |
||
| 445 | */ |
||
| 446 | public function getAllowedMaxFileNumber() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Determine maximum number of files allowed to be attached. |
||
| 467 | * |
||
| 468 | * @param integer|null $allowedMaxFileNumber Maximum limit. 0 or null will be treated as unlimited |
||
| 469 | * @return UploadField Self reference |
||
| 470 | */ |
||
| 471 | public function setAllowedMaxFileNumber($allowedMaxFileNumber) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Determine if the user has permission to upload. |
||
| 478 | * |
||
| 479 | * @return boolean |
||
| 480 | */ |
||
| 481 | public function canUpload() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Specify whether the user can upload files. |
||
| 492 | * String values will be treated as required permission codes |
||
| 493 | * |
||
| 494 | * @param boolean|string $canUpload Either a boolean flag, or a required |
||
| 495 | * permission code |
||
| 496 | * @return UploadField Self reference |
||
| 497 | */ |
||
| 498 | public function setCanUpload($canUpload) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Determine if the user has permission to attach existing files |
||
| 505 | * By default returns true if the user has the CMS_ACCESS_AssetAdmin permission |
||
| 506 | * |
||
| 507 | * @return boolean |
||
| 508 | */ |
||
| 509 | public function canAttachExisting() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Returns true if the field is neither readonly nor disabled |
||
| 520 | * |
||
| 521 | * @return boolean |
||
| 522 | */ |
||
| 523 | public function isActive() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Specify whether the user can attach existing files |
||
| 530 | * String values will be treated as required permission codes |
||
| 531 | * |
||
| 532 | * @param boolean|string $canAttachExisting Either a boolean flag, or a |
||
| 533 | * required permission code |
||
| 534 | * @return UploadField Self reference |
||
| 535 | */ |
||
| 536 | public function setCanAttachExisting($canAttachExisting) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Gets thumbnail width. Defaults to 80 |
||
| 543 | * |
||
| 544 | * @return integer |
||
| 545 | */ |
||
| 546 | public function getPreviewMaxWidth() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @see UploadField::getPreviewMaxWidth() |
||
| 553 | * |
||
| 554 | * @param integer $previewMaxWidth |
||
| 555 | * @return UploadField Self reference |
||
| 556 | */ |
||
| 557 | public function setPreviewMaxWidth($previewMaxWidth) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Gets thumbnail height. Defaults to 60 |
||
| 564 | * |
||
| 565 | * @return integer |
||
| 566 | */ |
||
| 567 | public function getPreviewMaxHeight() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @see UploadField::getPreviewMaxHeight() |
||
| 574 | * |
||
| 575 | * @param integer $previewMaxHeight |
||
| 576 | * @return UploadField Self reference |
||
| 577 | */ |
||
| 578 | public function setPreviewMaxHeight($previewMaxHeight) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * javascript template used to display uploading files |
||
| 585 | * Defaults to 'ss-uploadfield-uploadtemplate' |
||
| 586 | * |
||
| 587 | * @see javascript/UploadField_uploadtemplate.js |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | public function getUploadTemplateName() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @see UploadField::getUploadTemplateName() |
||
| 597 | * |
||
| 598 | * @param string $uploadTemplateName |
||
| 599 | * @return UploadField Self reference |
||
| 600 | */ |
||
| 601 | public function setUploadTemplateName($uploadTemplateName) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * javascript template used to display already uploaded files |
||
| 608 | * Defaults to 'ss-downloadfield-downloadtemplate' |
||
| 609 | * |
||
| 610 | * @see javascript/DownloadField_downloadtemplate.js |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | public function getDownloadTemplateName() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @see Uploadfield::getDownloadTemplateName() |
||
| 620 | * |
||
| 621 | * @param string $downloadTemplateName |
||
| 622 | * @return Uploadfield Self reference |
||
| 623 | */ |
||
| 624 | public function setDownloadTemplateName($downloadTemplateName) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * FieldList $fields for the EditForm |
||
| 631 | * @example 'getCMSFields' |
||
| 632 | * |
||
| 633 | * @param DataObject $file File context to generate fields for |
||
| 634 | * @return FieldList List of form fields |
||
| 635 | */ |
||
| 636 | public function getFileEditFields(DataObject $file) |
||
| 660 | |||
| 661 | /** |
||
| 662 | * FieldList $fields or string $name (of a method on File to provide a fields) for the EditForm |
||
| 663 | * @example 'getCMSFields' |
||
| 664 | * |
||
| 665 | * @param FieldList|string |
||
| 666 | * @return Uploadfield Self reference |
||
| 667 | */ |
||
| 668 | public function setFileEditFields($fileEditFields) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * FieldList $actions or string $name (of a method on File to provide a actions) for the EditForm |
||
| 676 | * @example 'getCMSActions' |
||
| 677 | * |
||
| 678 | * @param DataObject $file File context to generate form actions for |
||
| 679 | * @return FieldList Field list containing FormAction |
||
| 680 | */ |
||
| 681 | public function getFileEditActions(DataObject $file) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * FieldList $actions or string $name (of a method on File to provide a actions) for the EditForm |
||
| 705 | * @example 'getCMSActions' |
||
| 706 | * |
||
| 707 | * @param FieldList|string |
||
| 708 | * @return Uploadfield Self reference |
||
| 709 | */ |
||
| 710 | public function setFileEditActions($fileEditActions) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Determines the validator to use for the edit form |
||
| 718 | * @example 'getCMSValidator' |
||
| 719 | * |
||
| 720 | * @param DataObject $file File context to generate validator from |
||
| 721 | * @return Validator Validator object |
||
| 722 | */ |
||
| 723 | public function getFileEditValidator(DataObject $file) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Validator (eg RequiredFields) or string $name (of a method on File to provide a Validator) for the EditForm |
||
| 745 | * @example 'getCMSValidator' |
||
| 746 | * |
||
| 747 | * @param Validator|string |
||
| 748 | * @return Uploadfield Self reference |
||
| 749 | */ |
||
| 750 | public function setFileEditValidator($fileEditValidator) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * |
||
| 758 | * @param File|AssetContainer $file |
||
| 759 | * @return string URL to thumbnail |
||
| 760 | */ |
||
| 761 | protected function getThumbnailURLForFile(AssetContainer $file) |
||
| 786 | |||
| 787 | public function getAttributes() |
||
| 797 | |||
| 798 | public function extraClass() |
||
| 809 | |||
| 810 | public function Field($properties = array()) |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Validation method for this field, called when the entire form is validated |
||
| 876 | * |
||
| 877 | * @param Validator $validator |
||
| 878 | * @return boolean |
||
| 879 | */ |
||
| 880 | public function validate($validator) |
||
| 929 | |||
| 930 | /** |
||
| 931 | * @param HTTPRequest $request |
||
| 932 | * @return UploadField_ItemHandler |
||
| 933 | */ |
||
| 934 | public function handleItem(HTTPRequest $request) |
||
| 938 | |||
| 939 | /** |
||
| 940 | * @param int $itemID |
||
| 941 | * @return UploadField_ItemHandler |
||
| 942 | */ |
||
| 943 | public function getItemHandler($itemID) |
||
| 947 | |||
| 948 | /** |
||
| 949 | * @param HTTPRequest $request |
||
| 950 | * @return UploadField_SelectHandler |
||
| 951 | */ |
||
| 952 | public function handleSelect(HTTPRequest $request) |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Safely encodes the File object with all standard fields required |
||
| 962 | * by the front end |
||
| 963 | * |
||
| 964 | * @param File|AssetContainer $file Object which contains a file |
||
| 965 | * @return array Array encoded list of file attributes |
||
| 966 | */ |
||
| 967 | protected function encodeFileAttributes(AssetContainer $file) |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Action to handle upload of a single file |
||
| 986 | * |
||
| 987 | * @param HTTPRequest $request |
||
| 988 | * @return HTTPResponse |
||
| 989 | * @return HTTPResponse |
||
| 990 | */ |
||
| 991 | public function upload(HTTPRequest $request) |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Retrieves details for files that this field wishes to attache to the |
||
| 1031 | * client-side form |
||
| 1032 | * |
||
| 1033 | * @param HTTPRequest $request |
||
| 1034 | * @return HTTPResponse |
||
| 1035 | */ |
||
| 1036 | public function attach(HTTPRequest $request) |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Check if file exists, both checking filtered filename and exact filename |
||
| 1058 | * |
||
| 1059 | * @param string $originalFile Filename |
||
| 1060 | * @return bool |
||
| 1061 | */ |
||
| 1062 | protected function checkFileExists($originalFile) |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Determines if a specified file exists |
||
| 1080 | * |
||
| 1081 | * @param HTTPRequest $request |
||
| 1082 | * @return HTTPResponse |
||
| 1083 | */ |
||
| 1084 | public function fileexists(HTTPRequest $request) |
||
| 1106 | |||
| 1107 | public function performReadonlyTransformation() |
||
| 1114 | } |
||
| 1115 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.