| Conditions | 2 |
| Paths | 1 |
| Total Lines | 150 |
| Code Lines | 104 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 108 | public function getCMSFields() |
||
| 109 | { |
||
| 110 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||
| 111 | $fields->removeByName([ |
||
| 112 | 'Images', |
||
| 113 | 'WeightModifier', |
||
| 114 | 'CodeModifier', |
||
| 115 | 'PriceModifier', |
||
| 116 | 'WeightModifierAction', |
||
| 117 | 'CodeModifierAction', |
||
| 118 | 'PriceModifierAction', |
||
| 119 | 'Available', |
||
| 120 | 'Type', |
||
| 121 | 'OptionModifierKey', |
||
| 122 | 'SortOrder', |
||
| 123 | 'ProductID', |
||
| 124 | ]); |
||
| 125 | |||
| 126 | $fields->insertBefore( |
||
| 127 | 'Content', |
||
| 128 | CheckboxField::create('Available') |
||
| 129 | ->setTitle('Available for purchase') |
||
| 130 | ); |
||
| 131 | |||
| 132 | $fields->insertBefore( |
||
| 133 | 'Content', |
||
| 134 | $fields->dataFieldByName('VariationTypeID') |
||
| 135 | ); |
||
| 136 | |||
| 137 | $fields->insertBefore( |
||
| 138 | 'Content', |
||
| 139 | $fields->dataFieldByName('UseProductContent') |
||
| 140 | ); |
||
| 141 | |||
| 142 | $content = $fields->dataFieldByName('Content'); |
||
| 143 | |||
| 144 | $content->hideUnless('UseProductContent')->isNotChecked()->end(); |
||
| 145 | |||
| 146 | if ($this->exists()) { |
||
| 147 | $fields->addFieldToTab( |
||
| 148 | 'Root.ProductModifications', |
||
| 149 | ReadonlyField::create('OptionModifierKey') |
||
| 150 | ->setTitle(_t('Variation.ModifierKey', 'Modifier Key')) |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 154 | $fields->addFieldsToTab( |
||
| 155 | 'Root.ProductModifications', |
||
| 156 | [ |
||
| 157 | FieldGroup::create( |
||
| 158 | DropdownField::create( |
||
| 159 | 'WeightModifierAction', |
||
| 160 | _t('Variation.WeightModifierAction', 'Weight Modification Type'), |
||
| 161 | [ |
||
| 162 | 'Add' => _t( |
||
| 163 | 'Variation.WeightAdd', |
||
| 164 | 'Add to Base Weight', |
||
| 165 | 'Add to weight' |
||
| 166 | ), |
||
| 167 | 'Subtract' => _t( |
||
| 168 | 'Variation.WeightSubtract', |
||
| 169 | 'Subtract from Base Weight', |
||
| 170 | 'Subtract from weight' |
||
| 171 | ), |
||
| 172 | 'Set' => _t('Variation.WeightSet', 'Set as a new Weight'), |
||
| 173 | ] |
||
| 174 | ) |
||
| 175 | ->setEmptyString('') |
||
| 176 | ->setDescription(_t( |
||
| 177 | 'Variation.WeightDescription', |
||
| 178 | 'Does weight modify or replace base weight?' |
||
| 179 | )), |
||
| 180 | NumericField::create("WeightModifier") |
||
| 181 | ->setTitle(_t('Variation.WeightModifier', 'Weight')) |
||
| 182 | ->setScale(3) |
||
| 183 | ->setDescription(_t( |
||
| 184 | 'Variation.WeightDescription', |
||
| 185 | 'Only supports up to 3 decimal places' |
||
| 186 | ))->displayIf('WeightModifierAction')->isNotEmpty()->end() |
||
| 187 | )->setTitle('Weight Modification'), |
||
| 188 | |||
| 189 | // Price Modifier Fields |
||
| 190 | //HeaderField::create('PriceHD', _t('Variation.PriceHD', 'Modify Price'), 4), |
||
| 191 | FieldGroup::create( |
||
| 192 | DropdownField::create( |
||
| 193 | 'PriceModifierAction', |
||
| 194 | _t('Variation.PriceModifierAction', 'Price Modification Type'), |
||
| 195 | [ |
||
| 196 | 'Add' => _t( |
||
| 197 | 'Variation.PriceAdd', |
||
| 198 | 'Add to Base Price', |
||
| 199 | 'Add to price' |
||
| 200 | ), |
||
| 201 | 'Subtract' => _t( |
||
| 202 | 'Variation.PriceSubtract', |
||
| 203 | 'Subtract from Base Price', |
||
| 204 | 'Subtract from price' |
||
| 205 | ), |
||
| 206 | 'Set' => _t('Variation.PriceSet', 'Set as a new Price'), |
||
| 207 | ] |
||
| 208 | ) |
||
| 209 | ->setEmptyString('') |
||
| 210 | ->setDescription(_t('Variation.PriceDescription', 'Does price modify or replace base price?')), |
||
| 211 | CurrencyField::create('PriceModifier') |
||
| 212 | ->setTitle(_t('Variation.PriceModifier', 'Price')) |
||
| 213 | ->displayIf('PriceModifierAction')->isNotEmpty()->end() |
||
| 214 | )->setTitle('Price Modifications'), |
||
| 215 | |||
| 216 | // Code Modifier Fields |
||
| 217 | //HeaderField::create('CodeHD', _t('Variation.CodeHD', 'Modify Code'), 4), |
||
| 218 | FieldGroup::create( |
||
| 219 | DropdownField::create( |
||
| 220 | 'CodeModifierAction', |
||
| 221 | _t('Variation.CodeModifierAction', 'Code Modification Type'), |
||
| 222 | [ |
||
| 223 | 'Add' => _t( |
||
| 224 | 'Variation.CodeAdd', |
||
| 225 | 'Add to Base Code', |
||
| 226 | 'Add to code' |
||
| 227 | ), |
||
| 228 | 'Subtract' => _t( |
||
| 229 | 'Variation.CodeSubtract', |
||
| 230 | 'Subtract from Base Code', |
||
| 231 | 'Subtract from code' |
||
| 232 | ), |
||
| 233 | 'Set' => _t('Variation.CodeSet', 'Set as a new Code'), |
||
| 234 | ] |
||
| 235 | ) |
||
| 236 | ->setEmptyString('') |
||
| 237 | ->setDescription(_t('Variation.CodeDescription', 'Does code modify or replace base code?')), |
||
| 238 | TextField::create('CodeModifier') |
||
| 239 | ->setTitle(_t('Variation.CodeModifier', 'Code')) |
||
| 240 | ->displayIf('CodeModifierAction')->isNotEmpty()->end() |
||
| 241 | )->setTitle('Code Modification'), |
||
| 242 | ] |
||
| 243 | ); |
||
| 244 | |||
| 245 | // Images tab |
||
| 246 | $images = SortableUploadField::create('Images') |
||
| 247 | ->setSortColumn('SortOrder') |
||
| 248 | ->setIsMultiUpload(true) |
||
| 249 | ->setAllowedExtensions($this->config()->get('allowed_images_extensions')) |
||
| 250 | ->setFolderName('Uploads/Products/Images'); |
||
| 251 | |||
| 252 | $fields->addFieldsToTab('Root.Images', [ |
||
| 253 | $images, |
||
| 254 | ]); |
||
| 255 | }); |
||
| 256 | |||
| 257 | return parent::getCMSFields(); |
||
| 258 | } |
||
| 367 |