These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 |
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.