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 ElectronicDelivery_OrderLog 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 ElectronicDelivery_OrderLog, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class ElectronicDelivery_OrderLog extends OrderStatusLog |
||
|
|
|||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Use for debugging |
||
| 17 | * uses debug::log |
||
| 18 | * @boolean |
||
| 19 | */ |
||
| 20 | private $debug = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Standard SS variable |
||
| 24 | */ |
||
| 25 | private static $db = array( |
||
| 26 | "FolderName" => "Varchar(255)", |
||
| 27 | "Completed" => "Boolean", |
||
| 28 | "NumberOfHoursBeforeDownloadGetsDeleted" => "Float" |
||
| 29 | ); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Standard SS variable |
||
| 33 | */ |
||
| 34 | private static $many_many = array( |
||
| 35 | "Files" => "File" |
||
| 36 | ); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Standard SS variable |
||
| 40 | */ |
||
| 41 | private static $summary_fields = array( |
||
| 42 | "Created" => "Date", |
||
| 43 | "Type" => "Type", |
||
| 44 | "Title" => "Title", |
||
| 45 | "FolderName" => "Folder" |
||
| 46 | ); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Standard SS variable |
||
| 50 | */ |
||
| 51 | private static $defaults = array( |
||
| 52 | "InternalUseOnly" => false, |
||
| 53 | "Completed" => false |
||
| 54 | ); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Set the default: the files are not ready yet! |
||
| 58 | * Standard SS method |
||
| 59 | */ |
||
| 60 | public function populateDefaults() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * |
||
| 68 | * @return Boolean |
||
| 69 | **/ |
||
| 70 | public function canDelete($member = null) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * |
||
| 77 | * @return Boolean |
||
| 78 | */ |
||
| 79 | public function canCreate($member = null) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | * @return Boolean |
||
| 87 | **/ |
||
| 88 | public function canEdit($member = null) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Standard SS var |
||
| 95 | * @var Array |
||
| 96 | */ |
||
| 97 | private static $searchable_fields = array( |
||
| 98 | 'OrderID' => array( |
||
| 99 | 'field' => 'NumericField', |
||
| 100 | 'title' => 'Order Number' |
||
| 101 | ), |
||
| 102 | "Title" => "PartialMatchFilter", |
||
| 103 | "Note" => "PartialMatchFilter", |
||
| 104 | "FolderName" => "PartialMatchFilter" |
||
| 105 | ); |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Standard SS var |
||
| 110 | * @var String |
||
| 111 | */ |
||
| 112 | private static $singular_name = "Electronic Delivery Details for one Order"; |
||
| 113 | public function i18n_singular_name() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Standard SS var |
||
| 120 | * @var String |
||
| 121 | */ |
||
| 122 | private static $plural_name = "Electronic Deliveries Detail for many Orders"; |
||
| 123 | public function i18n_plural_name() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Standard SS var |
||
| 130 | * @var String |
||
| 131 | */ |
||
| 132 | private static $default_sort = "\"Created\" DESC"; |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Size of the folder name (recommended to be at least 5+) |
||
| 137 | * @var Int |
||
| 138 | */ |
||
| 139 | private static $random_folder_name_character_count = 12; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * if set to anything except an empty string, |
||
| 143 | * an .htaccess file will be added to the download folder |
||
| 144 | * with the content of the variable |
||
| 145 | * content idea: Options -Indexes (stops directly from listing folders) |
||
| 146 | * @var String |
||
| 147 | */ |
||
| 148 | private static $htaccess_content = ""; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * List of files to be ignored when searching for files in the folder |
||
| 152 | * This may allow you to add "hidden" files or ignore other files. |
||
| 153 | * Can be added as |
||
| 154 | * 1 => mypng.png |
||
| 155 | * 2 => mysecondImage.jpg |
||
| 156 | * |
||
| 157 | * @var Array |
||
| 158 | */ |
||
| 159 | private static $files_to_be_excluded = array(); |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Permissions on download folders |
||
| 163 | * if not set, it will use: |
||
| 164 | * Config::inst()->get('Filesystem', 'folder_create_mask') |
||
| 165 | * @var string |
||
| 166 | */ |
||
| 167 | private static $permissions_on_folder = ""; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var String $order_dir - the root folder for the place where the files for the order are saved. |
||
| 171 | * if the variable is equal to downloads then the downloads URL is www.mysite.com/downloads/ |
||
| 172 | */ |
||
| 173 | private static $order_dir = '_downloads'; |
||
| 174 | |||
| 175 | public function getCMSFields() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Adds the download files to the Log and makes them available for download. |
||
| 184 | * @param ArrayList | Null $dosWithFiles - Data Object Set with files |
||
| 185 | */ |
||
| 186 | public function AddFiles($listOfFiles) |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * checks if the download has expired (i.e. too much time has passed) |
||
| 241 | * @return Boolean |
||
| 242 | */ |
||
| 243 | public function IsExpired() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Standard SS method |
||
| 256 | * Creates the folder and files. |
||
| 257 | */ |
||
| 258 | public function onBeforeWrite() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Standard SS method |
||
| 268 | * If it has expired, then the folder is deleted... |
||
| 269 | */ |
||
| 270 | public function onAfterWrite() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * making sure we dont end up in an infinite loop |
||
| 278 | * @var int |
||
| 279 | */ |
||
| 280 | private $loopEscape = 0; |
||
| 281 | |||
| 282 | public function deleteFolderIfExpired() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Standard SS method |
||
| 303 | * Deletes the files in the download folder, |
||
| 304 | * and the actual download folder itself. |
||
| 305 | */ |
||
| 306 | public function onBeforeDelete() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * returns the list of files that are in the current folder |
||
| 316 | * @return Array |
||
| 317 | */ |
||
| 318 | protected function getFilesInFolder() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * creates a folder and returns the full folder path |
||
| 329 | * if the folder is already created it still returns the folder path, |
||
| 330 | * but it does not create the folder. |
||
| 331 | * |
||
| 332 | * @param Boolean $absolutePath |
||
| 333 | * |
||
| 334 | * @return NULL | String |
||
| 335 | */ |
||
| 336 | protected function getOrderDownloadFolder($absolutePath = true) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * returns the folder in which all the downloads are kept |
||
| 368 | * (each order has an individual folder within this base folder) |
||
| 369 | * returns location of base folder. |
||
| 370 | * |
||
| 371 | * @param Boolean $absolutePath - absolute folder path (set to false to get relative path) |
||
| 372 | * |
||
| 373 | * @return NULL | String |
||
| 374 | */ |
||
| 375 | protected function getBaseFolder($absolutePath = true) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * returns the permissions for the folder to be created. |
||
| 409 | * @return String |
||
| 410 | */ |
||
| 411 | protected function getFolderPermissions() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * get folder contents |
||
| 418 | * |
||
| 419 | * @param String $fullPath (e.g. /var/www/mysite.co.nz/downloads) |
||
| 420 | * @param Boolean $showFiles - list the files in the directory? |
||
| 421 | * @param Boolean $showFolders - list the folders in the directory? |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | protected function getDirectoryContents($fullPath, $showFiles = false, $showFolders = false) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * remove all the folder contents and remove the folder itself |
||
| 449 | * as well... Returns true on success. |
||
| 450 | * Assumes that there are no folders in the folder... |
||
| 451 | * |
||
| 452 | * @return Boolean |
||
| 453 | */ |
||
| 454 | protected function deleteFolderContents() |
||
| 469 | } |
||
| 470 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.