Fix infinite loop for CSV with all lines empty
Only occured with multiline CSV files
This commit is contained in:
parent
16d0290a17
commit
a1a1077677
@ -111,26 +111,28 @@ class RowIterator implements IteratorInterface
|
|||||||
*/
|
*/
|
||||||
public function next()
|
public function next()
|
||||||
{
|
{
|
||||||
$lineData = false;
|
|
||||||
$this->hasReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer);
|
$this->hasReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer);
|
||||||
|
|
||||||
if (!$this->hasReachedEndOfFile) {
|
if ($this->hasReachedEndOfFile) {
|
||||||
do {
|
return;
|
||||||
$utf8EncodedLineData = $this->getNextUTF8EncodedLine();
|
}
|
||||||
if ($utf8EncodedLineData !== false) {
|
|
||||||
$lineData = $this->globalFunctionsHelper->str_getcsv($utf8EncodedLineData, $this->fieldDelimiter, $this->fieldEnclosure);
|
|
||||||
}
|
|
||||||
$hasNowReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer);
|
|
||||||
} while (($lineData === false && !$hasNowReachedEndOfFile) || $this->isEmptyLine($lineData));
|
|
||||||
|
|
||||||
if ($lineData !== false) {
|
do {
|
||||||
$this->rowDataBuffer = $lineData;
|
$lineData = false;
|
||||||
$this->numReadRows++;
|
$utf8EncodedLineData = $this->getNextUTF8EncodedLine();
|
||||||
} else {
|
if ($utf8EncodedLineData !== false) {
|
||||||
// If we reach this point, it means end of file was reached.
|
$lineData = $this->globalFunctionsHelper->str_getcsv($utf8EncodedLineData, $this->fieldDelimiter, $this->fieldEnclosure);
|
||||||
// This happens when the last lines are empty lines.
|
|
||||||
$this->hasReachedEndOfFile = $hasNowReachedEndOfFile;
|
|
||||||
}
|
}
|
||||||
|
$hasNowReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer);
|
||||||
|
} while (($lineData === false && !$hasNowReachedEndOfFile) || $this->isEmptyLine($lineData));
|
||||||
|
|
||||||
|
if ($lineData !== false) {
|
||||||
|
$this->rowDataBuffer = $lineData;
|
||||||
|
$this->numReadRows++;
|
||||||
|
} else {
|
||||||
|
// If we reach this point, it means end of file was reached.
|
||||||
|
// This happens when the last lines are empty lines.
|
||||||
|
$this->hasReachedEndOfFile = $hasNowReachedEndOfFile;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,6 +142,29 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($expectedRows, $allRows);
|
$this->assertEquals($expectedRows, $allRows);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function dataProviderForTestReadShouldReadEmptyFile()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['csv_empty.csv'],
|
||||||
|
['csv_all_lines_empty.csv'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataProviderForTestReadShouldReadEmptyFile
|
||||||
|
*
|
||||||
|
* @param string $fileName
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testReadShouldReadEmptyFile($fileName)
|
||||||
|
{
|
||||||
|
$allRows = $this->getAllRowsForFile($fileName);
|
||||||
|
$this->assertEquals([], $allRows);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -3,3 +3,6 @@ require_once(dirname(__DIR__) . '/vendor/autoload.php');
|
|||||||
|
|
||||||
require_once(dirname(__DIR__) . '/tests/Spout/TestUsingResource.php');
|
require_once(dirname(__DIR__) . '/tests/Spout/TestUsingResource.php');
|
||||||
require_once(dirname(__DIR__) . '/tests/Spout/ReflectionHelper.php');
|
require_once(dirname(__DIR__) . '/tests/Spout/ReflectionHelper.php');
|
||||||
|
|
||||||
|
// Make sure a timezone is set to be able to work with dates
|
||||||
|
date_default_timezone_set('UTC');
|
||||||
|
2
tests/resources/csv/csv_all_lines_empty.csv
Normal file
2
tests/resources/csv/csv_all_lines_empty.csv
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
|
|
0
tests/resources/csv/csv_empty.csv
Normal file
0
tests/resources/csv/csv_empty.csv
Normal file
|
Loading…
x
Reference in New Issue
Block a user