| Conditions | 19 |
| Paths | 288 |
| Total Lines | 154 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 9 | public function __construct($controller, $name, $repeatOrderID = 0, $originatingOrder = 0) |
||
| 10 | { |
||
| 11 | $order = null; |
||
| 12 | //create vs edit |
||
| 13 | if ($repeatOrderID) { |
||
| 14 | $repeatOrder = DataObject::get_one('RepeatOrder', "ID = ".$repeatOrderID); |
||
| 15 | $items = $repeatOrder->OrderItems(); |
||
| 16 | } else { |
||
| 17 | $repeatOrder = null; |
||
| 18 | if ($originatingOrder) { |
||
| 19 | $order = Order::get_by_id_if_can_view($originatingOrder); |
||
| 20 | } |
||
| 21 | if (!$order) { |
||
| 22 | $order = ShoppingCart::current_order(); |
||
| 23 | } |
||
| 24 | if ($order) { |
||
| 25 | $items = $order->Items(); |
||
| 26 | } else { |
||
| 27 | $items = null; |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | //build fields |
||
| 32 | $fields = FieldList::create(); |
||
| 33 | |||
| 34 | //products! |
||
| 35 | if ($items) { |
||
| 36 | $fields->push(HeaderField::create('ProductsHeader', 'Products')); |
||
| 37 | $products = Product::get()->filter(["AllowPurchase" => 1]); |
||
| 38 | $productsMap = $products->map('ID', 'Title')->toArray(); |
||
| 39 | $this->array_unshift_assoc($productsMap, 0, "--- Please select ---"); |
||
| 40 | foreach ($productsMap as $id => $title) { |
||
| 41 | if ($product = Product::get()->byID($id)) { |
||
| 42 | if (!$product->canPurchase()) { |
||
| 43 | unset($productsMap[$id]); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | $j = 0; |
||
| 48 | foreach ($items as $key => $item) { |
||
| 49 | $j++; |
||
| 50 | $alternativeItemsMap = $productsMap; |
||
| 51 | $defaultProductID = $item->ProductID ? $item->ProductID : $item->BuyableID; |
||
| 52 | $itemID = $defaultProductID; |
||
| 53 | unset($alternativeItemsMap[$defaultProductID]); |
||
| 54 | $fields->push( |
||
| 55 | DropdownField::create( |
||
| 56 | 'Product[ID]['.$itemID.']', |
||
| 57 | "Preferred Product #$j", |
||
| 58 | $productsMap, |
||
| 59 | $defaultProductID |
||
| 60 | ) |
||
| 61 | ); |
||
| 62 | $fields->push( |
||
| 63 | NumericField::create( |
||
| 64 | 'Product[Quantity]['.$itemID.']', |
||
| 65 | " ... quantity", |
||
| 66 | $item->Quantity |
||
| 67 | ) |
||
| 68 | ); |
||
| 69 | $altCount = Config::inst()->get('RepeatOrderForm', 'number_of_product_alternatives'); |
||
| 70 | if($altCount > 9) { |
||
| 71 | user_error('You can only have up to nine alternatives buyables'); |
||
| 72 | } elseif($altCount > 0) { |
||
| 73 | for ($i = 1; $i <= $altCount; $i++) { |
||
| 74 | $alternativeField = "Alternative".$i."ID"; |
||
| 75 | $fields->push( |
||
| 76 | DropdownField::create( |
||
| 77 | 'Product['.$alternativeField.']['.$itemID.']', |
||
| 78 | " ... alternative $i", |
||
| 79 | $alternativeItemsMap, |
||
| 80 | (isset($item->$alternativeField) ? $item->$alternativeField : 0) |
||
| 81 | ) |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } else { |
||
| 87 | $fields->push(HeaderField::create('items', 'There are no products in this repeating order')); |
||
| 88 | } |
||
| 89 | |||
| 90 | //other details |
||
| 91 | $fields->push( |
||
| 92 | HeaderField::create( |
||
| 93 | 'DetailsHeader', |
||
| 94 | 'Repeat Order Details' |
||
| 95 | ) |
||
| 96 | ); |
||
| 97 | $fields->push( |
||
| 98 | DropdownField::create( |
||
| 99 | 'PaymentMethod', |
||
| 100 | 'Payment Method', |
||
| 101 | Config::inst()->get('RepeatOrder', 'payment_methods') |
||
| 102 | ) |
||
| 103 | ); |
||
| 104 | $startField = DateField::create('Start', 'Start Date'); |
||
| 105 | $startField->setConfig('showcalendar', true); |
||
| 106 | $fields->push($startField); |
||
| 107 | |||
| 108 | $endField = DateField::create('End', 'End Date'); |
||
| 109 | $endField->setConfig('showcalendar', true); |
||
| 110 | $fields->push($endField); |
||
| 111 | |||
| 112 | $fields->push( |
||
| 113 | DropdownField::create( |
||
| 114 | 'Period', |
||
| 115 | 'Period', |
||
| 116 | Config::inst()->get('RepeatOrder', 'period_fields') |
||
| 117 | ) |
||
| 118 | ); |
||
| 119 | $fields->push( |
||
| 120 | TextareaField::create('Notes', 'Notes') |
||
| 121 | ); |
||
| 122 | |||
| 123 | //hidden field |
||
| 124 | if (isset($order->ID)) { |
||
| 125 | $fields->push( |
||
| 126 | HiddenField::create('OrderID', 'OrderID', $order->ID) |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | if ($repeatOrder) { |
||
| 130 | $fields->push( |
||
| 131 | HiddenField::create('RepeatOrderID', 'RepeatOrderID', $repeatOrder->ID) |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | //actions |
||
| 136 | $actions = FieldList::create(); |
||
| 137 | if ($repeatOrder) { |
||
| 138 | $actions->push(FormAction::create('doSave', 'Save')); |
||
| 139 | } else { |
||
| 140 | $actions->push(FormAction::create('doCreate', 'Create')); |
||
| 141 | } |
||
| 142 | |||
| 143 | //required fields |
||
| 144 | $requiredArray = array('Start', 'End', 'Period'); |
||
| 145 | $requiredFields = RequiredFields::create($requiredArray); |
||
| 146 | |||
| 147 | //make form |
||
| 148 | parent::__construct($controller, $name, $fields, $actions, $requiredFields); |
||
| 149 | |||
| 150 | //load data |
||
| 151 | if ($repeatOrder) { |
||
| 152 | $this->loadDataFrom( |
||
| 153 | [ |
||
| 154 | 'Start' => $repeatOrder->Start, |
||
| 155 | 'End' => $repeatOrder->End, |
||
| 156 | 'Period' => $repeatOrder->Period, |
||
| 157 | 'Notes' => $repeatOrder->Notes, |
||
| 158 | 'PaymentMethod' => $repeatOrder->PaymentMethod, |
||
| 159 | ] |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 291 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.