| Conditions | 37 |
| Paths | 13 |
| Total Lines | 106 |
| Code Lines | 69 |
| Lines | 22 |
| Ratio | 20.75 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 102 | public static function apply_min_max() |
||
| 103 | { |
||
| 104 | if (self::$min_field || self::$max_field || self::$default_min_quantity || self::$default_max_quantity) { |
||
| 105 | $msgArray = array(); |
||
| 106 | $minFieldName = self::$min_field; |
||
| 107 | $maxFieldName = self::$max_field; |
||
| 108 | $currentOrder = ShoppingCart::current_order(); |
||
| 109 | if ($currentOrder->IsSubmitted()) { |
||
| 110 | //too late! |
||
| 111 | return; |
||
| 112 | } |
||
| 113 | $items = $currentOrder->Items(); |
||
| 114 | $i = 0; |
||
| 115 | if ($items) { |
||
| 116 | foreach ($items as $item) { |
||
| 117 | $buyable = $item->Buyable(); |
||
| 118 | if ($buyable) { |
||
| 119 | $quantity = $item->Quantity; |
||
| 120 | $absoluteMin = self::$default_min_quantity; |
||
| 121 | $absoluteMax = self::$default_max_quantity; |
||
| 122 | $parent = $buyable->Parent(); |
||
| 123 | View Code Duplication | if ($minFieldName) { |
|
| 124 | if (isset($buyable->$minFieldName) && $buyable->$minFieldName > 0) { |
||
| 125 | $absoluteMin = $buyable->$minFieldName; |
||
| 126 | } |
||
| 127 | //product variations |
||
| 128 | elseif (!isset($buyable->$minFieldName)) { |
||
| 129 | if ($parent && isset($parent->$minFieldName) && $parent->$minFieldName > 0) { |
||
| 130 | $absoluteMin = $parent->$minFieldName; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | View Code Duplication | if ($maxFieldName) { |
|
| 135 | if (isset($buyable->$maxFieldName) && $buyable->$maxFieldName > 0) { |
||
| 136 | $absoluteMax = $buyable->$maxFieldName; |
||
| 137 | } |
||
| 138 | //product variations |
||
| 139 | elseif (!isset($buyable->$maxFieldName)) { |
||
| 140 | if ($parent && isset($parent->$maxFieldName) && $parent->$maxFieldName > 0) { |
||
| 141 | $absoluteMax = $parent->$maxFieldName; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | if ($buyable->UnlimitedStock) { |
||
| 146 | //nothing more to do |
||
| 147 | } elseif (self::$use_stock_quantities) { |
||
| 148 | $maxStockQuantity = $buyable->getActualQuantity(); |
||
| 149 | if ($absoluteMax > $maxStockQuantity) { |
||
| 150 | $absoluteMax = $maxStockQuantity; |
||
| 151 | } |
||
| 152 | if ($absoluteMin > $maxStockQuantity) { |
||
| 153 | $absoluteMax = 0; |
||
| 154 | $maxStockQuantity = 0; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | $absoluteMin = intval($absoluteMin) - 0; |
||
| 158 | $absoluteMax = intval($absoluteMax) - 0; |
||
| 159 | $newValue = $quantity; |
||
| 160 | if ($quantity < $absoluteMin && $absoluteMin > 0) { |
||
| 161 | debug::log("adjusting for MIN: $quantity < $absoluteMin"); |
||
| 162 | $newValue = $absoluteMin; |
||
| 163 | } |
||
| 164 | if ($quantity > $absoluteMax && $absoluteMax > 0) { |
||
| 165 | debug::log("adjusting for MAX: $quantity > $absoluteMax"); |
||
| 166 | $newValue = $absoluteMax; |
||
| 167 | } |
||
| 168 | if ($quantity != $newValue) { |
||
| 169 | $item->Quantity = $newValue; |
||
| 170 | ShoppingCart::singleton()->setQuantity($buyable, $newValue); |
||
| 171 | $msgArray[$i] = $buyable->Title." changed from ".$quantity." to ".$newValue; |
||
| 172 | $i++; |
||
| 173 | $quantity = $newValue; |
||
| 174 | self::$ids_of_items_adjusted[$item->ID] = $item->ID; |
||
| 175 | } |
||
| 176 | if (Director::is_ajax()) { |
||
| 177 | //do nothing |
||
| 178 | } else { |
||
| 179 | //IS THIS WORKING? |
||
| 180 | $fieldName = $item->AJAXDefinitions()->QuantityFieldName(); |
||
| 181 | $js = ' |
||
| 182 | MinMaxModifierData = []; |
||
| 183 | MinMaxModifierData.push( |
||
| 184 | { |
||
| 185 | selector: "input[name=\''.$fieldName.'\']", |
||
| 186 | min: '.intval($absoluteMin).', |
||
| 187 | max: '.intval($absoluteMax).', |
||
| 188 | msg: "'.addslashes(self::$sorry_message).'" |
||
| 189 | } |
||
| 190 | );'; |
||
| 191 | Requirements::javascript("ecommerce_stockcontrol/javascript/MinMaxModifier.js"); |
||
| 192 | Requirements::customScript($js, $fieldName); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | if (count($msgArray)) { |
||
| 199 | if (self::$adjustment_message) { |
||
| 200 | $msg = self::$adjustment_message."\n".implode("\n", $msgArray); |
||
| 201 | if ($msg && !Director::is_ajax()) { |
||
| 202 | Requirements::customScript('alert("'.Convert::raw2js($msg).'");', "MinMaxModifierAlert"); |
||
| 203 | } |
||
| 204 | //$this->Adjustments = $msg; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 227 |
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.