Stricter type-hinting for IReader

This commit is contained in:
MarkBaker
2022-09-30 15:34:23 +02:00
parent 40b6fc7c69
commit 43413002fd
15 changed files with 43 additions and 80 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ $helper->logWrite($writer, $filename, $callStartTime);
class MyReadFilter implements IReadFilter
{
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
// Read title row and rows 20 - 30
if ($row == 1 || ($row >= 20 && $row <= 30)) {
@@ -13,7 +13,7 @@ $sheetname = 'Data Sheet #3';
class MyReadFilter implements IReadFilter
{
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
// Read rows 1 to 7 and columns A to E only
if ($row >= 1 && $row <= 7) {
@@ -26,7 +26,7 @@ class MyReadFilter implements IReadFilter
$this->columns = $columns;
}
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
if ($row >= $this->startRow && $row <= $this->endRow) {
if (in_array($columnAddress, $this->columns)) {
@@ -29,7 +29,7 @@ class ChunkReadFilter implements IReadFilter
$this->endRow = $startRow + $chunkSize;
}
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
// Only read the heading row, and the rows that were configured in the constructor
if (($row == 1) || ($row >= $this->startRow && $row < $this->endRow)) {
@@ -29,7 +29,7 @@ class ChunkReadFilter implements IReadFilter
$this->endRow = $startRow + $chunkSize;
}
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
// Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
if (($row == 1) || ($row >= $this->startRow && $row < $this->endRow)) {
@@ -29,7 +29,7 @@ class ChunkReadFilter implements IReadFilter
$this->endRow = $startRow + $chunkSize;
}
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
// Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
if (($row == 1) || ($row >= $this->startRow && $row < $this->endRow)) {
+14 -14
View File
@@ -41,7 +41,7 @@ abstract class BaseReader implements IReader
* Restrict which sheets should be loaded?
* This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
*
* @var null|string[]
* @var ?string[]
*/
protected $loadSheetsOnly;
@@ -64,38 +64,38 @@ abstract class BaseReader implements IReader
$this->readFilter = new DefaultReadFilter();
}
public function getReadDataOnly()
public function getReadDataOnly(): bool
{
return $this->readDataOnly;
}
public function setReadDataOnly($readCellValuesOnly)
public function setReadDataOnly(bool $readDataOnly): IReader
{
$this->readDataOnly = (bool) $readCellValuesOnly;
$this->readDataOnly = (bool) $readDataOnly;
return $this;
}
public function getReadEmptyCells()
public function getReadEmptyCells(): bool
{
return $this->readEmptyCells;
}
public function setReadEmptyCells($readEmptyCells)
public function setReadEmptyCells(bool $readEmptyCells): IReader
{
$this->readEmptyCells = (bool) $readEmptyCells;
$this->readEmptyCells = $readEmptyCells;
return $this;
}
public function getIncludeCharts()
public function getIncludeCharts(): bool
{
return $this->includeCharts;
}
public function setIncludeCharts($includeCharts)
public function setIncludeCharts(bool $includeCharts): IReader
{
$this->includeCharts = (bool) $includeCharts;
$this->includeCharts = $includeCharts;
return $this;
}
@@ -105,7 +105,7 @@ abstract class BaseReader implements IReader
return $this->loadSheetsOnly;
}
public function setLoadSheetsOnly($sheetList)
public function setLoadSheetsOnly($sheetList): IReader
{
if ($sheetList === null) {
return $this->setLoadAllSheets();
@@ -116,19 +116,19 @@ abstract class BaseReader implements IReader
return $this;
}
public function setLoadAllSheets()
public function setLoadAllSheets(): IReader
{
$this->loadSheetsOnly = null;
return $this;
}
public function getReadFilter()
public function getReadFilter(): IReadFilter
{
return $this->readFilter;
}
public function setReadFilter(IReadFilter $readFilter)
public function setReadFilter(IReadFilter $readFilter): IReader
{
$this->readFilter = $readFilter;
@@ -10,10 +10,8 @@ class DefaultReadFilter implements IReadFilter
* @param string $columnAddress Column address (as a string value like "A", or "IV")
* @param int $row Row number
* @param string $worksheetName Optional worksheet name
*
* @return bool
*/
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
return true;
}
+1 -3
View File
@@ -10,8 +10,6 @@ interface IReadFilter
* @param string $columnAddress Column address (as a string value like "A", or "IV")
* @param int $row Row number
* @param string $worksheetName Optional worksheet name
*
* @return bool
*/
public function readCell($columnAddress, $row, $worksheetName = '');
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool;
}
+15 -46
View File
@@ -2,15 +2,12 @@
namespace PhpOffice\PhpSpreadsheet\Reader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
interface IReader
{
public const LOAD_WITH_CHARTS = 1;
/**
* IReader constructor.
*/
public function __construct();
/**
* Can the current IReader read the file?
*/
@@ -20,110 +17,82 @@ interface IReader
* Read data only?
* If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
* If false (the default) it will read data and formatting.
*
* @return bool
*/
public function getReadDataOnly();
public function getReadDataOnly(): bool;
/**
* Set read data only
* Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
* Set to false (the default) to advise the Reader to read both data and formatting for cells.
*
* @param bool $readDataOnly
*
* @return IReader
*/
public function setReadDataOnly($readDataOnly);
public function setReadDataOnly(bool $readDataOnly): self;
/**
* Read empty cells?
* If this is true (the default), then the Reader will read data values for all cells, irrespective of value.
* If false it will not read data for cells containing a null value or an empty string.
*
* @return bool
*/
public function getReadEmptyCells();
public function getReadEmptyCells(): bool;
/**
* Set read empty cells
* Set to true (the default) to advise the Reader read data values for all cells, irrespective of value.
* Set to false to advise the Reader to ignore cells containing a null value or an empty string.
*
* @param bool $readEmptyCells
*
* @return IReader
*/
public function setReadEmptyCells($readEmptyCells);
public function setReadEmptyCells(bool $readEmptyCells): self;
/**
* Read charts in workbook?
* If this is true, then the Reader will include any charts that exist in the workbook.
* Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
* If false (the default) it will ignore any charts defined in the workbook file.
*
* @return bool
*/
public function getIncludeCharts();
public function getIncludeCharts(): bool;
/**
* Set read charts in workbook
* Set to true, to advise the Reader to include any charts that exist in the workbook.
* Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
* Set to false (the default) to discard charts.
*
* @param bool $includeCharts
*
* @return IReader
*/
public function setIncludeCharts($includeCharts);
public function setIncludeCharts(bool $includeCharts): self;
/**
* Get which sheets to load
* Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
* indicating that all worksheets in the workbook should be loaded.
*
* @return mixed
* @return ?string[]
*/
public function getLoadSheetsOnly();
/**
* Set which sheets to load.
*
* @param mixed $value
* @param null|string|string[] $sheetList
* This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
* If NULL, then it tells the Reader to read all worksheets in the workbook
*
* @return IReader
*/
public function setLoadSheetsOnly($value);
public function setLoadSheetsOnly($sheetList): self;
/**
* Set all sheets to load
* Tells the Reader to load all worksheets from the workbook.
*
* @return IReader
*/
public function setLoadAllSheets();
public function setLoadAllSheets(): self;
/**
* Read filter.
*
* @return IReadFilter
*/
public function getReadFilter();
public function getReadFilter(): IReadFilter;
/**
* Set read filter.
*
* @return IReader
*/
public function setReadFilter(IReadFilter $readFilter);
public function setReadFilter(IReadFilter $readFilter): self;
/**
* Loads PhpSpreadsheet from file.
*
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet
*/
public function load(string $filename, int $flags = 0);
public function load(string $filename, int $flags = 0): Spreadsheet;
}
@@ -11,11 +11,9 @@ class ReadFilterFilter implements IReadFilter
* @param int $row Row number
* @param string $worksheetName Optional worksheet name
*
* @return bool
*
* @see \PhpOffice\PhpSpreadsheet\Reader\IReadFilter::readCell()
*/
public function readCell($column, $row, $worksheetName = '')
public function readCell($column, $row, $worksheetName = ''): bool
{
// define filter range
$rowMin = 2;
@@ -52,9 +52,9 @@ class CsvContiguousFilter implements IReadFilter
return false;
}
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
if ($this->filterType == 1) {
if ($this->filterType === 1) {
return $this->filter1($row);
}
@@ -7,7 +7,7 @@ use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;
/** Define a Read Filter class implementing IReadFilter */
class GnumericFilter implements IReadFilter
{
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
return $row !== 4;
}
@@ -9,7 +9,7 @@ use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;
*/
class OddColumnReadFilter implements IReadFilter
{
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
return (\ord(\substr($columnAddress, -1, 1)) % 2) === 1;
}
@@ -7,7 +7,7 @@ use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;
/** Define a Read Filter class implementing IReadFilter */
class XmlFilter implements IReadFilter
{
public function readCell($columnAddress, $row, $worksheetName = '')
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
{
return $row !== 4;
}