Skip to content

Commit

Permalink
Improve Info and function codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Oct 13, 2024
1 parent d70d5af commit 6b2e346
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
8 changes: 3 additions & 5 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

require __DIR__ . '/src/functions_include.php';

spl_autoload_register(static function (string $class_name): void {

$prefix = 'League\Csv\\';
if (!str_starts_with($class_name, $prefix)) {
spl_autoload_register(static function (string $class): void {
if (!str_starts_with($class, 'League\Csv\\')) {
return;
}

$file = __DIR__ . '/src/' . str_replace('\\', '/', substr($class_name, 11)) . '.php';
$file = __DIR__ . '/src/' . str_replace('\\', '/', substr($class, 11)).'.php';
if (is_readable($file)) {
require $file;
}
Expand Down
32 changes: 12 additions & 20 deletions src/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,27 @@ public static function fetchBOMSequence(string $str): ?string
* when processing at most $limit CSV records with the given delimiter
*
* @param array<string> $delimiters
* @param int<-1, max> $limit
*
* @return array<string, int>
*/
public static function getDelimiterStats(Reader $csv, array $delimiters, int $limit = 1): array
{
$stmt = Statement::create()->offset(0)->limit($limit);

$delimiterStats = function (array $stats, string $delimiter) use ($csv, $stmt): array {
$csv->setDelimiter($delimiter);
$foundRecords = [];
foreach ($stmt->process($csv) as $record) {
if (1 < count($record)) {
$foundRecords[] = $record;
}
}

$stats[$delimiter] = count($foundRecords, COUNT_RECURSIVE);

return $stats;
};

$currentDelimiter = $csv->getDelimiter();
$currentHeaderOffset = $csv->getHeaderOffset();

$csv->setHeaderOffset(null);
$currentDelimiter = $csv->getDelimiter();

$stats = array_reduce(
array_unique(array_filter($delimiters, fn (string $value): bool => 1 === strlen($value))),
$delimiterStats,
fn (array $stats, string $delimiter): array => [
...$stats,
...[$delimiter => count([
...$csv
->setHeaderOffset(null)
->setDelimiter($delimiter)
->slice(0, $limit)
->filter(fn (array $record, int|string $key): bool => 1 < count($record)),
], COUNT_RECURSIVE)],
],
array_fill_keys($delimiters, 0)
);

Expand Down
6 changes: 3 additions & 3 deletions src/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ final class InfoTest extends TestCase
{
public function testDetectDelimiterListWithInvalidRowLimit(): void
{
$this->expectException(Exception::class);

$file = new SplTempFileObject();
$file->fwrite("How are you today ?\nI'm doing fine thanks!");
$csv = Reader::createFromFileObject($file);

Info::getDelimiterStats($csv, [','], -4);
$this->expectException(Exception::class);

Info::getDelimiterStats($csv, [','], -4); /* @phpstan-ignore-line */
}

public function testDetectDelimiterListWithInvalidDelimiter(): void
Expand Down
11 changes: 7 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace League\Csv;

/**
* DEPRECATION WARNING! This class will be removed in the next major point release.
* DEPRECATION WARNING! This namespace function will be removed in the next major point release.
*
* @deprecated since version 9.7.0
* @see Bom::tryFromSequence()
Expand All @@ -30,19 +30,22 @@ function bom_match(string $str): string
}

/**
* @param array<string> $delimiters
* DEPRECATION WARNING! This namespace function will be removed in the next major point release.
*
* @return array<string,int>
* @deprecated since version 9.7.0
* @see Info::getDelimiterStats()
* @codeCoverageIgnore
*
* Detect Delimiters usage in a {@link Reader} object.
*
* Returns a associative array where each key represents
* a submitted delimiter and each value the number CSV fields found
* a submitted delimiter and each value the number of CSV fields found
* when processing at most $limit CSV records with the given delimiter
*
* @param array<string> $delimiters
* @param int<-1, max> $limit
*
* @return array<string,int>
*/
function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array
{
Expand Down

0 comments on commit 6b2e346

Please sign in to comment.