Leverage type hinting to assert array parameters

This commit is contained in:
Adrien Crivelli 2017-04-16 13:15:04 +09:00
parent 033a4bdad5
commit 06ca8f9b04
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
10 changed files with 407 additions and 475 deletions

View File

@ -709,10 +709,10 @@ class Cell
* *
* @return string String representation of $pRange * @return string String representation of $pRange
*/ */
public static function buildRange($pRange) public static function buildRange(array $pRange)
{ {
// Verify range // Verify range
if (!is_array($pRange) || empty($pRange) || !is_array($pRange[0])) { if (empty($pRange) || !is_array($pRange[0])) {
throw new Exception('Range does not contain any information'); throw new Exception('Range does not contain any information');
} }

View File

@ -84,13 +84,9 @@ class IOFactory
* *
* @throws Reader\Exception * @throws Reader\Exception
*/ */
public static function setSearchLocations($value) public static function setSearchLocations(array $value)
{ {
if (is_array($value)) {
self::$searchLocations = $value; self::$searchLocations = $value;
} else {
throw new Reader\Exception('Invalid parameter passed.');
}
} }
/** /**

View File

@ -462,12 +462,8 @@ class HeaderFooter
* *
* @return HeaderFooter * @return HeaderFooter
*/ */
public function setImages($images) public function setImages(array $images)
{ {
if (!is_array($images)) {
throw new \PhpOffice\PhpSpreadsheet\Exception('Invalid parameter!');
}
$this->headerFooterImages = $images; $this->headerFooterImages = $images;
return $this; return $this;

View File

@ -1172,15 +1172,14 @@ class Html extends BaseWriter implements IWriter
* @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet \PhpOffice\PhpSpreadsheet\Worksheet * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet \PhpOffice\PhpSpreadsheet\Worksheet
* @param array $pValues Array containing cells in a row * @param array $pValues Array containing cells in a row
* @param int $pRow Row number (0-based) * @param int $pRow Row number (0-based)
* @param mixed $cellType * @param mixed $cellType eg: 'td'
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* *
* @return string * @return string
*/ */
private function generateRow(\PhpOffice\PhpSpreadsheet\Worksheet $pSheet, $pValues = null, $pRow = 0, $cellType = 'td') private function generateRow(\PhpOffice\PhpSpreadsheet\Worksheet $pSheet, array $pValues, $pRow, $cellType)
{ {
if (is_array($pValues)) {
// Construct HTML // Construct HTML
$html = ''; $html = '';
@ -1418,8 +1417,6 @@ class Html extends BaseWriter implements IWriter
// Return // Return
return $html; return $html;
} }
throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Invalid parameters passed.');
}
/** /**
* Takes array where of CSS properties / values and converts to CSS string. * Takes array where of CSS properties / values and converts to CSS string.

View File

@ -210,7 +210,7 @@ class ContentTypes extends WriterPart
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */
private function writeDefaultContentType(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, $pPartname = '', $pContentType = '') private function writeDefaultContentType(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, $pPartname, $pContentType)
{ {
if ($pPartname != '' && $pContentType != '') { if ($pPartname != '' && $pContentType != '') {
// Write content type // Write content type
@ -232,7 +232,7 @@ class ContentTypes extends WriterPart
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */
private function writeOverrideContentType(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, $pPartname = '', $pContentType = '') private function writeOverrideContentType(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, $pPartname, $pContentType)
{ {
if ($pPartname != '' && $pContentType != '') { if ($pPartname != '' && $pContentType != '') {
// Write content type // Write content type

View File

@ -163,7 +163,7 @@ class Drawing extends WriterPart
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */
public function writeDrawing(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing $pDrawing = null, $pRelationId = -1) public function writeDrawing(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing $pDrawing, $pRelationId = -1)
{ {
if ($pRelationId >= 0) { if ($pRelationId >= 0) {
// xdr:oneCellAnchor // xdr:oneCellAnchor
@ -271,54 +271,6 @@ class Drawing extends WriterPart
$objWriter->endElement(); $objWriter->endElement();
} }
/*
// a:scene3d
$objWriter->startElement('a:scene3d');
// a:camera
$objWriter->startElement('a:camera');
$objWriter->writeAttribute('prst', 'orthographicFront');
$objWriter->endElement();
// a:lightRig
$objWriter->startElement('a:lightRig');
$objWriter->writeAttribute('rig', 'twoPt');
$objWriter->writeAttribute('dir', 't');
// a:rot
$objWriter->startElement('a:rot');
$objWriter->writeAttribute('lat', '0');
$objWriter->writeAttribute('lon', '0');
$objWriter->writeAttribute('rev', '0');
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
*/
/*
// a:sp3d
$objWriter->startElement('a:sp3d');
// a:bevelT
$objWriter->startElement('a:bevelT');
$objWriter->writeAttribute('w', '25400');
$objWriter->writeAttribute('h', '19050');
$objWriter->endElement();
// a:contourClr
$objWriter->startElement('a:contourClr');
// a:srgbClr
$objWriter->startElement('a:srgbClr');
$objWriter->writeAttribute('val', 'FFFFFF');
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
*/
$objWriter->endElement(); $objWriter->endElement();
$objWriter->endElement(); $objWriter->endElement();

View File

@ -397,7 +397,7 @@ class Rels extends WriterPart
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */
private function writeRelationship(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '') private function writeRelationship(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, $pId, $pType, $pTarget, $pTargetMode = '')
{ {
if ($pType != '' && $pTarget != '') { if ($pType != '' && $pTarget != '') {
// Write relationship // Write relationship

View File

@ -241,7 +241,7 @@ class Workbook extends WriterPart
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */
private function writeSheet(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, $pSheetname = '', $pSheetId = 1, $pRelId = 1, $sheetState = 'visible') private function writeSheet(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, $pSheetname, $pSheetId = 1, $pRelId = 1, $sheetState = 'visible')
{ {
if ($pSheetname != '') { if ($pSheetname != '') {
// Write sheet // Write sheet

View File

@ -978,9 +978,8 @@ class Worksheet extends WriterPart
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */
private function writeSheetData(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null, $pStringTable = null) private function writeSheetData(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet, array $pStringTable)
{ {
if (is_array($pStringTable)) {
// Flipped stringtable, for faster index searching // Flipped stringtable, for faster index searching
$aFlippedStringTable = $this->getParentWriter()->getWriterPart('stringtable')->flipStringTable($pStringTable); $aFlippedStringTable = $this->getParentWriter()->getWriterPart('stringtable')->flipStringTable($pStringTable);
@ -1045,7 +1044,7 @@ class Worksheet extends WriterPart
if (isset($cellsByRow[$currentRow])) { if (isset($cellsByRow[$currentRow])) {
foreach ($cellsByRow[$currentRow] as $cellAddress) { foreach ($cellsByRow[$currentRow] as $cellAddress) {
// Write cell // Write cell
$this->writeCell($objWriter, $pSheet, $cellAddress, $pStringTable, $aFlippedStringTable); $this->writeCell($objWriter, $pSheet, $cellAddress, $aFlippedStringTable);
} }
} }
@ -1055,9 +1054,6 @@ class Worksheet extends WriterPart
} }
$objWriter->endElement(); $objWriter->endElement();
} else {
throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Invalid parameters passed.');
}
} }
/** /**
@ -1071,9 +1067,8 @@ class Worksheet extends WriterPart
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */
private function writeCell(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null, $pCellAddress = null, $pStringTable = null, $pFlippedStringTable = null) private function writeCell(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet, $pCellAddress, array $pFlippedStringTable)
{ {
if (is_array($pStringTable) && is_array($pFlippedStringTable)) {
// Cell // Cell
$pCell = $pSheet->getCell($pCellAddress); $pCell = $pSheet->getCell($pCellAddress);
$objWriter->startElement('c'); $objWriter->startElement('c');
@ -1175,9 +1170,6 @@ class Worksheet extends WriterPart
} }
$objWriter->endElement(); $objWriter->endElement();
} else {
throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Invalid parameters passed.');
}
} }
/** /**

View File

@ -223,18 +223,17 @@ class CellTest extends \PHPUnit_Framework_TestCase
return require 'data/CellBuildRange.php'; return require 'data/CellBuildRange.php';
} }
/**
* @expectedException \TypeError
*/
public function testBuildRangeInvalid() public function testBuildRangeInvalid()
{ {
$cellRange = ''; if (PHP_MAJOR_VERSION < 7) {
try { $this->markTestSkipped('Cannot catch type hinting error with PHP 5.6');
Cell::buildRange($cellRange);
} catch (\Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals($e->getMessage(), 'Range does not contain any information');
return;
} }
$this->fail('An expected exception has not been raised.');
$cellRange = '';
Cell::buildRange($cellRange);
} }
/** /**