Conditions | 23 |
Paths | 72 |
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 |
||
43 | public static function repeatOrderFormFields($repeatOrderID = 0, $originatingOrder = 0) |
||
44 | { |
||
45 | $order = null; |
||
46 | //create vs edit |
||
47 | if ($repeatOrderID) { |
||
48 | $repeatOrder = DataObject::get_one('RepeatOrder', "ID = ".$repeatOrderID); |
||
49 | $items = $repeatOrder->OrderItems(); |
||
50 | } else { |
||
51 | $repeatOrder = null; |
||
52 | if ($originatingOrder) { |
||
53 | $order = Order::get_by_id_if_can_view($originatingOrder); |
||
54 | } |
||
55 | if (!$order) { |
||
56 | $order = ShoppingCart::current_order(); |
||
57 | } |
||
58 | if ($order) { |
||
59 | $items = $order->Items(); |
||
60 | } else { |
||
61 | $items = null; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | //build fields |
||
66 | $fields = FieldList::create(); |
||
67 | |||
68 | //products! |
||
69 | if ($items) { |
||
70 | // $fields->push(HeaderField::create('ProductsHeader', 'Products')); |
||
71 | $products = Product::get()->filter(["AllowPurchase" => 1]); |
||
72 | $productsMap = $products->map('ID', 'Title')->toArray(); |
||
73 | $arr1 = [0 => "--- Please select ---"] + $productsMap; |
||
74 | foreach ($productsMap as $id => $title) { |
||
75 | if ($product = Product::get()->byID($id)) { |
||
76 | if (!$product->canPurchase()) { |
||
77 | unset($productsMap[$id]); |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | $j = 0; |
||
82 | foreach ($items as $key => $item) { |
||
83 | $j++; |
||
84 | $alternativeItemsMap = $productsMap; |
||
85 | $defaultProductID = $item->ProductID ? $item->ProductID : $item->BuyableID; |
||
86 | $itemID = $defaultProductID; |
||
87 | unset($alternativeItemsMap[$defaultProductID]); |
||
88 | $fields->push( |
||
89 | HiddenField::create( |
||
90 | 'Product[ID]['.$itemID.']', |
||
91 | "Preferred Product #$j", |
||
92 | $defaultProductID |
||
93 | ) |
||
94 | ); |
||
95 | $fields->push( |
||
96 | HiddenField::create( |
||
97 | 'Product[Quantity]['.$itemID.']', |
||
98 | " ... quantity", |
||
99 | $item->Quantity |
||
100 | ) |
||
101 | ); |
||
102 | $altCount = Config::inst()->get('RepeatOrderForm', 'number_of_product_alternatives'); |
||
103 | if($altCount > 9) { |
||
104 | user_error('You can only have up to nine alternatives buyables'); |
||
105 | } elseif($altCount > 0) { |
||
106 | for ($i = 1; $i <= $altCount; $i++) { |
||
107 | $alternativeField = "Alternative".$i."ID"; |
||
108 | $fields->push( |
||
109 | DropdownField::create( |
||
110 | 'Product['.$alternativeField.']['.$itemID.']', |
||
111 | " ... alternative $i", |
||
112 | $alternativeItemsMap, |
||
113 | (isset($item->$alternativeField) ? $item->$alternativeField : 0) |
||
114 | ) |
||
115 | ); |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | } else { |
||
120 | $fields->push(HeaderField::create('items', 'There are no products in this repeating order')); |
||
121 | } |
||
122 | |||
123 | //other details |
||
124 | $fields->push( |
||
125 | HeaderField::create( |
||
126 | 'DetailsHeader', |
||
127 | 'Repeat Order Details' |
||
128 | ) |
||
129 | ); |
||
130 | $fields->push( |
||
131 | DropdownField::create( |
||
132 | 'PaymentMethod', |
||
133 | 'Payment Method', |
||
134 | Config::inst()->get('RepeatOrder', 'payment_methods'), |
||
135 | $repeatOrder ? $repeatOrder->PaymentMethod : '' |
||
136 | ) |
||
137 | ); |
||
138 | |||
139 | $startField = DateField::create( |
||
140 | 'Start', |
||
141 | 'Start Date', |
||
142 | $repeatOrder ? $repeatOrder->Start : date('d-m-Y') |
||
143 | ); |
||
144 | $startField->setAttribute('autocomplete', 'off'); |
||
145 | $startField->setConfig('showcalendar', true); |
||
146 | $fields->push($startField); |
||
147 | |||
148 | $endField = DateField::create( |
||
149 | 'End', |
||
150 | 'End Date', |
||
151 | $repeatOrder ? $repeatOrder->End : '' |
||
152 | ); |
||
153 | $endField->setAttribute('autocomplete', 'off'); |
||
154 | $endField->setConfig('showcalendar', true); |
||
155 | $fields->push($endField); |
||
156 | |||
157 | $fields->push( |
||
158 | DropdownField::create( |
||
159 | 'Period', |
||
160 | 'Period', |
||
161 | Config::inst()->get('RepeatOrder', 'period_fields'), |
||
162 | $repeatOrder ? $repeatOrder->Period : '' |
||
163 | ) |
||
164 | ); |
||
165 | |||
166 | $fields->push( |
||
167 | TextareaField::create( |
||
168 | 'Notes', |
||
169 | 'Notes', |
||
170 | $repeatOrder ? $repeatOrder->Notes : '' |
||
171 | ) |
||
172 | ); |
||
173 | |||
174 | $repeatOrderFormLink = RepeatOrdersPage::get_repeat_order_link('ajaxcreateorder', $order ? $order->ID : 0); |
||
175 | $fields->push( |
||
176 | HiddenField::create('AjaxSubmissionLink', 'AjaxSubmissionLink', $repeatOrderFormLink) |
||
177 | ); |
||
178 | |||
179 | //hidden field |
||
180 | if (isset($order->ID)) { |
||
181 | $fields->push( |
||
182 | HiddenField::create('OrderID', 'OrderID', $order->ID) |
||
183 | ); |
||
184 | } |
||
185 | if ($repeatOrder) { |
||
186 | $fields->push( |
||
187 | HiddenField::create('RepeatOrderID', 'RepeatOrderID', $repeatOrder->ID) |
||
188 | ); |
||
189 | } |
||
190 | return $fields; |
||
191 | } |
||
192 | |||
339 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.