Complex classes like AnyPriceRoundUpDonationModifier 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 AnyPriceRoundUpDonationModifier, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class AnyPriceRoundUpDonationModifier extends OrderModifier |
||
|
|
|||
| 16 | { |
||
| 17 | |||
| 18 | // ######################################## *** model defining static variables (e.g. $db, $has_one) |
||
| 19 | |||
| 20 | /** |
||
| 21 | * add extra fields as you need them. |
||
| 22 | * |
||
| 23 | **/ |
||
| 24 | private static $db = array( |
||
| 25 | "ModifierTotalExcludingDonation" => "Currency", |
||
| 26 | "SubTotal" => "Currency", |
||
| 27 | "OtherValue" => "Currency", |
||
| 28 | "AddDonation" => "Boolean" |
||
| 29 | ); |
||
| 30 | |||
| 31 | // ######################################## *** cms variables + functions (e.g. getCMSFields, $searchableFields) |
||
| 32 | |||
| 33 | public function getCMSFields() |
||
| 38 | |||
| 39 | private static $singular_name = "Round Up Donation"; |
||
| 40 | public function i18n_singular_name() |
||
| 44 | |||
| 45 | private static $plural_name = "Round Up Donations"; |
||
| 46 | public function i18n_plural_name() |
||
| 50 | |||
| 51 | // ######################################## *** other (non) static variables (e.g. private static $special_name_for_something, protected $order) |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Maximum Round Up |
||
| 56 | * to which the donation should round. |
||
| 57 | * +1 = nearest 10, e.g. 73.45 rounds to 80 |
||
| 58 | * 0 = nearest rounded integer - e.g. 73.45 rounds to 74 |
||
| 59 | * -1 = nearest 10 cents - e.g. 73.45 rounds to 73.50 |
||
| 60 | * |
||
| 61 | * @var Int |
||
| 62 | */ |
||
| 63 | private static $precision = 1; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Maximum Round Up - modifier will ensure that the round up is no more |
||
| 67 | * than the number specified here. |
||
| 68 | * @var Int |
||
| 69 | */ |
||
| 70 | private static $maximum_round_up = 5; |
||
| 71 | |||
| 72 | private static $round_up_even_if_there_is_nothing_to_round = true; |
||
| 73 | |||
| 74 | private static $include_form_in_order_table = true; |
||
| 75 | |||
| 76 | // ######################################## *** CRUD functions (e.g. canEdit) |
||
| 77 | // ######################################## *** init and update functions |
||
| 78 | |||
| 79 | /** |
||
| 80 | * For all modifers with their own database fields, we need to include this... |
||
| 81 | * It will update each of the fields. |
||
| 82 | * With this, we also need to create the methods |
||
| 83 | * Live{functionName} |
||
| 84 | * e.g LiveMyField() and LiveMyReduction() in this case... |
||
| 85 | * @param Bool $force - run it, even if it has run already |
||
| 86 | */ |
||
| 87 | public function runUpdate($force = false) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * allows you to save a new value AddDonation |
||
| 98 | * @param Boolean $b |
||
| 99 | */ |
||
| 100 | public function updateAddDonation($b) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * allows you to save a new value OtherValue |
||
| 108 | * @param float |
||
| 109 | */ |
||
| 110 | public function updateOtherValue($f) |
||
| 115 | |||
| 116 | |||
| 117 | // ######################################## *** form functions (e. g. Showform and getform) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * standard OrderModifier Method |
||
| 121 | * Should we show a form in the checkout page for this modifier? |
||
| 122 | */ |
||
| 123 | public function ShowForm() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Should the form be included in the editable form |
||
| 136 | * on the checkout page? |
||
| 137 | * @return Boolean |
||
| 138 | */ |
||
| 139 | public function ShowFormInEditableOrderTable() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * standard OrderModifier Method |
||
| 146 | * This method returns the form for the checkout page. |
||
| 147 | * @param Object $controller = Controller object for form |
||
| 148 | * @return Object - AnyPriceRoundUpDonationModifier |
||
| 149 | */ |
||
| 150 | public function getModifierForm(Controller $optionalController = null, Validator $optionalValidator = null) |
||
| 176 | |||
| 177 | // ######################################## *** template functions (e.g. ShowInTable, TableTitle, etc...) ... USES DB VALUES |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * This has to be set to true, because it can be added by form using AJAX. |
||
| 182 | * @return Boolean |
||
| 183 | */ |
||
| 184 | public function ShowInTable() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Removed via form instead. |
||
| 194 | * @return Boolean |
||
| 195 | */ |
||
| 196 | public function CanBeRemoved() |
||
| 200 | |||
| 201 | // ######################################## *** inner calculations.... USES CALCULATED VALUES |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * Works out if there is a donation at all. |
||
| 206 | * |
||
| 207 | *@return Boolean |
||
| 208 | */ |
||
| 209 | protected function hasDonation() |
||
| 216 | /** |
||
| 217 | * Works out the total round up amount, using both the |
||
| 218 | * sub-total and the modifier total. |
||
| 219 | * |
||
| 220 | *@return Float |
||
| 221 | */ |
||
| 222 | protected function workOutRoundUpAmount() |
||
| 245 | |||
| 246 | |||
| 247 | // ######################################## *** calculate database fields: protected function Live[field name] ... USES CALCULATED VALUES |
||
| 248 | |||
| 249 | /** |
||
| 250 | * if we want to change the default value for the Name field |
||
| 251 | * (defined in the OrderModifer class) then we can do this |
||
| 252 | * as shown in the method below. |
||
| 253 | * You may choose to return an empty string or just a standard message. |
||
| 254 | * |
||
| 255 | * |
||
| 256 | **/ |
||
| 257 | protected function LiveName() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * |
||
| 270 | * @return Boolean |
||
| 271 | **/ |
||
| 272 | protected function LiveAddDonation() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * |
||
| 279 | * @return Float |
||
| 280 | **/ |
||
| 281 | protected function LiveOtherValue() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Work out sub total amount for order |
||
| 288 | * @return float |
||
| 289 | **/ |
||
| 290 | protected function LiveSubTotal() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Work out modifier total excluding donation |
||
| 302 | * @return float |
||
| 303 | **/ |
||
| 304 | protected function LiveModifierTotalExcludingDonation() |
||
| 328 | |||
| 329 | protected function LiveCalculatedTotal() |
||
| 337 | |||
| 338 | public function LiveTableValue() |
||
| 342 | |||
| 343 | |||
| 344 | // ######################################## *** Type Functions (IsChargeable, IsDeductable, IsNoChange, IsRemoved) |
||
| 345 | |||
| 346 | private static $table_sub_title; |
||
| 347 | |||
| 348 | public function getTableSubTitle() |
||
| 352 | |||
| 353 | // ######################################## *** standard database related functions (e.g. onBeforeWrite, onAfterWrite, etc...) |
||
| 354 | |||
| 355 | public function onBeforeWrite() |
||
| 359 | |||
| 360 | |||
| 361 | // ######################################## *** AJAX related functions |
||
| 362 | /** |
||
| 363 | * some modifiers can be hidden after an ajax update (e.g. if someone enters a discount coupon and it does not exist). |
||
| 364 | * There might be instances where ShowInTable (the starting point) is TRUE and HideInAjaxUpdate return false. |
||
| 365 | *@return Boolean |
||
| 366 | **/ |
||
| 367 | public function HideInAjaxUpdate() |
||
| 380 | // ######################################## *** debug functions |
||
| 381 | } |
||
| 382 | |||
| 422 |
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.