| Conditions | 16 |
| Paths | 42 |
| Total Lines | 71 |
| Code Lines | 42 |
| 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 |
||
| 59 | public static function setTitle( |
||
| 60 | Worksheet $worksheet, |
||
| 61 | array $fileTitle, |
||
| 62 | int $titleRow, |
||
| 63 | array $titleConfig, |
||
| 64 | int $col, |
||
| 65 | int $row, |
||
| 66 | ?int $titleHeight = null, |
||
| 67 | ?int $titleWidth = null, |
||
| 68 | array $requiredFields = [] |
||
| 69 | ): array { |
||
| 70 | if (!empty($titleConfig['title_start_row'])) { |
||
| 71 | $row = $titleConfig['title_start_row']; |
||
| 72 | } |
||
| 73 | |||
| 74 | $_merge = WorkSheetHelper::cellName($col); |
||
| 75 | foreach ($fileTitle as $key => $val) { |
||
| 76 | if (!empty($titleHeight)) { |
||
| 77 | $worksheet->getRowDimension($col)->setRowHeight($titleHeight); // 行高度 |
||
| 78 | } |
||
| 79 | $rowName = WorkSheetHelper::cellName($col); |
||
| 80 | $worksheet->getStyle($rowName . $row)->getAlignment()->setWrapText(true); // 自动换行 |
||
| 81 | |||
| 82 | if (is_array($val)) { |
||
| 83 | $num = 1; |
||
| 84 | $_cols = $col; |
||
| 85 | foreach ($val as $k => $v) { |
||
| 86 | if (!isset($titleConfig['title_show']) || $titleConfig['title_show'] !== false) { |
||
| 87 | // 检查子标题是否必填 |
||
| 88 | $cellValue = self::formatHeaderTitle($k, $v, $requiredFields); |
||
| 89 | $worksheet->setCellValue(WorkSheetHelper::cellName($_cols) . ($row + 1), $cellValue); |
||
| 90 | } |
||
| 91 | if (!empty($titleWidth)) { |
||
| 92 | $worksheet->getColumnDimension(WorkSheetHelper::cellName($_cols))->setWidth($titleWidth); // 列宽度 |
||
| 93 | } else { |
||
| 94 | $worksheet->getColumnDimension(WorkSheetHelper::cellName($_cols))->setAutoSize(true); // 自动计算宽度 |
||
| 95 | } |
||
| 96 | if ($num < count($val)) { |
||
| 97 | $col++; |
||
| 98 | $num++; |
||
| 99 | } |
||
| 100 | $_cols++; |
||
| 101 | } |
||
| 102 | $worksheet->mergeCells($_merge . $row . ':' . WorkSheetHelper::cellName($col) . $row); |
||
| 103 | if (!isset($titleConfig['title_show']) || $titleConfig['title_show'] !== false) { |
||
| 104 | // 检查主标题是否必填(根据第一个子字段判断) |
||
| 105 | $firstSubField = reset($val); |
||
| 106 | $cellValue = self::formatHeaderTitle($key, $firstSubField, $requiredFields); |
||
| 107 | $worksheet->setCellValue($_merge . $row, $cellValue); |
||
| 108 | } |
||
| 109 | } else { |
||
| 110 | if ($titleRow != 1) { |
||
| 111 | $worksheet->mergeCells($rowName . $row . ':' . $rowName . ($row + $titleRow - 1)); |
||
| 112 | } |
||
| 113 | if (!isset($titleConfig['title_show']) || $titleConfig['title_show'] !== false) { |
||
| 114 | // 检查字段是否必填并格式化标题 |
||
| 115 | $cellValue = self::formatHeaderTitle($key, $val, $requiredFields); |
||
| 116 | $worksheet->setCellValue($rowName . $row, $cellValue); |
||
| 117 | } |
||
| 118 | if (!empty($titleWidth)) { |
||
| 119 | $worksheet->getColumnDimension($rowName)->setWidth($titleWidth); // 列宽度 |
||
| 120 | } else { |
||
| 121 | $worksheet->getColumnDimension($rowName)->setAutoSize(true); // 自动计算宽度 |
||
| 122 | } |
||
| 123 | } |
||
| 124 | $col++; |
||
| 125 | $_merge = WorkSheetHelper::cellName($col); |
||
| 126 | } |
||
| 127 | $row += $titleRow; // 当前行数 |
||
| 128 | |||
| 129 | return ['col' => $col, 'row' => $row]; |
||
| 130 | } |
||
| 162 |