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