| Conditions | 5 |
| Paths | 5 |
| Total Lines | 51 |
| Code Lines | 29 |
| 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 |
||
| 10 | public static function errorHtml(array $errors, ModelInterface $model): string |
||
| 11 | { |
||
| 12 | // フィールド定義を取得 |
||
| 13 | $fieldDefinitions = $model->getFields(); |
||
| 14 | |||
| 15 | $html = '<div class="errormessages" role="status">'; |
||
| 16 | $html .= '<ul class="alert alert-danger p-3 ps-5 pt-0 mt-3 mb-3 fs-6">'; |
||
| 17 | |||
| 18 | foreach ($errors as $field => $errorDetails) { |
||
| 19 | $messages = $errorDetails['messages'] ?? []; |
||
| 20 | $htmlName = $errorDetails['htmlName'] ?? $field; |
||
| 21 | |||
| 22 | // フォーム全体のエラー |
||
| 23 | if ($field === 0) { |
||
| 24 | $html .= '<li class="pt-3">' . __('found_the_problem', 'Found the problem'); |
||
| 25 | $html .= '<ul class="ps-3">'; |
||
| 26 | foreach ($messages as $message) { |
||
| 27 | $html .= sprintf('<li class="pt-2">%s</li>', e($message)); |
||
| 28 | } |
||
| 29 | $html .= '</ul></li>'; |
||
| 30 | continue; |
||
| 31 | } |
||
| 32 | |||
| 33 | // フィールド固有のエラー |
||
| 34 | $id = FormUtils::nameToId($htmlName); |
||
| 35 | |||
| 36 | $html .= sprintf('<li id="errormessage_%s" class="pt-3">', e($id)); |
||
| 37 | |||
| 38 | // モデルからラベルを取得 |
||
| 39 | $labelText = $fieldDefinitions[$field]['label'] ?? ucfirst($field); |
||
| 40 | |||
| 41 | $html .= sprintf( |
||
| 42 | '<a href="#%s" class="alert-link">%s</a>', |
||
| 43 | e($id), |
||
| 44 | __('error_at_x', 'Error at :name', ['name' => $labelText]), |
||
| 45 | ); |
||
| 46 | |||
| 47 | $html .= '<ul class="ps-3">'; |
||
| 48 | foreach ($messages as $message) { |
||
| 49 | $html .= sprintf( |
||
| 50 | '<li class="pt-2">%s%s</li>', |
||
| 51 | $labelText, |
||
| 52 | e($message) |
||
| 53 | ); |
||
| 54 | } |
||
| 55 | $html .= '</ul></li>'; |
||
| 56 | } |
||
| 57 | |||
| 58 | $html .= '</ul></div>'; |
||
| 59 | |||
| 60 | return $html; |
||
| 61 | } |
||
| 102 |