| Conditions | 13 |
| Paths | 24 |
| Total Lines | 72 |
| Code Lines | 37 |
| 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 29 | public static function processGroupLeft( |
||
| 30 | Worksheet $worksheet, |
||
| 31 | array $data, |
||
| 32 | int $groupLeftCount, |
||
| 33 | array $groupLeft, |
||
| 34 | array $fields, |
||
| 35 | array $mergeColumns, |
||
| 36 | int $row, |
||
| 37 | callable $setCellValueFunc |
||
| 38 | ): int { |
||
| 39 | // 获取分组字段在field中的实际位置 |
||
| 40 | $group_field_positions = []; |
||
| 41 | foreach ($groupLeft as $group_field) { |
||
| 42 | $position = array_search($group_field, $fields); |
||
| 43 | if ($position !== false) { |
||
| 44 | $group_field_positions[] = $position; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | if (empty($group_field_positions)) { |
||
| 49 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, '分组字段未在标题中定义'); |
||
| 50 | } |
||
| 51 | |||
| 52 | $group_start = $row; |
||
| 53 | foreach ($data as $key => $val) { |
||
| 54 | // 第一级分组的合并单元格 |
||
| 55 | $rowName = WorkSheetHelper::cellName($group_field_positions[0]); // 使用第一个分组字段的实际位置 |
||
| 56 | $coordinate = $rowName . $row . ':' . $rowName . ($row + $val['count'] - 1); |
||
| 57 | $worksheet->mergeCells($coordinate); |
||
| 58 | $worksheet->setCellValue($rowName . $row, $key); |
||
| 59 | |||
| 60 | // 合并mergeColumns指定的其它列 |
||
| 61 | if (!empty($mergeColumns)) { |
||
| 62 | foreach ($mergeColumns as $field) { |
||
| 63 | // 跳过分组字段本身 |
||
| 64 | if (in_array($field, $groupLeft)) continue; |
||
| 65 | $colIdx = array_search($field, $fields); |
||
| 66 | if ($colIdx !== false) { |
||
| 67 | $colLetter = WorkSheetHelper::cellName($colIdx); |
||
|
|
|||
| 68 | $worksheet->mergeCells($colLetter . $row . ':' . $colLetter . ($row + $val['count'] - 1)); |
||
| 69 | // 取本组第一个数据的值 |
||
| 70 | $worksheet->setCellValue($colLetter . $row, $val['data'][0][$field] ?? ''); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($groupLeftCount == 1) { |
||
| 76 | foreach ($val['data'] as $dataRow) { |
||
| 77 | $setCellValueFunc($dataRow); |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | $sub_group_start = $row; |
||
| 81 | $rowName = WorkSheetHelper::cellName($group_field_positions[1]); // 使用第二个分组字段的实际位置 |
||
| 82 | |||
| 83 | foreach ($val['data'] as $k => $v) { |
||
| 84 | $coordinate = $rowName . $sub_group_start . ':' . $rowName . ($sub_group_start + $v['count'] - 1); |
||
| 85 | $worksheet->mergeCells($coordinate); |
||
| 86 | $worksheet->setCellValue($rowName . $sub_group_start, $k); |
||
| 87 | |||
| 88 | foreach ($v['data'] as $data) { |
||
| 89 | $setCellValueFunc($data); |
||
| 90 | } |
||
| 91 | |||
| 92 | $sub_group_start = $sub_group_start + $v['count']; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $row = $group_start + $val['count']; |
||
| 97 | $group_start = $row; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $row; |
||
| 101 | } |
||
| 170 |