Skip to content

Commit 6620337

Browse files
committed
Include Some Readers
1 parent ebb04a8 commit 6620337

35 files changed

+182
-34
lines changed

src/PhpSpreadsheet/Reader/BaseReader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ public function setIncludeCharts(bool $includeCharts): self
112112
return $this;
113113
}
114114

115+
/** @return null|string[] */
115116
public function getLoadSheetsOnly(): ?array
116117
{
117118
return $this->loadSheetsOnly;
118119
}
119120

121+
/** @param null|string|string[] $sheetList */
120122
public function setLoadSheetsOnly(string|array|null $sheetList): self
121123
{
122124
if ($sheetList === null) {

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public function listWorksheetInfo(string $filename): array
228228
$this->checkSeparator();
229229
$this->inferSeparator();
230230

231+
/** @var array<int, array{worksheetName: string, lastColumnLetter: string, lastColumnIndex: int, totalRows: int, totalColumns: int, sheetState: string}> */
231232
$worksheetInfo = [];
232233
$worksheetInfo[0]['worksheetName'] = 'Worksheet';
233234
$worksheetInfo[0]['lastColumnLetter'] = 'A';

src/PhpSpreadsheet/Reader/Csv/Delimiter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Delimiter
1313

1414
protected string $enclosure;
1515

16+
/** @var array<string, int[]> */
1617
protected array $counts = [];
1718

1819
protected int $numberLines = 0;
@@ -53,6 +54,7 @@ protected function countPotentialDelimiters(): void
5354
}
5455
}
5556

57+
/** @param array<string, int> $delimiterKeys */
5658
protected function countDelimiterValues(string $line, array $delimiterKeys): void
5759
{
5860
$splitString = mb_str_split($line, 1, 'UTF-8');
@@ -69,7 +71,7 @@ public function infer(): ?string
6971
// Calculate the mean square deviations for each delimiter
7072
// (ignoring delimiters that haven't been found consistently)
7173
$meanSquareDeviations = [];
72-
$middleIdx = floor(($this->numberLines - 1) / 2);
74+
$middleIdx = (int) floor(($this->numberLines - 1) / 2);
7375

7476
foreach (self::POTENTIAL_DELIMETERS as $delimiter) {
7577
$series = $this->counts[$delimiter];

src/PhpSpreadsheet/Reader/Gnumeric.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class Gnumeric extends BaseReader
3838

3939
/**
4040
* Shared Expressions.
41+
*
42+
* @var array<array{column: int, row: int, formula:string}>
4143
*/
4244
private array $expressions = [];
4345

@@ -48,6 +50,7 @@ class Gnumeric extends BaseReader
4850

4951
private ReferenceHelper $referenceHelper;
5052

53+
/** @var array{'dataType': string[]} */
5154
public static array $mappings = [
5255
'dataType' => [
5356
'10' => DataType::TYPE_NULL,
@@ -96,6 +99,8 @@ private static function matchXml(XMLReader $xml, string $expectedLocalName): boo
9699

97100
/**
98101
* Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object.
102+
*
103+
* @return string[]
99104
*/
100105
public function listWorksheetNames(string $filename): array
101106
{
@@ -203,6 +208,7 @@ private function gzfileGetContents(string $filename): string
203208
return $data;
204209
}
205210

211+
/** @return mixed[] */
206212
public static function gnumericMappings(): array
207213
{
208214
return array_merge(self::$mappings, Styles::$mappings);
@@ -559,8 +565,8 @@ private function loadCell(
559565
if (((string) $cell) > '') {
560566
// Formula
561567
$this->expressions[$ExprID] = [
562-
'column' => $cellAttributes->Col,
563-
'row' => $cellAttributes->Row,
568+
'column' => (int) $cellAttributes->Col,
569+
'row' => (int) $cellAttributes->Row,
564570
'formula' => (string) $cell,
565571
];
566572
} else {

src/PhpSpreadsheet/Reader/Gnumeric/PageSetup.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public function sheetMargins(SimpleXMLElement $sheet): self
7070
return $this;
7171
}
7272

73+
/**
74+
* @param float[] $marginSet
75+
*
76+
* @return float[]
77+
*/
7378
private function buildMarginSet(SimpleXMLElement $sheet, array $marginSet): array
7479
{
7580
foreach ($sheet->PrintInformation->Margins->children(Gnumeric::NAMESPACE_GNM) as $key => $margin) {
@@ -83,6 +88,7 @@ private function buildMarginSet(SimpleXMLElement $sheet, array $marginSet): arra
8388
return $marginSet;
8489
}
8590

91+
/** @param float[] $marginSet */
8692
private function adjustMargins(array $marginSet): void
8793
{
8894
foreach ($marginSet as $key => $marginSize) {

src/PhpSpreadsheet/Reader/Gnumeric/Styles.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Styles
1818

1919
protected bool $readDataOnly;
2020

21+
/** @var array<string, string[]> */
2122
public static array $mappings = [
2223
'borderStyle' => [
2324
'0' => Border::BORDER_NONE,
@@ -100,6 +101,7 @@ private function readStyles(SimpleXMLElement $styleRegion, int $maxRow, int $max
100101

101102
$styleAttributes = $style->Style->attributes();
102103

104+
/** @var mixed[][] */
103105
$styleArray = [];
104106
// We still set the number format mask for date/time values, even if readDataOnly is true
105107
// so that we can identify whether a float is a float or a date value
@@ -117,6 +119,7 @@ private function readStyles(SimpleXMLElement $styleRegion, int $maxRow, int $max
117119
}
118120
}
119121

122+
/** @param mixed[][] $styleArray */
120123
private function addBorderDiagonal(SimpleXMLElement $srssb, array &$styleArray): void
121124
{
122125
if (isset($srssb->Diagonal, $srssb->{'Rev-Diagonal'})) {
@@ -131,11 +134,14 @@ private function addBorderDiagonal(SimpleXMLElement $srssb, array &$styleArray):
131134
}
132135
}
133136

137+
/** @param mixed[][] $styleArray */
134138
private function addBorderStyle(SimpleXMLElement $srssb, array &$styleArray, string $direction): void
135139
{
136140
$ucDirection = ucfirst($direction);
137141
if (isset($srssb->$ucDirection)) {
138-
$styleArray['borders'][$direction] = self::parseBorderAttributes($srssb->$ucDirection->attributes());
142+
/** @var SimpleXMLElement */
143+
$temp = $srssb->$ucDirection;
144+
$styleArray['borders'][$direction] = self::parseBorderAttributes($temp->attributes());
139145
}
140146
}
141147

@@ -150,22 +156,26 @@ private function calcRotation(SimpleXMLElement $styleAttributes): int
150156
return $rotation;
151157
}
152158

159+
/** @param mixed[][] $styleArray */
153160
private static function addStyle(array &$styleArray, string $key, string $value): void
154161
{
155162
if (array_key_exists($value, self::$mappings[$key])) {
156-
$styleArray[$key] = self::$mappings[$key][$value];
163+
$styleArray[$key] = self::$mappings[$key][$value]; //* @phpstan-ignore-line
157164
}
158165
}
159166

167+
/** @param mixed[][] $styleArray */
160168
private static function addStyle2(array &$styleArray, string $key1, string $key, string $value): void
161169
{
162170
if (array_key_exists($value, self::$mappings[$key])) {
163171
$styleArray[$key1][$key] = self::$mappings[$key][$value];
164172
}
165173
}
166174

175+
/** @return mixed[][] */
167176
private static function parseBorderAttributes(?SimpleXMLElement $borderAttributes): array
168177
{
178+
/** @var mixed[][] */
169179
$styleArray = [];
170180
if ($borderAttributes !== null) {
171181
if (isset($borderAttributes['Color'])) {
@@ -174,6 +184,7 @@ private static function parseBorderAttributes(?SimpleXMLElement $borderAttribute
174184

175185
self::addStyle($styleArray, 'borderStyle', (string) $borderAttributes['Style']);
176186
}
187+
/** @var mixed[][] $styleArray */
177188

178189
return $styleArray;
179190
}
@@ -188,9 +199,11 @@ private static function parseGnumericColour(string $gnmColour): string
188199
return $gnmR . $gnmG . $gnmB;
189200
}
190201

202+
/** @param mixed[][] $styleArray */
191203
private function addColors(array &$styleArray, SimpleXMLElement $styleAttributes): void
192204
{
193205
$RGB = self::parseGnumericColour((string) $styleAttributes['Fore']);
206+
/** @var mixed[][][] $styleArray */
194207
$styleArray['font']['color']['rgb'] = $RGB;
195208
$RGB = self::parseGnumericColour((string) $styleAttributes['Back']);
196209
$shade = (string) $styleAttributes['Shade'];
@@ -221,6 +234,11 @@ private function readStyleRange(SimpleXMLElement $styleAttributes, int $maxCol,
221234
return $cellRange;
222235
}
223236

237+
/**
238+
* @param mixed[][] $styleArray
239+
*
240+
* @return mixed[]
241+
*/
224242
private function readStyle(array $styleArray, SimpleXMLElement $styleAttributes, SimpleXMLElement $style): array
225243
{
226244
self::addStyle2($styleArray, 'alignment', 'horizontal', (string) $styleAttributes['HAlign']);

src/PhpSpreadsheet/Reader/IReader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ public function setIncludeCharts(bool $includeCharts): self;
9696
* Get which sheets to load
9797
* Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
9898
* indicating that all worksheets in the workbook should be loaded.
99+
*
100+
* @return null|string[]
99101
*/
100102
public function getLoadSheetsOnly(): ?array;
101103

102104
/**
103105
* Set which sheets to load.
104106
*
105-
* @param null|array|string $value This should be either an array of worksheet names to be loaded,
107+
* @param null|string|string[] $value This should be either an array of worksheet names to be loaded,
106108
* or a string containing a single worksheet name. If NULL, then it tells the Reader to
107109
* read all worksheets in the workbook
108110
*

src/PhpSpreadsheet/Reader/Ods.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp
259259
throw new Exception('Unable to read data from {$pFilename}');
260260
}
261261

262+
/** @var array{meta?: string, office?: string, dc?: string} */
262263
$namespacesMeta = $xml->getNamespaces(true);
263264

264265
(new DocumentProperties($spreadsheet))->load($xml, $namespacesMeta);
@@ -383,6 +384,7 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp
383384
$columnWidth = new HelperDimension($columnWidths[$tableStyleName]);
384385
$tableColumnString = Coordinate::stringFromColumnIndex($tableColumnIndex);
385386
for ($rowRepeats2 = $rowRepeats; $rowRepeats2 > 0; --$rowRepeats2) {
387+
/** @var string $tableColumnString */
386388
$spreadsheet->getActiveSheet()
387389
->getColumnDimension($tableColumnString)
388390
->setWidth($columnWidth->toUnit('cm'), 'cm');

src/PhpSpreadsheet/Reader/Ods/PageSettings.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DOMDocument;
66
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
77
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8+
use stdClass;
89

910
class PageSettings
1011
{
@@ -21,6 +22,7 @@ class PageSettings
2122
*/
2223
private array $tableStylesCrossReference = [];
2324

25+
/** @var mixed[] */
2426
private array $pageLayoutStyles = [];
2527

2628
/**
@@ -151,12 +153,15 @@ public function setPrintSettingsForWorksheet(Worksheet $worksheet, string $style
151153
if (!array_key_exists($printSettingsIndex, $this->pageLayoutStyles)) {
152154
return;
153155
}
156+
/** @var (object{orientation: string, scale: int|string, printOrder: string|null,
157+
* horizontalCentered: bool, verticalCentered: bool, marginLeft: float, marginRight: float, marginTop: float,
158+
* marginBottom: float, marginHeader: float, marginFooter: float}&stdClass) */
154159
$printSettings = $this->pageLayoutStyles[$printSettingsIndex];
155160

156161
$worksheet->getPageSetup()
157162
->setOrientation($printSettings->orientation ?? PageSetup::ORIENTATION_DEFAULT)
158163
->setPageOrder($printSettings->printOrder === 'ltr' ? PageSetup::PAGEORDER_OVER_THEN_DOWN : PageSetup::PAGEORDER_DOWN_THEN_OVER)
159-
->setScale((int) trim($printSettings->scale, '%'))
164+
->setScale((int) trim((string) $printSettings->scale, '%'))
160165
->setHorizontalCentered($printSettings->horizontalCentered)
161166
->setVerticalCentered($printSettings->verticalCentered);
162167

src/PhpSpreadsheet/Reader/Ods/Properties.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ public function __construct(Spreadsheet $spreadsheet)
1515
$this->spreadsheet = $spreadsheet;
1616
}
1717

18+
/** @param array{meta?: string, office?: string, dc?: string} $namespacesMeta */
1819
public function load(SimpleXMLElement $xml, array $namespacesMeta): void
1920
{
2021
$docProps = $this->spreadsheet->getProperties();
21-
$officeProperty = $xml->children($namespacesMeta['office']);
22+
$officeProperty = $xml->children($namespacesMeta['office'] ?? '');
2223
foreach ($officeProperty as $officePropertyData) {
2324
if (isset($namespacesMeta['dc'])) {
2425
$officePropertiesDC = $officePropertyData->children($namespacesMeta['dc']);
@@ -27,7 +28,7 @@ public function load(SimpleXMLElement $xml, array $namespacesMeta): void
2728

2829
$officePropertyMeta = null;
2930
if (isset($namespacesMeta['dc'])) {
30-
$officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
31+
$officePropertyMeta = $officePropertyData->children($namespacesMeta['meta'] ?? '');
3132
}
3233
$officePropertyMeta = $officePropertyMeta ?? [];
3334
foreach ($officePropertyMeta as $propertyName => $propertyValue) {
@@ -66,13 +67,14 @@ private function setCoreProperties(DocumentProperties $docProps, SimpleXMLElemen
6667
}
6768
}
6869

70+
/** @param array{meta?: string, office?: mixed, dc?: mixed} $namespacesMeta */
6971
private function setMetaProperties(
7072
array $namespacesMeta,
7173
SimpleXMLElement $propertyValue,
7274
string $propertyName,
7375
DocumentProperties $docProps
7476
): void {
75-
$propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
77+
$propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta'] ?? '');
7678
$propertyValue = (string) $propertyValue;
7779
switch ($propertyName) {
7880
case 'initial-creator':
@@ -101,14 +103,17 @@ private function setMetaProperties(
101103
}
102104
}
103105

106+
/** @param iterable<string> $propertyValueAttributes */
104107
private function setUserDefinedProperty(iterable $propertyValueAttributes, string $propertyValue, DocumentProperties $docProps): void
105108
{
106109
$propertyValueName = '';
107110
$propertyValueType = DocumentProperties::PROPERTY_TYPE_STRING;
108111
foreach ($propertyValueAttributes as $key => $value) {
109112
if ($key == 'name') {
113+
/** @var scalar $value */
110114
$propertyValueName = (string) $value;
111115
} elseif ($key == 'value-type') {
116+
/** @var string $value */
112117
switch ($value) {
113118
case 'date':
114119
$propertyValue = DocumentProperties::convertProperty($propertyValue, 'date');

0 commit comments

Comments
 (0)