| Conditions | 15 |
| Paths | 38 |
| Total Lines | 65 |
| Code Lines | 34 |
| 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 |
||
| 30 | public static function setCellValue( |
||
| 31 | Worksheet $worksheet, |
||
| 32 | array $val, |
||
| 33 | array $fields, |
||
| 34 | int $row, |
||
| 35 | int $titleRow, |
||
| 36 | ?int $height = null, |
||
| 37 | bool $autoDataType = false, |
||
| 38 | bool $format = true, |
||
| 39 | string $formatDate = 'Y-m-d H:i:s' |
||
| 40 | ): int { |
||
| 41 | // 设置单元格行高 |
||
| 42 | if (!empty($height)) { |
||
| 43 | $worksheet->getRowDimension($row)->setRowHeight($height); |
||
| 44 | } |
||
| 45 | |||
| 46 | $_lie = 0; |
||
| 47 | foreach ($fields as $v) { |
||
| 48 | $rowName = WorkSheetHelper::cellName($_lie); |
||
| 49 | |||
| 50 | // 处理嵌套字段(如 'user.name') |
||
| 51 | if (strpos($v, '.') !== false) { |
||
| 52 | $v = explode('.', $v); |
||
| 53 | $content = $val; |
||
| 54 | for ($i = 0; $i < count($v); $i++) { |
||
|
|
|||
| 55 | $content = $content[$v[$i]] ?? ''; |
||
| 56 | } |
||
| 57 | } elseif ($v == '_id') { |
||
| 58 | $content = $row - $titleRow; // 自增序号列 |
||
| 59 | } else { |
||
| 60 | $content = ($val[$v] ?? ''); |
||
| 61 | } |
||
| 62 | |||
| 63 | // 处理图片类型 |
||
| 64 | if (is_array($content) && isset($content['type']) && isset($content['content'])) { |
||
| 65 | if ($content['type'] == 'image') { |
||
| 66 | self::setImage($worksheet, $content, $rowName, $row); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | // 处理公式类型 |
||
| 70 | elseif (is_array($content) && isset($content['formula'])) { |
||
| 71 | $worksheet->setCellValueExplicit( |
||
| 72 | $rowName . $row, |
||
| 73 | $content['formula'], |
||
| 74 | DataType::TYPE_FORMULA |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | // 处理普通值 |
||
| 78 | else { |
||
| 79 | $content = WorkSheetHelper::formatValue($content, $format, $formatDate); // 格式化数据 |
||
| 80 | if (is_numeric($content)) { |
||
| 81 | if ($autoDataType && strlen($content) < 11) { |
||
| 82 | $worksheet->setCellValueExplicit($rowName . $row, $content, DataType::TYPE_NUMERIC); |
||
| 83 | } else { |
||
| 84 | $worksheet->setCellValueExplicit($rowName . $row, $content, DataType::TYPE_STRING2); |
||
| 85 | } |
||
| 86 | } else { |
||
| 87 | $worksheet->setCellValueExplicit($rowName . $row, $content, DataType::TYPE_STRING2); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | $_lie++; |
||
| 91 | } |
||
| 92 | $row++; |
||
| 93 | |||
| 94 | return $row; |
||
| 95 | } |
||
| 132 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: