Completed
Push — master ( 48cbe3...3422ff )
by ignace nyamagana
05:36 queued 03:38
created

functions.php ➔ delimiter_detect()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 22
nc 2
nop 3
dl 0
loc 40
ccs 21
cts 21
cp 1
crap 2
rs 8.8571
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 League\Csv\Exception\OutOfRangeException;
18
use ReflectionClass;
19
20
/**
21
 * Returns the BOM sequence found at the start of the string
22
 *
23
 * If no valid BOM sequence is found an empty string is returned
24
 *
25
 * @param string $str
26
 *
27
 * @return string
28
 */
29
function bom_match(string $str): string
30
{
31 16
    static $list;
32
33 16
    $list = $list ?? (new ReflectionClass(ByteSequence::class))->getConstants();
34
35 16
    foreach ($list as $sequence) {
36 16
        if (0 === strpos($str, $sequence)) {
37 10
            return $sequence;
38
        }
39
    }
40
41 12
    return '';
42
}
43
44
/**
45
 * Detect Delimiters occurences in the CSV
46
 *
47
 * Returns a associative array where each key represents
48
 * a valid delimiter and each value the number of occurences
49
 *
50
 * @param Reader   $csv        the CSV object
51
 * @param string[] $delimiters the delimiters to consider
52
 * @param int      $nb_records Detection is made using $nb_records of the CSV
53
 *
54
 * @return array
55
 */
56
function delimiter_detect(Reader $csv, array $delimiters, int $nb_records = 1): array
57
{
58
    $filter = function ($value): bool {
59 8
        return 1 == strlen($value);
60 4
    };
61 8
    $delimiters = array_unique(array_filter($delimiters, $filter));
62
63 8
    if ($nb_records < 1) {
64 2
        throw new OutOfRangeException(sprintf(__FUNCTION__.'() expects the nb_records argument to be a valid positive integer, %s given', $nb_records));
65
    }
66
67 6
    $stmt = (new Statement())
68
        ->where(function (array $record): bool {
69 6
            return count($record) > 1;
70 6
        })
71 6
        ->limit($nb_records)
72
    ;
73
74
    //cache CSV settings
75 6
    $csv_delimiter = $csv->getDelimiter();
76 6
    $csv_header_offset = $csv->getHeaderOffset();
77 6
    $csv->setHeaderOffset(null);
78
79
    $reducer = function (array $result, string $delimiter) use ($csv, $stmt): array {
80 6
        $result[$delimiter] = count(
81 6
            $stmt->process($csv->setDelimiter($delimiter))->fetchAll(),
82 6
            COUNT_RECURSIVE
83
        );
84
85 6
        return $result;
86 6
    };
87
88 6
    $result = array_reduce($delimiters, $reducer, []);
89
90
    //restore original CSV settings
91 6
    $csv->setHeaderOffset($csv_header_offset)->setDelimiter($csv_delimiter);
92
93 6
    arsort($result, SORT_NUMERIC);
94 6
    return $result;
95
}
96