| Conditions | 12 |
| Paths | 24 |
| Total Lines | 37 |
| Code Lines | 23 |
| 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 |
||
| 34 | public static function csvToArray($input, $fieldDelimiter = ',', $fieldEnclosure = '"', $maximumColumns = 0) |
||
| 35 | { |
||
| 36 | $multiArray = []; |
||
| 37 | $maximumCellCount = 0; |
||
| 38 | |||
| 39 | if (($handle = fopen('php://memory', 'r+')) !== false) { |
||
| 40 | fwrite($handle, $input); |
||
| 41 | rewind($handle); |
||
| 42 | while (($cells = fgetcsv($handle, 0, $fieldDelimiter, $fieldEnclosure)) !== false) { |
||
| 43 | $cells = is_array($cells) ? $cells : []; |
||
| 44 | $maximumCellCount = max(count($cells), $maximumCellCount); |
||
| 45 | $multiArray[] = preg_replace('|<br */?>|i', LF, $cells); |
||
| 46 | } |
||
| 47 | fclose($handle); |
||
| 48 | } |
||
| 49 | |||
| 50 | if ($maximumColumns > $maximumCellCount) { |
||
| 51 | $maximumCellCount = $maximumColumns; |
||
| 52 | } |
||
| 53 | |||
| 54 | foreach ($multiArray as &$row) { |
||
| 55 | for ($key = 0; $key < $maximumCellCount; $key++) { |
||
| 56 | if ( |
||
| 57 | $maximumColumns > 0 |
||
| 58 | && $maximumColumns < $maximumCellCount |
||
| 59 | && $key >= $maximumColumns |
||
| 60 | ) { |
||
| 61 | if (isset($row[$key])) { |
||
| 62 | unset($row[$key]); |
||
| 63 | } |
||
| 64 | } elseif (!isset($row[$key])) { |
||
| 65 | $row[$key] = ''; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | return $multiArray; |
||
| 71 | } |
||
| 90 |