Skip to content

Commit

Permalink
Merge pull request #101 from boekkooi/patch-1
Browse files Browse the repository at this point in the history
CSVReader::current() avoid recursion
  • Loading branch information
Baachi committed Jul 25, 2014
2 parents d58d326 + 813cebd commit 87283d9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Ddeboer/DataImport/Reader/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure =
*/
public function current()
{
$line = $this->file->current();
// If the CSV has no column headers just return the line
if (empty($this->columnHeaders)) {
return $this->file->current();
}

// Since the CSV has column headers use them to construct an associative array for the columns in this line
do {
$line = $this->file->current();

// If the CSV has column headers, use them to construct an associative
// array for the columns in this line
if (!empty($this->columnHeaders)) {
// In non-strict mode pad/slice the line to match the column headers
if (!$this->isStrict()) {
if ($this->headersCount > count($line)) {
Expand All @@ -135,13 +139,10 @@ public function current()
if ($this->valid()) {
$this->errors[$this->key()] = $line;
$this->next();

return $this->current();
}
}
} while($this->valid());

// Else just return the column values
return $line;
return null;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Reader/CsvReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,37 @@ public function testDuplicateHeadersMerge()
$this->assertEquals($expected, $current);
}

public function testMaximumNesting()
{
if (!function_exists('xdebug_is_enabled')) {
$this->markTestSkipped('xDebug is not installed');
}

$xdebug_start = !xdebug_is_enabled();
if ($xdebug_start) {
xdebug_enable();
}

ini_set('xdebug.max_nesting_level', 200);

$file = new \SplTempFileObject();
for($i = 0; $i < 500; $i++) {
$file->fwrite("1,2,3\n");
}

$reader = new CsvReader($file);
$reader->rewind();
$reader->setStrict(true);
$reader->setColumnHeaders(array('one','two'));

$current = $reader->current();
$this->assertEquals(null, $current);

if ($xdebug_start) {
xdebug_disable();
}
}

protected function getReader($filename)
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/'.$filename);
Expand Down

0 comments on commit 87283d9

Please sign in to comment.