Completed
Pull Request — master (#288)
by ignace nyamagana
03:05
created

delimiter_detect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 3
dl 0
loc 20
ccs 13
cts 13
cp 1
crap 1
rs 9.4285
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.1.3
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
    use Traversable;
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 22
        static $list;
32
33 22
        $list = $list ?? (new ReflectionClass(ByteSequence::class))->getConstants();
34
35 22
        foreach ($list as $sequence) {
36 22
            if (0 === strpos($str, $sequence)) {
37 22
                return $sequence;
38
            }
39
        }
40
41 18
        return '';
42
    }
43
44
    /**
45
     * Detect Delimiters usage in a {@link Reader} object
46
     *
47
     * Returns a associative array where each key represents
48
     * a submitted delimiter and each value the number CSV fields found
49
     * when processing at most $limit CSV records with the given delimiter
50
     *
51
     * @param Reader   $csv        the CSV object
52
     * @param string[] $delimiters list of delimiters to consider
53
     * @param int      $limit      Detection is made using up to $limit records
54
     *
55
     * @return int[]
56
     */
57
    function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array
58
    {
59
        $found = array_unique(array_filter($delimiters, function (string $value): bool {
60 12
            return 1 == strlen($value);
61 12
        }));
62
        $stmt = (new Statement())->limit($limit)->where(function (array $record): bool {
63 6
            return count($record) > 1;
64 8
        });
65
        $reducer = function (array $result, string $delimiter) use ($csv, $stmt): array {
66 6
            $result[$delimiter] = count(iterator_to_array($stmt->process($csv->setDelimiter($delimiter)), false), COUNT_RECURSIVE);
67
68 6
            return $result;
69 8
        };
70 8
        $delimiter = $csv->getDelimiter();
71 8
        $header_offset = $csv->getHeaderOffset();
72 8
        $csv->setHeaderOffset(null);
73 8
        $stats = array_reduce($found, $reducer, array_fill_keys($delimiters, 0));
74 8
        $csv->setHeaderOffset($header_offset)->setDelimiter($delimiter);
75
76 8
        return $stats;
77
    }
78
79
    /**
80
     * Tell whether the content of the variable is iterable
81
     *
82
     * @see http://php.net/manual/en/function.is-iterable.php
83
     *
84
     * @param mixed $iterable
85
     *
86
     * @return bool
87
     */
88
    function is_iterable($iterable): bool
89
    {
90
        return is_array($iterable) || $iterable instanceof Traversable;
91
    }
92
93
    /**
94
     * Tell whether the content of the variable is an int or null
95
     *
96
     * @see https://wiki.php.net/rfc/nullable_types
97
     *
98
     * @param mixed $value
99
     *
100
     * @return bool
101
     */
102
    function is_nullable_int($value): bool
103
    {
104 6
        return null === $value || is_int($value);
105
    }
106
}
107
108
namespace {
109
110
    use League\Csv;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
111
112
    if (PHP_VERSION_ID < 70100 && !function_exists('\is_iterable')) {
113
        function is_iterable($iterable)
114
        {
115
            return Csv\is_iterable($iterable);
116
        }
117
    }
118
}
119