| Conditions | 15 |
| Paths | 18 |
| Total Lines | 137 |
| Code Lines | 99 |
| 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 |
||
| 71 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 72 | { |
||
| 73 | $tree = $request->getAttribute('tree'); |
||
| 74 | assert($tree instanceof Tree); |
||
| 75 | |||
| 76 | $user = $request->getAttribute('user'); |
||
| 77 | assert($user instanceof UserInterface); |
||
| 78 | |||
| 79 | $report = $request->getAttribute('report'); |
||
| 80 | $module = $this->module_service->findByName($report); |
||
| 81 | |||
| 82 | if (!$module instanceof ModuleReportInterface) { |
||
| 83 | return redirect(route(ReportListPage::class, ['tree' => $tree->name()])); |
||
| 84 | } |
||
| 85 | |||
| 86 | Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user); |
||
| 87 | |||
| 88 | $xref = $request->getQueryParams()['xref'] ?? ''; |
||
| 89 | |||
| 90 | $xml_filename = $module->resourcesFolder() . $module->xmlFilename(); |
||
| 91 | |||
| 92 | $report_array = (new ReportParserSetup($xml_filename))->reportProperties(); |
||
| 93 | $description = $report_array['description']; |
||
| 94 | $title = $report_array['title']; |
||
| 95 | |||
| 96 | $inputs = []; |
||
| 97 | |||
| 98 | foreach ($report_array['inputs'] ?? [] as $n => $input) { |
||
| 99 | $input += [ |
||
| 100 | 'type' => 'text', |
||
| 101 | 'default' => '', |
||
| 102 | 'lookup' => '', |
||
| 103 | 'extra' => '', |
||
| 104 | ]; |
||
| 105 | |||
| 106 | $attributes = [ |
||
| 107 | 'id' => 'input-' . $n, |
||
| 108 | 'name' => 'vars[' . $input['name'] . ']', |
||
| 109 | 'class' => $input['type'] === 'checkbox' ? 'form-control-check' : 'form-control', |
||
| 110 | ]; |
||
| 111 | |||
| 112 | switch ($input['lookup']) { |
||
| 113 | case 'INDI': |
||
| 114 | $input['control'] = view('components/select-individual', [ |
||
| 115 | 'id' => 'input-' . $n, |
||
| 116 | 'name' => 'vars[' . $input['name'] . ']', |
||
| 117 | 'individual' => Factory::individual()->make($xref, $tree), |
||
| 118 | 'tree' => $tree, |
||
| 119 | 'required' => true, |
||
| 120 | ]); |
||
| 121 | break; |
||
| 122 | |||
| 123 | case 'FAM': |
||
| 124 | $input['control'] = view('components/select-family', [ |
||
| 125 | 'id' => 'input-' . $n, |
||
| 126 | 'name' => 'vars[' . $input['name'] . ']', |
||
| 127 | 'family' => Factory::family()->make($xref, $tree), |
||
| 128 | 'tree' => $tree, |
||
| 129 | 'required' => true, |
||
| 130 | ]); |
||
| 131 | break; |
||
| 132 | |||
| 133 | case 'SOUR': |
||
| 134 | $input['control'] = view('components/select-source', [ |
||
| 135 | 'id' => 'input-' . $n, |
||
| 136 | 'name' => 'vars[' . $input['name'] . ']', |
||
| 137 | 'family' => Factory::source()->make($xref, $tree), |
||
| 138 | 'tree' => $tree, |
||
| 139 | 'required' => true, |
||
| 140 | ]); |
||
| 141 | break; |
||
| 142 | |||
| 143 | case 'DATE': |
||
| 144 | // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly. |
||
| 145 | $dmy = $this->localization_service->dateFormatToOrder(I18N::dateFormat()); |
||
| 146 | |||
| 147 | $attributes += [ |
||
| 148 | 'type' => 'text', |
||
| 149 | 'value' => $input['default'], |
||
| 150 | 'dir' => 'ltr', |
||
| 151 | 'onchange' => 'webtrees.reformatDate(this, "' . $dmy . '")' |
||
| 152 | ]; |
||
| 153 | $input['control'] = '<input ' . Html::attributes($attributes) . '>'; |
||
| 154 | $input['extra'] = view('edit/input-addon-calendar', ['id' => 'input-' . $n]); |
||
| 155 | break; |
||
| 156 | |||
| 157 | default: |
||
| 158 | switch ($input['type']) { |
||
| 159 | case 'text': |
||
| 160 | $attributes += [ |
||
| 161 | 'type' => 'text', |
||
| 162 | 'value' => $input['default'], |
||
| 163 | ]; |
||
| 164 | $input['control'] = '<input ' . Html::attributes($attributes) . '>'; |
||
| 165 | break; |
||
| 166 | |||
| 167 | case 'checkbox': |
||
| 168 | $attributes += [ |
||
| 169 | 'type' => 'checkbox', |
||
| 170 | 'checked' => (bool) $input['default'], |
||
| 171 | ]; |
||
| 172 | $input['control'] = '<input ' . Html::attributes($attributes) . '>'; |
||
| 173 | break; |
||
| 174 | |||
| 175 | case 'select': |
||
| 176 | $options = []; |
||
| 177 | foreach (explode('|', $input['options']) as $option) { |
||
| 178 | [$key, $value] = explode('=>', $option); |
||
| 179 | if (preg_match('/^I18N::number\((.+?)(,([\d+]))?\)$/', $value, $match)) { |
||
| 180 | $number = (float) $match[1]; |
||
| 181 | $precision = (int) ($match[3] ?? 0); |
||
| 182 | $options[$key] = I18N::number($number, $precision); |
||
| 183 | } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) { |
||
| 184 | $options[$key] = I18N::translate($match[1]); |
||
| 185 | } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) { |
||
| 186 | $options[$key] = I18N::translateContext($match[1], $match[2]); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | $input['control'] = view('components/select', ['name' => 'vars[' . $input['name'] . ']', 'id' => 'input-' . $n, 'selected' => $input['default'], 'options' => $options]); |
||
| 190 | break; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | $inputs[] = $input; |
||
| 195 | } |
||
| 196 | |||
| 197 | $destination = $user->getPreference('default-report-destination', 'view'); |
||
| 198 | $format = $user->getPreference('default-report-format', 'PDF'); |
||
| 199 | |||
| 200 | return $this->viewResponse('report-setup-page', [ |
||
| 201 | 'description' => $description, |
||
| 202 | 'destination' => $destination, |
||
| 203 | 'format' => $format, |
||
| 204 | 'inputs' => $inputs, |
||
| 205 | 'report' => $report, |
||
| 206 | 'title' => $title, |
||
| 207 | 'tree' => $tree, |
||
| 208 | ]); |
||
| 211 |