| Conditions | 1 |
| Paths | 1 |
| Total Lines | 85 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 116 | private function getLocationFieldset() |
||
| 117 | { |
||
| 118 | // Fieldset |
||
| 119 | $fieldset = new FormElement(FormElement::TAG_FIELDSET, 'location'); |
||
| 120 | // Legend for the fieldset. |
||
| 121 | $legend = new FormElement(FormElement::TAG_LEGEND); |
||
| 122 | $legend->setLabel('Location'); |
||
| 123 | |||
| 124 | // Select box with no multi selection and with no option groups |
||
| 125 | $select1 = new FormElement(FormElement::TAG_SELECT, 'country', 'I live in:'); |
||
| 126 | $select1->setOptions( |
||
| 127 | [ |
||
| 128 | ['label' => 'Hungary', 'value' => 'hu'], |
||
| 129 | ['label' => 'Germany', 'value' => 'de', 'checked' => true], |
||
| 130 | ['label' => 'Austria', 'value' => 'at'], |
||
| 131 | ] |
||
| 132 | ); |
||
| 133 | |||
| 134 | // Select box with no multi selection and WITH option groups |
||
| 135 | $select2 = new FormElement(FormElement::TAG_SELECT, 'dream', 'I\'d like live in:'); |
||
| 136 | $select2->setOptions( |
||
| 137 | [ |
||
| 138 | ['label' => 'Anywhere', 'value' => '*'], |
||
| 139 | ['label' => 'Hungary', 'value' => 'hu', 'group' => 'Europe'], |
||
| 140 | ['label' => 'USA', 'value' => 'us', 'group' => 'America'], |
||
| 141 | ['label' => 'Germany', 'value' => 'de', 'group' => 'Europe'], |
||
| 142 | ['label' => 'Austria', 'value' => 'at', 'group' => 'Europe'], |
||
| 143 | ['label' => 'Canada', 'value' => 'ca', 'group' => 'America'], |
||
| 144 | ['label' => 'Switzerland', 'value' => 'ch', 'group' => 'Europe', 'checked' => true], |
||
| 145 | ['label' => 'France', 'value' => 'fr', 'group' => 'Europe'], |
||
| 146 | ['label' => 'Spain', 'value' => 'es', 'group' => 'Europe'], |
||
| 147 | ] |
||
| 148 | ); |
||
| 149 | |||
| 150 | // Select box WITH multi selection. The element is build with constructors. |
||
| 151 | $select3 = new FormElement(FormElement::TAG_SELECT, 'past', 'I\'ve already lived in:'); |
||
| 152 | $select3->setAttribute('multiple', true) |
||
| 153 | ->setAttribute('size', 10); |
||
| 154 | |||
| 155 | $select3Group1 = new FormElement(FormElement::TAG_OPTION_GROUP); |
||
| 156 | $select3Group1->setLabel('Europe'); |
||
| 157 | |||
| 158 | $option1 = new FormElement(FormElement::TAG_OPTION); |
||
| 159 | $option1->setValue('hu')->setLabel('Hungary')->setAttribute('selected', true); |
||
| 160 | |||
| 161 | $option2 = new FormElement(FormElement::TAG_OPTION); |
||
| 162 | $option2->setValue('de')->setLabel('Germany')->setAttribute('selected', true); |
||
| 163 | |||
| 164 | $option3 = new FormElement(FormElement::TAG_OPTION); |
||
| 165 | $option3->setValue('ch')->setLabel('Switzerland'); |
||
| 166 | |||
| 167 | $option4 = new FormElement(FormElement::TAG_OPTION); |
||
| 168 | $option4->setValue('fr')->setLabel('France'); |
||
| 169 | |||
| 170 | $select3Group1->setChildNodes([$option1, $option2, $option3, $option4]); |
||
| 171 | |||
| 172 | $select3Group2 = new FormElement(FormElement::TAG_OPTION_GROUP); |
||
| 173 | $select3Group2->setLabel('America'); |
||
| 174 | |||
| 175 | $option5 = new FormElement(FormElement::TAG_OPTION); |
||
| 176 | $option5->setValue('us')->setLabel('USA'); |
||
| 177 | |||
| 178 | $option6 = new FormElement(FormElement::TAG_OPTION); |
||
| 179 | $option6->setValue('ca')->setLabel('Canada'); |
||
| 180 | |||
| 181 | $select3Group2->setChildNodes([$option5, $option6]); |
||
| 182 | |||
| 183 | $select3Group3 = new FormElement(FormElement::TAG_OPTION_GROUP); |
||
| 184 | $select3Group3->setLabel('Asia'); |
||
| 185 | |||
| 186 | $option7 = new FormElement(FormElement::TAG_OPTION); |
||
| 187 | $option7->setValue('jp')->setLabel('Japan'); |
||
| 188 | |||
| 189 | $select3Group3->setChildNodes([$option7]); |
||
| 190 | |||
| 191 | $select3->setChildNodes([$select3Group1, $select3Group2, $select3Group3]); |
||
| 192 | |||
| 193 | // Assign elements to the fieldset |
||
| 194 | $fieldset->addChildNode($legend) |
||
| 195 | ->addChildNode($select1) |
||
| 196 | ->addChildNode($select2) |
||
| 197 | ->addChildNode($select3); |
||
| 198 | |||
| 199 | return $fieldset; |
||
| 200 | } |
||
| 201 | } |
||
| 202 |