| Conditions | 7 |
| Paths | 16 |
| Total Lines | 52 |
| Code Lines | 34 |
| 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 |
||
| 111 | protected function editableItems() |
||
| 112 | { |
||
| 113 | $editables = ArrayList::create(); |
||
| 114 | foreach ($this->items as $item) { |
||
| 115 | $buyable = $item->Buyable(); |
||
| 116 | if (!$buyable) { |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | // If the buyable is a variation, use the belonging product instead for variation-form generation |
||
| 120 | if ($buyable instanceof Variation) { |
||
| 121 | $buyable = $buyable->Product(); |
||
| 122 | } |
||
| 123 | $name = $this->name . "[$item->ID]"; |
||
| 124 | $quantity = TextField::create( |
||
| 125 | $name . '[Quantity]', |
||
| 126 | 'Quantity', |
||
| 127 | $item->Quantity |
||
| 128 | ) |
||
| 129 | ->addExtraClass('numeric') |
||
| 130 | ->setAttribute('type', 'number') |
||
| 131 | ->setAttribute('min', '0'); |
||
| 132 | |||
| 133 | $variationfield = false; |
||
| 134 | if ($buyable->hasMany('Variations')) { |
||
| 135 | $variations = $buyable->Variations(); |
||
| 136 | if ($variations->exists()) { |
||
| 137 | $variationfield = DropdownField::create( |
||
| 138 | $name . '[ProductVariationID]', |
||
| 139 | _t('SilverShop\Model\Variation\Variation.SINGULARNAME', 'Variation'), |
||
| 140 | $variations->map('ID', 'Title'), |
||
| 141 | $item->ProductVariationID |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | $remove = CheckboxField::create($name . '[Remove]', _t('SilverShop\Generic.Remove', 'Remove')); |
||
| 146 | $editables->push( |
||
| 147 | $item->customise( |
||
| 148 | [ |
||
| 149 | 'QuantityField' => $quantity, |
||
| 150 | 'VariationField' => $variationfield, |
||
| 151 | 'RemoveField' => $remove, |
||
| 152 | ] |
||
| 153 | ) |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | if (is_callable($this->editableItemsCallback)) { |
||
| 158 | $callback = $this->editableItemsCallback; |
||
| 159 | $editables = $callback($editables); |
||
| 160 | } |
||
| 161 | |||
| 162 | return $editables; |
||
| 163 | } |
||
| 165 |