Conditions | 11 |
Paths | 29 |
Total Lines | 56 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
47 | public static function getMultiArrayFromString(string $string, array $header, array $options = []): array |
||
48 | { |
||
49 | $nbRows = StringUtils::countRows($string); |
||
50 | if (isset($options['nb_max_row']) && $nbRows > $options['nb_max_row']) { |
||
51 | throw new MultiArrayNbMaxRowsException($options['nb_max_row'], $nbRows); |
||
52 | } |
||
53 | |||
54 | $toReturn = self::getArrayFromString($string); |
||
55 | $nbHeader = count($header); |
||
56 | $headerConsistencyError = []; |
||
57 | |||
58 | // options config |
||
59 | $delimiterOption = $options['delimiter'] ?? ','; |
||
60 | $fixHeaderConsistencyOption = (isset($options['fix_header_consistency']) && $options['fix_header_consistency']); |
||
61 | |||
62 | foreach ($toReturn as $key => $row) { |
||
63 | $rowData = str_getcsv($row, $delimiterOption); |
||
64 | |||
65 | // @todo extract in method mapTrimNull |
||
66 | $rowData = array_map(function ($data) { |
||
67 | if ($data !== null) { |
||
68 | $data = trim($data); |
||
69 | |||
70 | if ($data === '') { |
||
71 | $data = null; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return $data; |
||
76 | }, $rowData); |
||
77 | |||
78 | $validRowConsistency = true; |
||
79 | if ($fixHeaderConsistencyOption) { |
||
80 | // @todo extract in method syncHeader |
||
81 | // fill missing field on the row with null values |
||
82 | $rowData = array_pad($rowData, $nbHeader, null); |
||
83 | // slice extra data on the row |
||
84 | $rowData = array_slice($rowData, 0, $nbHeader); |
||
85 | } else { |
||
86 | if (count($rowData) != $nbHeader) { |
||
87 | $headerConsistencyError[] = $key + 1; |
||
88 | $validRowConsistency = false; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | // combine to form the multidimensional array |
||
93 | if ($validRowConsistency) { |
||
94 | $toReturn[$key] = array_combine($header, $rowData); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | if (count($headerConsistencyError) > 0) { |
||
99 | throw new MultiArrayHeaderConsistencyException($headerConsistencyError); |
||
100 | } |
||
101 | |||
102 | return $toReturn; |
||
103 | } |
||
105 |