From a5c7a65ec37a1ba6736ffe402b28dbfe50e04bab Mon Sep 17 00:00:00 2001 From: rahul Date: Wed, 20 Sep 2023 13:04:42 +0530 Subject: [PATCH] fix complexity Signed-off-by: rahul --- src/CsvFileHandler.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/CsvFileHandler.php b/src/CsvFileHandler.php index 1d2734d..a7a6cd4 100644 --- a/src/CsvFileHandler.php +++ b/src/CsvFileHandler.php @@ -171,6 +171,7 @@ private function setColumnsToHide(array &$headers, array $hideColumns): array /** * @param string $filename * @param array|false $hideColumns + * @param int|false $limit * @return Generator * @throws FileHandlerException */ @@ -180,18 +181,18 @@ private function getRows(string $filename, array|false $hideColumns = false, int if (!$csvFile) { throw new FileHandlerException('file not found'); } + $headers = $this->extractHeader($csvFile); if (!is_array($headers)) { + fclose($csvFile); throw new FileHandlerException('could not extract header'); } - if (is_array($hideColumns)) { - $indices = $this->setColumnsToHide($headers, $hideColumns); - } - + $indices = is_array($hideColumns) ? $this->setColumnsToHide($headers, $hideColumns) : []; $isEmptyFile = true; $count = 0; + try { while (($row = fgetcsv($csvFile)) !== false) { $isEmptyFile = false; @@ -203,7 +204,6 @@ private function getRows(string $filename, array|false $hideColumns = false, int $this->removeElementByIndex($row, $indices); } - $item = array_combine($headers, $row); yield $item; $count++; @@ -216,12 +216,12 @@ private function getRows(string $filename, array|false $hideColumns = false, int fclose($csvFile); } - if ($isEmptyFile) { throw new FileHandlerException('invalid csv file format'); } } + /** * @param array $row * @param array, int> $indices