Completed
Push — master ( ecd0f8...3ad52b )
by ignace nyamagana
29:57 queued 15:08
created

functions.php ➔ bom_match()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 14
ccs 4
cts 4
cp 1
crap 3
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * League.Csv (https://csv.thephpleague.com).
5
 *
6
 * @author  Ignace Nyamagana Butera <[email protected]>
7
 * @license https://github.com/thephpleague/csv/blob/master/LICENSE (MIT License)
8
 * @version 9.1.5
9
 * @link    https://github.com/thephpleague/csv
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
declare(strict_types=1);
16
17
namespace League\Csv {
18
19
    use ReflectionClass;
20
    use Traversable;
21
    use function array_fill_keys;
22
    use function array_filter;
23
    use function array_reduce;
24
    use function array_unique;
25
    use function count;
26
    use function is_array;
27
    use function iterator_to_array;
28
    use function strpos;
29
30
    /**
31 22
     * Returns the BOM sequence found at the start of the string.
32
     *
33 22
     * If no valid BOM sequence is found an empty string is returned
34
     */
35 22
    function bom_match(string $str): string
36 22
    {
37 22
        static $list;
38
39
        $list = $list ?? (new ReflectionClass(ByteSequence::class))->getConstants();
40
41 18
        foreach ($list as $sequence) {
42
            if (0 === strpos($str, $sequence)) {
43
                return $sequence;
44
            }
45
        }
46
47
        return '';
48
    }
49
50
    /**
51
     * Detect Delimiters usage in a {@link Reader} object.
52
     *
53
     * Returns a associative array where each key represents
54
     * a submitted delimiter and each value the number CSV fields found
55
     * when processing at most $limit CSV records with the given delimiter
56
     *
57
     * @param string[] $delimiters
58
     *
59
     * @return int[]
60 12
     */
61 12
    function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array
62
    {
63 6
        $found = array_unique(array_filter($delimiters, function (string $value): bool {
64 8
            return 1 == strlen($value);
65
        }));
66 6
        $stmt = (new Statement())->limit($limit)->where(function (array $record): bool {
67
            return count($record) > 1;
68 6
        });
69 8
        $reducer = function (array $result, string $delimiter) use ($csv, $stmt): array {
70 8
            $result[$delimiter] = count(iterator_to_array($stmt->process($csv->setDelimiter($delimiter)), false), COUNT_RECURSIVE);
71 8
72 8
            return $result;
73 8
        };
74 8
        $delimiter = $csv->getDelimiter();
75
        $header_offset = $csv->getHeaderOffset();
76 8
        $csv->setHeaderOffset(null);
77
        $stats = array_reduce($found, $reducer, array_fill_keys($delimiters, 0));
78
        $csv->setHeaderOffset($header_offset)->setDelimiter($delimiter);
79
80
        return $stats;
81
    }
82
83
    /**
84
     * Tell whether the content of the variable is iterable.
85
     *
86
     * @see http://php.net/manual/en/function.is-iterable.php
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
    function is_nullable_int($value): bool
99
    {
100
        return null === $value || is_int($value);
101
    }
102
}
103
104 6
namespace {
105
106
    use League\Csv;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
107
108
    if (PHP_VERSION_ID < 70100 && !function_exists('\is_iterable')) {
109
        function is_iterable($iterable)
110
        {
111
            return Csv\is_iterable($iterable);
112
        }
113
    }
114
}
115