| Conditions | 16 |
| Paths | 47 |
| Total Lines | 39 |
| Code Lines | 28 |
| 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 |
||
| 28 | public static function detectCsvSeparator(string $filePath): ?string |
||
| 29 | { |
||
| 30 | if (!is_readable($filePath)) { |
||
| 31 | return null; |
||
| 32 | } |
||
| 33 | $h = @fopen($filePath, 'rb'); |
||
| 34 | if (!$h) { |
||
|
|
|||
| 35 | return null; |
||
| 36 | } |
||
| 37 | $line = ''; |
||
| 38 | for ($i = 0; $i < 5 && !feof($h); $i++) { |
||
| 39 | $line = fgets($h, 1024 * 1024); |
||
| 40 | if ($line !== false && trim($line) !== '') { |
||
| 41 | break; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | fclose($h); |
||
| 45 | if ($line === false || $line === '') { |
||
| 46 | return null; |
||
| 47 | } |
||
| 48 | |||
| 49 | $cands = [',', ';', "\t", '|']; |
||
| 50 | $scores = array_fill_keys($cands, 0); |
||
| 51 | $inQuotes = false; |
||
| 52 | $len = strlen($line); |
||
| 53 | for ($i = 0; $i < $len; $i++) { |
||
| 54 | $ch = $line[$i]; |
||
| 55 | if ($ch === '"') { |
||
| 56 | $prev = $i > 0 ? $line[$i - 1] : null; |
||
| 57 | if ($prev !== '\\') { |
||
| 58 | $inQuotes = !$inQuotes; |
||
| 59 | } |
||
| 60 | } elseif (!$inQuotes && isset($scores[$ch])) { |
||
| 61 | $scores[$ch]++; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | arsort($scores); |
||
| 65 | $top = array_key_first($scores); |
||
| 66 | return ($scores[$top] > 0) ? $top : null; |
||
| 67 | } |
||
| 138 |