From 1ce931a4241d118013589d2da48da31349653e32 Mon Sep 17 00:00:00 2001 From: Stian Liknes Date: Sun, 19 Feb 2017 11:30:35 +0100 Subject: [PATCH] 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. --- src/Spout/Reader/XLSX/Helper/CellHelper.php | 3 +++ .../Reader/XLSX/Helper/CellHelperTest.php | 21 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Spout/Reader/XLSX/Helper/CellHelper.php b/src/Spout/Reader/XLSX/Helper/CellHelper.php index c2f21f7..6077839 100644 --- a/src/Spout/Reader/XLSX/Helper/CellHelper.php +++ b/src/Spout/Reader/XLSX/Helper/CellHelper.php @@ -31,6 +31,9 @@ class CellHelper */ public static function fillMissingArrayIndexes($dataArray, $fillValue = '') { + if (empty($dataArray)) { + return []; + } $existingIndexes = array_keys($dataArray); $newIndexes = array_fill_keys(range(0, max($existingIndexes)), $fillValue); diff --git a/tests/Spout/Reader/XLSX/Helper/CellHelperTest.php b/tests/Spout/Reader/XLSX/Helper/CellHelperTest.php index ff417b9..a410d9d 100644 --- a/tests/Spout/Reader/XLSX/Helper/CellHelperTest.php +++ b/tests/Spout/Reader/XLSX/Helper/CellHelperTest.php @@ -10,14 +10,25 @@ namespace Box\Spout\Reader\XLSX\Helper; class CellHelperTest extends \PHPUnit_Framework_TestCase { /** - * @return void + * @return array */ - public function testFillMissingArrayIndexes() + public function dataProviderForTestFillMissingArrayIndexes() { - $arrayToFill = [1 => 1, 3 => 3]; - $filledArray = CellHelper::fillMissingArrayIndexes($arrayToFill, 'FILL'); + return [ + [ 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); }