Handle empty rows without E_WARNING when filling missing array indexes (#385)

In some cases, reading an XLSX file produce E_WARNING from the max()
call in the fillMissingArray() method. This commit fix the problem
by handling empty rows.
This commit is contained in:
Stian Liknes 2017-02-19 11:30:35 +01:00 committed by Adrien Loison
parent 6f4ddb1569
commit 1ce931a424
2 changed files with 19 additions and 5 deletions

View File

@ -31,6 +31,9 @@ class CellHelper
*/ */
public static function fillMissingArrayIndexes($dataArray, $fillValue = '') public static function fillMissingArrayIndexes($dataArray, $fillValue = '')
{ {
if (empty($dataArray)) {
return [];
}
$existingIndexes = array_keys($dataArray); $existingIndexes = array_keys($dataArray);
$newIndexes = array_fill_keys(range(0, max($existingIndexes)), $fillValue); $newIndexes = array_fill_keys(range(0, max($existingIndexes)), $fillValue);

View File

@ -10,14 +10,25 @@ namespace Box\Spout\Reader\XLSX\Helper;
class CellHelperTest extends \PHPUnit_Framework_TestCase class CellHelperTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @return void * @return array
*/ */
public function testFillMissingArrayIndexes() public function dataProviderForTestFillMissingArrayIndexes()
{ {
$arrayToFill = [1 => 1, 3 => 3]; return [
$filledArray = CellHelper::fillMissingArrayIndexes($arrayToFill, 'FILL'); [ null, [] ],
[ [], [] ],
[ [1 => 1, 3 => 3], ['FILL', 1, 'FILL', 3] ]
];
}
$expectedFilledArray = ['FILL', 1, 'FILL', 3]; /**
* @dataProvider dataProviderForTestFillMissingArrayIndexes
* @param array $arrayToFill
* @param array $expectedFilledArray
*/
public function testFillMissingArrayIndexes($arrayToFill, array $expectedFilledArray)
{
$filledArray = CellHelper::fillMissingArrayIndexes($arrayToFill, 'FILL');
$this->assertEquals($expectedFilledArray, $filledArray); $this->assertEquals($expectedFilledArray, $filledArray);
} }