| Conditions | 17 |
| Paths | 300 |
| Total Lines | 117 |
| Code Lines | 86 |
| 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 |
||
| 41 | public function run(): string |
||
| 42 | { |
||
| 43 | $new = clone $this; |
||
| 44 | $type = strtolower($new->type); |
||
| 45 | |||
| 46 | $uncheckValue = ArrayHelper::remove($new->options, 'unselect'); |
||
| 47 | if (!$new->noUnselect) { |
||
| 48 | $uncheckValue ??= $new->unselect; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (!empty($new->getId())) { |
||
| 52 | $new->options['id'] = $new->getId(); |
||
| 53 | } |
||
| 54 | |||
| 55 | $containerTag = ArrayHelper::remove($new->options, 'tag', 'div'); |
||
| 56 | $separator = ArrayHelper::remove($new->options, 'separator', "\n"); |
||
| 57 | $encode = ArrayHelper::remove($new->options, 'encode', true); |
||
| 58 | $disabled = ArrayHelper::remove($new->options, 'disabled', false); |
||
| 59 | |||
| 60 | switch ($type) { |
||
| 61 | case 'checkboxlist': |
||
| 62 | $checkboxAttributes = ArrayHelper::remove($new->options, 'itemOptions', []); |
||
| 63 | $encodeLabels = ArrayHelper::remove($checkboxAttributes, 'encode', true); |
||
| 64 | |||
| 65 | /** @psalm-var Closure(CheckboxItem):string|null $itemFormatter */ |
||
| 66 | $itemFormatter = $this->itemFormatter; |
||
| 67 | |||
| 68 | $value = $new->getValue(); |
||
| 69 | /** @psalm-suppress PossiblyInvalidArgument */ |
||
| 70 | return Html::checkboxList($new->getName()) |
||
| 71 | ->values(!is_iterable($value) ? [$value] : $value) |
||
| 72 | ->uncheckValue($uncheckValue) |
||
| 73 | ->items($new->items, $encodeLabels) |
||
| 74 | ->itemFormatter($itemFormatter) |
||
| 75 | ->separator($separator) |
||
| 76 | ->containerTag($containerTag) |
||
| 77 | ->containerAttributes($new->options) |
||
| 78 | ->checkboxAttributes($checkboxAttributes) |
||
| 79 | ->disabled($disabled) |
||
| 80 | ->render(); |
||
| 81 | |||
| 82 | case 'radiolist': |
||
| 83 | $radioAttributes = ArrayHelper::remove($new->options, 'itemOptions', []); |
||
| 84 | $encodeLabels = ArrayHelper::remove($radioAttributes, 'encode', true); |
||
| 85 | |||
| 86 | /** @psalm-var Closure(RadioItem):string|null $itemFormatter */ |
||
| 87 | $itemFormatter = $this->itemFormatter; |
||
| 88 | |||
| 89 | $value = $new->getValue(); |
||
| 90 | return Html::radioList($new->getName()) |
||
| 91 | ->value($value) |
||
| 92 | ->uncheckValue($uncheckValue) |
||
| 93 | ->items($new->items, $encodeLabels) |
||
| 94 | ->itemFormatter($itemFormatter) |
||
| 95 | ->separator($separator) |
||
| 96 | ->containerTag($containerTag) |
||
| 97 | ->containerAttributes($new->options) |
||
| 98 | ->radioAttributes($radioAttributes) |
||
| 99 | ->disabled($disabled) |
||
| 100 | ->render(); |
||
| 101 | |||
| 102 | case 'listbox': |
||
| 103 | case 'dropdownlist': |
||
| 104 | $groups = ArrayHelper::remove($new->options, 'groups', []); |
||
| 105 | $optionsAttributes = ArrayHelper::remove($new->options, 'options', []); |
||
| 106 | |||
| 107 | $items = []; |
||
| 108 | foreach ($new->items as $value => $content) { |
||
| 109 | if (is_array($content)) { |
||
| 110 | $groupAttrs = $groups[$value] ?? []; |
||
| 111 | $groupAttrs['encode'] = false; |
||
| 112 | if (!isset($groupAttrs['label'])) { |
||
| 113 | $groupAttrs['label'] = $value; |
||
| 114 | } |
||
| 115 | $options = []; |
||
| 116 | foreach ($content as $v => $c) { |
||
| 117 | $options[] = Html::option($c, $v) |
||
| 118 | ->attributes($optionsAttributes[$v] ?? []) |
||
| 119 | ->encode($encode); |
||
| 120 | } |
||
| 121 | $items[] = Html::optgroup() |
||
| 122 | ->options(...$options) |
||
| 123 | ->attributes($groupAttrs); |
||
| 124 | } else { |
||
| 125 | $items[] = Html::option($content, $value) |
||
| 126 | ->attributes($optionsAttributes[$value] ?? []) |
||
| 127 | ->encode($encode); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | $promptOption = null; |
||
| 132 | $prompt = ArrayHelper::remove($new->options, 'prompt'); |
||
| 133 | if ($prompt) { |
||
| 134 | $promptText = $prompt['text'] ?? ''; |
||
| 135 | if ($promptText) { |
||
| 136 | $promptOption = Html::option($promptText) |
||
| 137 | ->attributes($prompt['options'] ?? []); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($type === 'listbox') { |
||
| 142 | $new->options['size'] ??= 4; |
||
| 143 | } |
||
| 144 | |||
| 145 | $value = $new->getValue(); |
||
| 146 | /** @psalm-suppress PossiblyInvalidArgument */ |
||
| 147 | return Html::select($new->getName()) |
||
| 148 | ->values(!is_iterable($value) ? [$value] : $value) |
||
| 149 | ->unselectValue($type === 'listbox' ? $uncheckValue : null) |
||
| 150 | ->promptOption($promptOption) |
||
| 151 | ->items(...$items) |
||
| 152 | ->attributes($new->options) |
||
| 153 | ->disabled($disabled) |
||
| 154 | ->render(); |
||
| 155 | } |
||
| 156 | |||
| 157 | throw new RuntimeException('Unknown type: ' . $type); |
||
| 158 | } |
||
| 353 |