| Conditions | 5 |
| Paths | 2 |
| Total Lines | 101 |
| Code Lines | 64 |
| 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 |
||
| 125 | public function getCMSFields() |
||
| 126 | { |
||
| 127 | $fields = FieldList::create( |
||
| 128 | TextField::create('InternalItemID', _t('SilverShop\Page\Product.Code', 'Product Code')), |
||
| 129 | TextField::create('Price', _t('SilverShop\Page\Product.db_BasePrice', 'Price')) |
||
| 130 | ); |
||
| 131 | //add attributes dropdowns |
||
| 132 | $attributes = $this->Product()->VariationAttributeTypes(); |
||
| 133 | if ($attributes->exists()) { |
||
| 134 | foreach ($attributes as $attribute) { |
||
| 135 | if ($field = $attribute->getDropDownField()) { |
||
| 136 | if ($value = $this->AttributeValues()->find('TypeID', $attribute->ID)) { |
||
| 137 | $field->setValue($value->ID); |
||
| 138 | } |
||
| 139 | $fields->push($field); |
||
| 140 | } else { |
||
| 141 | $fields->push( |
||
| 142 | LiteralField::create( |
||
| 143 | 'novalues' . $attribute->Name, |
||
| 144 | '<p class="message warning">' . |
||
| 145 | _t( |
||
| 146 | __CLASS__ . '.NoAttributeValuesMessage', |
||
| 147 | '{attribute} has no values to choose from. You can create them in the "Products" > "Product Attribute Type" section of the CMS.', |
||
| 148 | 'Warning that will be shown if an attribute doesn\'t have any values', |
||
| 149 | ['attribute' => $attribute->Name] |
||
| 150 | ) . |
||
| 151 | '</p>' |
||
| 152 | ) |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | //TODO: allow setting custom values here, rather than visiting the products section |
||
| 156 | } |
||
| 157 | } else { |
||
| 158 | $fields->push( |
||
| 159 | LiteralField::create( |
||
| 160 | 'savefirst', |
||
| 161 | '<p class="message warning">' . |
||
| 162 | _t( |
||
| 163 | __CLASS__ . '.MustSaveFirstMessage', |
||
| 164 | 'You can choose variation attributes after saving for the first time, if they exist.' |
||
| 165 | ) . |
||
| 166 | '</p>' |
||
| 167 | ) |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | $fields->push( |
||
| 171 | UploadField::create('Image', _t('SilverShop\Page\Product.Image', 'Product Image')) |
||
| 172 | ); |
||
| 173 | |||
| 174 | //physical measurement units |
||
| 175 | $fieldSubstitutes = [ |
||
| 176 | 'LengthUnit' => Product::config()->length_unit |
||
| 177 | ]; |
||
| 178 | |||
| 179 | //physical measurements |
||
| 180 | $fields->push( |
||
| 181 | TextField::create( |
||
| 182 | 'Weight', |
||
| 183 | _t( |
||
| 184 | 'SilverShop\Page\Product.WeightWithUnit', |
||
| 185 | 'Weight ({WeightUnit})', |
||
| 186 | '', |
||
| 187 | [ |
||
| 188 | 'WeightUnit' => Product::config()->weight_unit |
||
| 189 | ] |
||
| 190 | ), |
||
| 191 | '', |
||
| 192 | 12 |
||
| 193 | ) |
||
| 194 | ); |
||
| 195 | |||
| 196 | $fields->push( |
||
| 197 | TextField::create( |
||
| 198 | 'Height', |
||
| 199 | _t('SilverShop\Page\Product.HeightWithUnit', 'Height ({LengthUnit})', '', $fieldSubstitutes), |
||
| 200 | '', |
||
| 201 | 12 |
||
| 202 | ) |
||
| 203 | ); |
||
| 204 | |||
| 205 | $fields->push( |
||
| 206 | TextField::create( |
||
| 207 | 'Width', |
||
| 208 | _t('SilverShop\Page\Product.WidthWithUnit', 'Width ({LengthUnit})', '', $fieldSubstitutes), |
||
| 209 | '', |
||
| 210 | 12 |
||
| 211 | ) |
||
| 212 | ); |
||
| 213 | |||
| 214 | $fields->push( |
||
| 215 | TextField::create( |
||
| 216 | 'Depth', |
||
| 217 | _t('SilverShop\Page\Product.DepthWithUnit', 'Depth ({LengthUnit})', '', $fieldSubstitutes), |
||
| 218 | '', |
||
| 219 | 12 |
||
| 220 | ) |
||
| 221 | ); |
||
| 222 | |||
| 223 | $this->extend('updateCMSFields', $fields); |
||
| 224 | |||
| 225 | return $fields; |
||
| 226 | } |
||
| 364 |