Completed
Push — master ( 186601...ba0e83 )
by ignace nyamagana
03:37
created

functions.php ➔ delimiter_detect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 3
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 1
rs 9.3142
c 0
b 0
f 0
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
declare(strict_types=1);
14
15
namespace League\Csv;
16
17
use ReflectionClass;
18
19
/**
20
 * Returns the BOM sequence found at the start of the string
21
 *
22
 * If no valid BOM sequence is found an empty string is returned
23
 *
24
 * @param string $str
25
 *
26
 * @return string
27
 */
28
function bom_match(string $str): string
29
{
30 22
    static $list;
31
32 22
    $list = $list ?? (new ReflectionClass(ByteSequence::class))->getConstants();
33
34 22
    foreach ($list as $sequence) {
35 22
        if (0 === strpos($str, $sequence)) {
36 13
            return $sequence;
37
        }
38
    }
39
40 18
    return '';
41
}
42
43
/**
44
 * Detect Delimiters usage in a CSV object
45
 *
46
 * Returns a associative array where each key represents
47
 * a submitted delimiter and each value the number CSV fields found
48
 * when processing at most $limit CSV records with the given delimiter
49
 *
50
 * @param Reader   $csv        the CSV object
51
 * @param string[] $delimiters the delimiters to consider
52
 * @param int      $limit      Detection is made using up to $limit CSV records
53
 *
54
 * @return int[string]
0 ignored issues
show
Documentation introduced by
The doc-type int[string] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
55
 */
56
function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array
57
{
58
    $found = array_unique(array_filter($delimiters, function (string $value): bool {
59 12
        return 1 == strlen($value);
60 12
    }));
61
    $stmt = (new Statement())->limit($limit)->where(function (array $record): bool {
62 6
        return count($record) > 1;
63 8
    });
64
    $reducer = function (array $result, string $delimiter) use ($csv, $stmt): array {
65 6
        $result[$delimiter] = count($stmt->process($csv->setDelimiter($delimiter))->fetchAll(), COUNT_RECURSIVE);
66
67 6
        return $result;
68 8
    };
69 8
    $delimiter = $csv->getDelimiter();
70 8
    $header_offset = $csv->getHeaderOffset();
71 8
    $csv->setHeaderOffset(null);
72 8
    $stats = array_reduce($found, $reducer, array_fill_keys($delimiters, 0));
73 8
    $csv->setHeaderOffset($header_offset)->setDelimiter($delimiter);
74
75 8
    return $stats;
76
}
77