Skip to content

Commit e35bbb9

Browse files
committed
Stricter type-hinting for IReader
1 parent 40b6fc7 commit e35bbb9

File tree

9 files changed

+38
-75
lines changed

9 files changed

+38
-75
lines changed

src/PhpSpreadsheet/Reader/BaseReader.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ abstract class BaseReader implements IReader
4141
* Restrict which sheets should be loaded?
4242
* This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
4343
*
44-
* @var null|string[]
44+
* @var ?string[]
4545
*/
46-
protected $loadSheetsOnly;
46+
protected $loadSheetsOnly = [];
4747

4848
/**
4949
* IReadFilter instance.
@@ -64,38 +64,38 @@ public function __construct()
6464
$this->readFilter = new DefaultReadFilter();
6565
}
6666

67-
public function getReadDataOnly()
67+
public function getReadDataOnly(): bool
6868
{
6969
return $this->readDataOnly;
7070
}
7171

72-
public function setReadDataOnly($readCellValuesOnly)
72+
public function setReadDataOnly(bool $readDataOnly): IReader
7373
{
74-
$this->readDataOnly = (bool) $readCellValuesOnly;
74+
$this->readDataOnly = (bool) $readDataOnly;
7575

7676
return $this;
7777
}
7878

79-
public function getReadEmptyCells()
79+
public function getReadEmptyCells(): bool
8080
{
8181
return $this->readEmptyCells;
8282
}
8383

84-
public function setReadEmptyCells($readEmptyCells)
84+
public function setReadEmptyCells(bool $readEmptyCells): IReader
8585
{
86-
$this->readEmptyCells = (bool) $readEmptyCells;
86+
$this->readEmptyCells = $readEmptyCells;
8787

8888
return $this;
8989
}
9090

91-
public function getIncludeCharts()
91+
public function getIncludeCharts(): bool
9292
{
9393
return $this->includeCharts;
9494
}
9595

96-
public function setIncludeCharts($includeCharts)
96+
public function setIncludeCharts(bool $includeCharts): IReader
9797
{
98-
$this->includeCharts = (bool) $includeCharts;
98+
$this->includeCharts = $includeCharts;
9999

100100
return $this;
101101
}
@@ -105,7 +105,7 @@ public function getLoadSheetsOnly()
105105
return $this->loadSheetsOnly;
106106
}
107107

108-
public function setLoadSheetsOnly($sheetList)
108+
public function setLoadSheetsOnly($sheetList): IReader
109109
{
110110
if ($sheetList === null) {
111111
return $this->setLoadAllSheets();
@@ -116,19 +116,19 @@ public function setLoadSheetsOnly($sheetList)
116116
return $this;
117117
}
118118

119-
public function setLoadAllSheets()
119+
public function setLoadAllSheets(): IReader
120120
{
121121
$this->loadSheetsOnly = null;
122122

123123
return $this;
124124
}
125125

126-
public function getReadFilter()
126+
public function getReadFilter(): IReadFilter
127127
{
128128
return $this->readFilter;
129129
}
130130

131-
public function setReadFilter(IReadFilter $readFilter)
131+
public function setReadFilter(IReadFilter $readFilter): IReader
132132
{
133133
$this->readFilter = $readFilter;
134134

src/PhpSpreadsheet/Reader/DefaultReadFilter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ class DefaultReadFilter implements IReadFilter
1010
* @param string $columnAddress Column address (as a string value like "A", or "IV")
1111
* @param int $row Row number
1212
* @param string $worksheetName Optional worksheet name
13-
*
14-
* @return bool
1513
*/
16-
public function readCell($columnAddress, $row, $worksheetName = '')
14+
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
1715
{
1816
return true;
1917
}

src/PhpSpreadsheet/Reader/IReadFilter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ interface IReadFilter
1010
* @param string $columnAddress Column address (as a string value like "A", or "IV")
1111
* @param int $row Row number
1212
* @param string $worksheetName Optional worksheet name
13-
*
14-
* @return bool
1513
*/
16-
public function readCell($columnAddress, $row, $worksheetName = '');
14+
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool;
1715
}

src/PhpSpreadsheet/Reader/IReader.php

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Reader;
44

5+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
57
interface IReader
68
{
79
public const LOAD_WITH_CHARTS = 1;
810

9-
/**
10-
* IReader constructor.
11-
*/
12-
public function __construct();
13-
1411
/**
1512
* Can the current IReader read the file?
1613
*/
@@ -20,110 +17,82 @@ public function canRead(string $filename): bool;
2017
* Read data only?
2118
* If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
2219
* If false (the default) it will read data and formatting.
23-
*
24-
* @return bool
2520
*/
26-
public function getReadDataOnly();
21+
public function getReadDataOnly(): bool;
2722

2823
/**
2924
* Set read data only
3025
* Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
3126
* Set to false (the default) to advise the Reader to read both data and formatting for cells.
32-
*
33-
* @param bool $readDataOnly
34-
*
35-
* @return IReader
3627
*/
37-
public function setReadDataOnly($readDataOnly);
28+
public function setReadDataOnly(bool $readDataOnly): self;
3829

3930
/**
4031
* Read empty cells?
4132
* If this is true (the default), then the Reader will read data values for all cells, irrespective of value.
4233
* If false it will not read data for cells containing a null value or an empty string.
43-
*
44-
* @return bool
4534
*/
46-
public function getReadEmptyCells();
35+
public function getReadEmptyCells(): bool;
4736

4837
/**
4938
* Set read empty cells
5039
* Set to true (the default) to advise the Reader read data values for all cells, irrespective of value.
5140
* Set to false to advise the Reader to ignore cells containing a null value or an empty string.
52-
*
53-
* @param bool $readEmptyCells
54-
*
55-
* @return IReader
5641
*/
57-
public function setReadEmptyCells($readEmptyCells);
42+
public function setReadEmptyCells(bool $readEmptyCells): self;
5843

5944
/**
6045
* Read charts in workbook?
6146
* If this is true, then the Reader will include any charts that exist in the workbook.
6247
* Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
6348
* If false (the default) it will ignore any charts defined in the workbook file.
64-
*
65-
* @return bool
6649
*/
67-
public function getIncludeCharts();
50+
public function getIncludeCharts(): bool;
6851

6952
/**
7053
* Set read charts in workbook
7154
* Set to true, to advise the Reader to include any charts that exist in the workbook.
7255
* Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
7356
* Set to false (the default) to discard charts.
74-
*
75-
* @param bool $includeCharts
76-
*
77-
* @return IReader
7857
*/
79-
public function setIncludeCharts($includeCharts);
58+
public function setIncludeCharts(bool $includeCharts): self;
8059

8160
/**
8261
* Get which sheets to load
8362
* Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
8463
* indicating that all worksheets in the workbook should be loaded.
8564
*
86-
* @return mixed
65+
* @return ?string[]
8766
*/
8867
public function getLoadSheetsOnly();
8968

9069
/**
9170
* Set which sheets to load.
9271
*
93-
* @param mixed $value
72+
* @param null|string|string[] $sheetList
9473
* This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
9574
* If NULL, then it tells the Reader to read all worksheets in the workbook
96-
*
97-
* @return IReader
9875
*/
99-
public function setLoadSheetsOnly($value);
76+
public function setLoadSheetsOnly($sheetList): self;
10077

10178
/**
10279
* Set all sheets to load
10380
* Tells the Reader to load all worksheets from the workbook.
104-
*
105-
* @return IReader
10681
*/
107-
public function setLoadAllSheets();
82+
public function setLoadAllSheets(): self;
10883

10984
/**
11085
* Read filter.
111-
*
112-
* @return IReadFilter
11386
*/
114-
public function getReadFilter();
87+
public function getReadFilter(): IReadFilter;
11588

11689
/**
11790
* Set read filter.
118-
*
119-
* @return IReader
12091
*/
121-
public function setReadFilter(IReadFilter $readFilter);
92+
public function setReadFilter(IReadFilter $readFilter): self;
12293

12394
/**
12495
* Loads PhpSpreadsheet from file.
125-
*
126-
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet
12796
*/
128-
public function load(string $filename, int $flags = 0);
97+
public function load(string $filename, int $flags = 0): Spreadsheet;
12998
}

tests/PhpSpreadsheetTests/Functional/ReadFilterFilter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ class ReadFilterFilter implements IReadFilter
1111
* @param int $row Row number
1212
* @param string $worksheetName Optional worksheet name
1313
*
14-
* @return bool
15-
*
1614
* @see \PhpOffice\PhpSpreadsheet\Reader\IReadFilter::readCell()
1715
*/
18-
public function readCell($column, $row, $worksheetName = '')
16+
public function readCell($column, $row, $worksheetName = ''): bool
1917
{
2018
// define filter range
2119
$rowMin = 2;

tests/PhpSpreadsheetTests/Reader/Csv/CsvContiguousFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public function filter0(int $row): bool
5252
return false;
5353
}
5454

55-
public function readCell($columnAddress, $row, $worksheetName = '')
55+
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
5656
{
57-
if ($this->filterType == 1) {
57+
if ($this->filterType === 1) {
5858
return $this->filter1($row);
5959
}
6060

tests/PhpSpreadsheetTests/Reader/Gnumeric/GnumericFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/** Define a Read Filter class implementing IReadFilter */
88
class GnumericFilter implements IReadFilter
99
{
10-
public function readCell($columnAddress, $row, $worksheetName = '')
10+
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
1111
{
1212
return $row !== 4;
1313
}

tests/PhpSpreadsheetTests/Reader/Xlsx/OddColumnReadFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class OddColumnReadFilter implements IReadFilter
1111
{
12-
public function readCell($columnAddress, $row, $worksheetName = '')
12+
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
1313
{
1414
return (\ord(\substr($columnAddress, -1, 1)) % 2) === 1;
1515
}

tests/PhpSpreadsheetTests/Reader/Xml/XmlFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/** Define a Read Filter class implementing IReadFilter */
88
class XmlFilter implements IReadFilter
99
{
10-
public function readCell($columnAddress, $row, $worksheetName = '')
10+
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
1111
{
1212
return $row !== 4;
1313
}

0 commit comments

Comments
 (0)