Conditions | 3 |
Paths | 1 |
Total Lines | 170 |
Code Lines | 122 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
150 | public function getCMSFields() |
||
151 | { |
||
152 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||
153 | $fields->removeByName([ |
||
154 | 'Images', |
||
155 | 'WeightModifier', |
||
156 | 'CodeModifier', |
||
157 | 'PriceModifier', |
||
158 | 'WeightModifierAction', |
||
159 | 'CodeModifierAction', |
||
160 | 'PriceModifierAction', |
||
161 | 'Available', |
||
162 | 'Type', |
||
163 | 'OptionModifierKey', |
||
164 | 'SortOrder', |
||
165 | 'ProductID', |
||
166 | 'FinalPrice', |
||
167 | 'FinalWeight', |
||
168 | 'FinalCode', |
||
169 | ]); |
||
170 | |||
171 | $fields->insertBefore( |
||
172 | 'Content', |
||
173 | CheckboxField::create('Available') |
||
174 | ->setTitle('Available for purchase') |
||
175 | ); |
||
176 | |||
177 | $fields->insertBefore( |
||
178 | 'Content', |
||
179 | $fields->dataFieldByName('VariationTypeID') |
||
180 | ); |
||
181 | |||
182 | $fields->insertBefore( |
||
183 | 'Content', |
||
184 | $fields->dataFieldByName('UseProductContent') |
||
185 | ); |
||
186 | |||
187 | $content = $fields->dataFieldByName('Content'); |
||
188 | |||
189 | $content->hideUnless('UseProductContent')->isNotChecked()->end(); |
||
190 | |||
191 | if ($this->exists()) { |
||
192 | $fields->addFieldToTab( |
||
193 | 'Root.ProductModifications', |
||
194 | ReadonlyField::create('OptionModifierKey') |
||
195 | ->setTitle(_t('Variation.ModifierKey', 'Modifier Key')) |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | if ($this->Product()->hasDatabaseField('Weight')) { |
||
|
|||
200 | $fields->addFieldtoTab( |
||
201 | 'Root.ProductModifications', |
||
202 | FieldGroup::create( |
||
203 | DropdownField::create( |
||
204 | 'WeightModifierAction', |
||
205 | _t('Variation.WeightModifierAction', 'Weight Modification Type'), |
||
206 | [ |
||
207 | 'Add' => _t( |
||
208 | 'Variation.WeightAdd', |
||
209 | 'Add to Base Weight', |
||
210 | 'Add to weight' |
||
211 | ), |
||
212 | 'Subtract' => _t( |
||
213 | 'Variation.WeightSubtract', |
||
214 | 'Subtract from Base Weight', |
||
215 | 'Subtract from weight' |
||
216 | ), |
||
217 | 'Set' => _t('Variation.WeightSet', 'Set as a new Weight'), |
||
218 | ] |
||
219 | ) |
||
220 | ->setEmptyString('') |
||
221 | ->setDescription(_t( |
||
222 | 'Variation.WeightDescription', |
||
223 | 'Does weight modify or replace base weight?' |
||
224 | )), |
||
225 | NumericField::create("WeightModifier") |
||
226 | ->setTitle(_t('Variation.WeightModifier', 'Weight')) |
||
227 | ->setScale(3) |
||
228 | ->setDescription(_t( |
||
229 | 'Variation.WeightDescription', |
||
230 | 'Only supports up to 3 decimal places' |
||
231 | ))->displayIf('WeightModifierAction')->isNotEmpty()->end(), |
||
232 | NumericField::create('FinalWeight') |
||
233 | ->setTitle('Final Modified Weight') |
||
234 | ->setDescription("Product's weight is {$this->Product()->Weight}") |
||
235 | ->performDisabledTransformation() |
||
236 | )->setTitle('Weight Modification') |
||
237 | ); |
||
238 | } |
||
239 | |||
240 | $fields->addFieldsToTab( |
||
241 | 'Root.ProductModifications', |
||
242 | [ |
||
243 | // Price Modifier Fields |
||
244 | //HeaderField::create('PriceHD', _t('Variation.PriceHD', 'Modify Price'), 4), |
||
245 | FieldGroup::create( |
||
246 | DropdownField::create( |
||
247 | 'PriceModifierAction', |
||
248 | _t('Variation.PriceModifierAction', 'Price Modification Type'), |
||
249 | [ |
||
250 | 'Add' => _t( |
||
251 | 'Variation.PriceAdd', |
||
252 | 'Add to Base Price', |
||
253 | 'Add to price' |
||
254 | ), |
||
255 | 'Subtract' => _t( |
||
256 | 'Variation.PriceSubtract', |
||
257 | 'Subtract from Base Price', |
||
258 | 'Subtract from price' |
||
259 | ), |
||
260 | 'Set' => _t('Variation.PriceSet', 'Set as a new Price'), |
||
261 | ] |
||
262 | ) |
||
263 | ->setEmptyString('') |
||
264 | ->setDescription(_t('Variation.PriceDescription', 'Does price modify or replace base price?')), |
||
265 | CurrencyField::create('PriceModifier') |
||
266 | ->setTitle(_t('Variation.PriceModifier', 'Price')) |
||
267 | ->displayIf('PriceModifierAction')->isNotEmpty()->end(), |
||
268 | CurrencyField::create('FinalPrice') |
||
269 | ->setTitle('Final Modified Price') |
||
270 | ->setDescription("Product's price is {$this->Product()->Price}") |
||
271 | ->performDisabledTransformation() |
||
272 | )->setTitle('Price Modifications'), |
||
273 | |||
274 | // Code Modifier Fields |
||
275 | //HeaderField::create('CodeHD', _t('Variation.CodeHD', 'Modify Code'), 4), |
||
276 | FieldGroup::create( |
||
277 | DropdownField::create( |
||
278 | 'CodeModifierAction', |
||
279 | _t('Variation.CodeModifierAction', 'Code Modification Type'), |
||
280 | [ |
||
281 | 'Add' => _t( |
||
282 | 'Variation.CodeAdd', |
||
283 | 'Add to Base Code', |
||
284 | 'Add to code' |
||
285 | ), |
||
286 | 'Subtract' => _t( |
||
287 | 'Variation.CodeSubtract', |
||
288 | 'Subtract from Base Code', |
||
289 | 'Subtract from code' |
||
290 | ), |
||
291 | 'Set' => _t('Variation.CodeSet', 'Set as a new Code'), |
||
292 | ] |
||
293 | ) |
||
294 | ->setEmptyString('') |
||
295 | ->setDescription(_t('Variation.CodeDescription', 'Does code modify or replace base code?')), |
||
296 | TextField::create('CodeModifier') |
||
297 | ->setTitle(_t('Variation.CodeModifier', 'Code')) |
||
298 | ->displayIf('CodeModifierAction')->isNotEmpty()->end(), |
||
299 | TextField::create('FinalCode') |
||
300 | ->setTitle('Final Modified Code') |
||
301 | ->setDescription("Product's code is {$this->Product()->Code}") |
||
302 | ->performDisabledTransformation() |
||
303 | )->setTitle('Code Modification'), |
||
304 | ] |
||
305 | ); |
||
306 | |||
307 | // Images tab |
||
308 | $images = SortableUploadField::create('Images') |
||
309 | ->setSortColumn('SortOrder') |
||
310 | ->setIsMultiUpload(true) |
||
311 | ->setAllowedExtensions($this->config()->get('allowed_images_extensions')) |
||
312 | ->setFolderName('Uploads/Products/Images'); |
||
313 | |||
314 | $fields->addFieldsToTab('Root.Images', [ |
||
315 | $images, |
||
316 | ]); |
||
317 | }); |
||
318 | |||
319 | return parent::getCMSFields(); |
||
320 | } |
||
567 |