Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Fix crash when using associative array with empty row (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrilo authored Nov 3, 2016
1 parent 9ce7740 commit e276b43
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Spout/Writer/XLSX/Internal/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public function addRow($dataRow, $style)
private function isEmptyRow($dataRow)
{
$numCells = count($dataRow);
return ($numCells === 1 && CellHelper::isEmpty($dataRow[0]));
// using "reset()" instead of "$dataRow[0]" because $dataRow can be an associative array
return ($numCells === 1 && CellHelper::isEmpty(reset($dataRow)));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Spout/Writer/XLSX/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public function testAddRowShouldNotWriteEmptyRows()
$dataRows = [
[''],
['xlsx--21', 'xlsx--22'],
[''],
['key' => ''],
[''],
['xlsx--51', 'xlsx--52'],
];
Expand Down

3 comments on commit e276b43

@j4y-funabashi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, just spent the morning debugging and fixing this, came to submit the pull request and discover you have already patched it!

@j4y-funabashi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only difference is I used array_values($dataRow)[0]

@adrilo
Copy link
Collaborator Author

@adrilo adrilo commented on e276b43 Nov 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array_values($dataRow)[0] is not as efficient as reset as you have to go through the array once, store this in a temporary variable to finally access its first cell. reset will only move a pointer.
While on small arrays you won't see much difference, the difference is huge for bigger arrays.

Please sign in to comment.