Completed
Push — master ( a327ab...c6c647 )
by ignace nyamagana
55:37 queued 54:07
created

functions.php ➔ is_nullable_int()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

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