Skip to content

Commit 41da846

Browse files
authored
Merge pull request #4409 from oleibman/stan2lv903
Phpstan Level 9 - Part 3 of Many
2 parents bb3d5fa + 54210e4 commit 41da846

File tree

19 files changed

+43
-235
lines changed

19 files changed

+43
-235
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,114 +1200,6 @@ parameters:
12001200
count: 1
12011201
path: src/PhpSpreadsheet/Calculation/TextData/Replace.php
12021202

1203-
-
1204-
message: '#^Cannot cast mixed to string\.$#'
1205-
identifier: cast.string
1206-
count: 2
1207-
path: src/PhpSpreadsheet/Calculation/TextData/Text.php
1208-
1209-
-
1210-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\Text\:\:split\(\) should return array\|string but returns mixed\.$#'
1211-
identifier: return.type
1212-
count: 1
1213-
path: src/PhpSpreadsheet/Calculation/TextData/Text.php
1214-
1215-
-
1216-
message: '#^Parameter \#1 \$str of function preg_quote expects string, mixed given\.$#'
1217-
identifier: argument.type
1218-
count: 1
1219-
path: src/PhpSpreadsheet/Calculation/TextData/Text.php
1220-
1221-
-
1222-
message: '#^Parameter \#2 \$subject of static method Composer\\Pcre\\Preg\:\:split\(\) expects string, mixed given\.$#'
1223-
identifier: argument.type
1224-
count: 2
1225-
path: src/PhpSpreadsheet/Calculation/TextData/Text.php
1226-
1227-
-
1228-
message: '#^Part \$formula \(mixed\) of encapsed string cannot be cast to string\.$#'
1229-
identifier: encapsedStringPart.nonString
1230-
count: 1
1231-
path: src/PhpSpreadsheet/Cell/DataValidator.php
1232-
1233-
-
1234-
message: '#^Parameter \#1 \$path of function basename expects string, mixed given\.$#'
1235-
identifier: argument.type
1236-
count: 1
1237-
path: src/PhpSpreadsheet/Helper/Sample.php
1238-
1239-
-
1240-
message: '#^Binary operation "\." between ''RICH TEXT\: '' and mixed results in an error\.$#'
1241-
identifier: binaryOp.invalid
1242-
count: 1
1243-
path: src/PhpSpreadsheet/Reader/Html.php
1244-
1245-
-
1246-
message: '#^Parameter \#4 \$cellContent of method PhpOffice\\PhpSpreadsheet\\Reader\\Html\:\:processDomElementBr\(\) expects string, mixed given\.$#'
1247-
identifier: argument.type
1248-
count: 1
1249-
path: src/PhpSpreadsheet/Reader/Html.php
1250-
1251-
-
1252-
message: '#^Parameter \#5 \$cellContent of method PhpOffice\\PhpSpreadsheet\\Reader\\Html\:\:processDomElement\(\) expects string, mixed given\.$#'
1253-
identifier: argument.type
1254-
count: 3
1255-
path: src/PhpSpreadsheet/Reader/Html.php
1256-
1257-
-
1258-
message: '#^Parameter &\$cellContent by\-ref type of method PhpOffice\\PhpSpreadsheet\\Reader\\Html\:\:processDomElementBr\(\) expects string, mixed given\.$#'
1259-
identifier: parameterByRef.type
1260-
count: 1
1261-
path: src/PhpSpreadsheet/Reader/Html.php
1262-
1263-
-
1264-
message: '#^Parameter &\$cellContent by\-ref type of method PhpOffice\\PhpSpreadsheet\\Reader\\Html\:\:processDomElementH1Etc\(\) expects string, mixed given\.$#'
1265-
identifier: parameterByRef.type
1266-
count: 2
1267-
path: src/PhpSpreadsheet/Reader/Html.php
1268-
1269-
-
1270-
message: '#^Parameter &\$cellContent by\-ref type of method PhpOffice\\PhpSpreadsheet\\Reader\\Html\:\:processDomElementHr\(\) expects string, mixed given\.$#'
1271-
identifier: parameterByRef.type
1272-
count: 1
1273-
path: src/PhpSpreadsheet/Reader/Html.php
1274-
1275-
-
1276-
message: '#^Parameter &\$cellContent by\-ref type of method PhpOffice\\PhpSpreadsheet\\Reader\\Html\:\:processDomElementLi\(\) expects string, mixed given\.$#'
1277-
identifier: parameterByRef.type
1278-
count: 2
1279-
path: src/PhpSpreadsheet/Reader/Html.php
1280-
1281-
-
1282-
message: '#^Parameter &\$cellContent by\-ref type of method PhpOffice\\PhpSpreadsheet\\Reader\\Html\:\:processDomElementTable\(\) expects string, mixed given\.$#'
1283-
identifier: parameterByRef.type
1284-
count: 1
1285-
path: src/PhpSpreadsheet/Reader/Html.php
1286-
1287-
-
1288-
message: '#^Parameter &\$cellContent by\-ref type of method PhpOffice\\PhpSpreadsheet\\Reader\\Html\:\:processDomElementThTd\(\) expects string, mixed given\.$#'
1289-
identifier: parameterByRef.type
1290-
count: 1
1291-
path: src/PhpSpreadsheet/Reader/Html.php
1292-
1293-
-
1294-
message: '#^Binary operation "\-" between mixed and 1 results in an error\.$#'
1295-
identifier: binaryOp.invalid
1296-
count: 1
1297-
path: src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php
1298-
1299-
-
1300-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Shared\\CodePage\:\:numberToName\(\) should return string but returns mixed\.$#'
1301-
identifier: return.type
1302-
count: 1
1303-
path: src/PhpSpreadsheet/Shared/CodePage.php
1304-
1305-
-
1306-
message: '#^Parameter \#2 \$to_encoding of function iconv expects string, mixed given\.$#'
1307-
identifier: argument.type
1308-
count: 1
1309-
path: src/PhpSpreadsheet/Shared/CodePage.php
1310-
13111203
-
13121204
message: '#^Cannot access an offset on mixed\.$#'
13131205
identifier: offsetAccess.nonOffsetAccessible
@@ -1379,105 +1271,3 @@ parameters:
13791271
identifier: clone.nonObject
13801272
count: 2
13811273
path: src/PhpSpreadsheet/Shared/OLE/PPS.php
1382-
1383-
-
1384-
message: '#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\.$#'
1385-
identifier: foreach.nonIterable
1386-
count: 1
1387-
path: src/PhpSpreadsheet/Spreadsheet.php
1388-
1389-
-
1390-
message: '#^Cannot clone non\-object variable \$item of type mixed\.$#'
1391-
identifier: clone.nonObject
1392-
count: 1
1393-
path: src/PhpSpreadsheet/Spreadsheet.php
1394-
1395-
-
1396-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Spreadsheet\:\:copy\(\) should return PhpOffice\\PhpSpreadsheet\\Spreadsheet but returns mixed\.$#'
1397-
identifier: return.type
1398-
count: 1
1399-
path: src/PhpSpreadsheet/Spreadsheet.php
1400-
1401-
-
1402-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Spreadsheet\:\:getRibbonBinObjects\(\) should return array\|null but returns mixed\.$#'
1403-
identifier: return.type
1404-
count: 1
1405-
path: src/PhpSpreadsheet/Spreadsheet.php
1406-
1407-
-
1408-
message: '#^Binary operation "\." between ''"'' and mixed results in an error\.$#'
1409-
identifier: binaryOp.invalid
1410-
count: 1
1411-
path: src/PhpSpreadsheet/Style/ConditionalFormatting/CellMatcher.php
1412-
1413-
-
1414-
message: '#^Binary operation "\*" between 24\|1440\|86400 and mixed results in an error\.$#'
1415-
identifier: binaryOp.invalid
1416-
count: 1
1417-
path: src/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php
1418-
1419-
-
1420-
message: '#^Cannot cast mixed to string\.$#'
1421-
identifier: cast.string
1422-
count: 2
1423-
path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
1424-
1425-
-
1426-
message: '#^Parameter \#2 \$replace of function str_replace expects array\<string\>\|string, mixed given\.$#'
1427-
identifier: argument.type
1428-
count: 1
1429-
path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
1430-
1431-
-
1432-
message: '#^Binary operation "\*\=" between mixed and int\<1, max\> results in an error\.$#'
1433-
identifier: assignOp.invalid
1434-
count: 1
1435-
path: src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php
1436-
1437-
-
1438-
message: '#^Cannot clone non\-object variable \$v of type mixed\.$#'
1439-
identifier: clone.nonObject
1440-
count: 1
1441-
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
1442-
1443-
-
1444-
message: '#^Cannot clone non\-object variable \$v of type mixed\.$#'
1445-
identifier: clone.nonObject
1446-
count: 1
1447-
path: src/PhpSpreadsheet/Worksheet/Table.php
1448-
1449-
-
1450-
message: '#^Parameter \#2 \$subject of function preg_match expects string, mixed given\.$#'
1451-
identifier: argument.type
1452-
count: 1
1453-
path: src/PhpSpreadsheet/Worksheet/Worksheet.php
1454-
1455-
-
1456-
message: '#^Parameter \#2 \$cellAddress of method PhpOffice\\PhpSpreadsheet\\Writer\\Html\:\:generateRowCellCss\(\) expects string, mixed given\.$#'
1457-
identifier: argument.type
1458-
count: 1
1459-
path: src/PhpSpreadsheet/Writer/Html.php
1460-
1461-
-
1462-
message: '#^Parameter \#1 \$token of method PhpOffice\\PhpSpreadsheet\\Writer\\Xls\\Parser\:\:convert\(\) expects string, mixed given\.$#'
1463-
identifier: argument.type
1464-
count: 1
1465-
path: src/PhpSpreadsheet/Writer/Xls/Parser.php
1466-
1467-
-
1468-
message: '#^Parameter \#1 \$tree of method PhpOffice\\PhpSpreadsheet\\Writer\\Xls\\Parser\:\:toReversePolish\(\) expects array, mixed given\.$#'
1469-
identifier: argument.type
1470-
count: 1
1471-
path: src/PhpSpreadsheet/Writer/Xls/Parser.php
1472-
1473-
-
1474-
message: '#^Cannot cast mixed to string\.$#'
1475-
identifier: cast.string
1476-
count: 1
1477-
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php
1478-
1479-
-
1480-
message: '#^Parameter \#2 \$subject of function preg_match expects string, mixed given\.$#'
1481-
identifier: argument.type
1482-
count: 1
1483-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

src/PhpSpreadsheet/Calculation/TextData/Text.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
99
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
1010
use PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue;
11+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
1112

1213
class Text
1314
{
@@ -113,7 +114,7 @@ public static function split(mixed $text, $columnDelimiter = null, $rowDelimiter
113114
{
114115
$text = Functions::flattenSingleValue($text);
115116
if (ErrorValue::isError($text, true)) {
116-
return $text;
117+
return StringHelper::convertToString($text);
117118
}
118119

119120
$flags = self::matchFlags($matchMode);
@@ -122,7 +123,7 @@ public static function split(mixed $text, $columnDelimiter = null, $rowDelimiter
122123
$delimiter = self::buildDelimiter($rowDelimiter);
123124
$rows = ($delimiter === '()')
124125
? [$text]
125-
: Preg::split("/{$delimiter}/{$flags}", $text);
126+
: Preg::split("/{$delimiter}/{$flags}", StringHelper::convertToString($text));
126127
} else {
127128
$rows = [$text];
128129
}
@@ -141,7 +142,7 @@ public static function split(mixed $text, $columnDelimiter = null, $rowDelimiter
141142
function (&$row) use ($delimiter, $flags, $ignoreEmpty): void {
142143
$row = ($delimiter === '()')
143144
? [$row]
144-
: Preg::split("/{$delimiter}/{$flags}", $row);
145+
: Preg::split("/{$delimiter}/{$flags}", StringHelper::convertToString($row));
145146
if ($ignoreEmpty === true) {
146147
$row = array_values(array_filter(
147148
$row,
@@ -195,7 +196,7 @@ private static function buildDelimiter($delimiter): string
195196
return '(' . $delimiters . ')';
196197
}
197198

198-
return '(' . preg_quote(Functions::flattenSingleValue($delimiter), '/') . ')';
199+
return '(' . preg_quote(StringHelper::convertToString(Functions::flattenSingleValue($delimiter)), '/') . ')';
199200
}
200201

201202
private static function matchFlags(bool $matchMode): string
@@ -226,7 +227,7 @@ private static function formatValueMode0(mixed $cellValue): string
226227
return Calculation::getLocaleBoolean($cellValue ? 'TRUE' : 'FALSE');
227228
}
228229

229-
return (string) $cellValue;
230+
return StringHelper::convertToString($cellValue);
230231
}
231232

232233
private static function formatValueMode1(mixed $cellValue): string
@@ -237,6 +238,6 @@ private static function formatValueMode1(mixed $cellValue): string
237238
return Calculation::getLocaleBoolean($cellValue ? 'TRUE' : 'FALSE');
238239
}
239240

240-
return (string) $cellValue;
241+
return StringHelper::convertToString($cellValue);
241242
}
242243
}

src/PhpSpreadsheet/Cell/DataValidator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
77
use PhpOffice\PhpSpreadsheet\Exception;
8+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
89

910
/**
1011
* Validate a cell value according to its validation rules.
@@ -60,8 +61,9 @@ private static function evaluateNumericFormula(mixed $formula, Cell $cell): mixe
6061
$calculation = Calculation::getInstance($cell->getWorksheet()->getParent());
6162

6263
try {
64+
$formula2 = StringHelper::convertToString($formula);
6365
$result = $calculation
64-
->calculateFormula("=$formula", $cell->getCoordinate(), $cell);
66+
->calculateFormula("=$formula2", $cell->getCoordinate(), $cell);
6567
while (is_array($result)) {
6668
$result = array_pop($result);
6769
}

src/PhpSpreadsheet/Helper/Sample.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpOffice\PhpSpreadsheet\Chart\Renderer\MtJpGraphRenderer;
77
use PhpOffice\PhpSpreadsheet\IOFactory;
88
use PhpOffice\PhpSpreadsheet\Settings;
9+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
910
use PhpOffice\PhpSpreadsheet\Spreadsheet;
1011
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
1112
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
@@ -35,7 +36,7 @@ public function isCli(): bool
3536
*/
3637
public function getScriptFilename(): string
3738
{
38-
return basename($_SERVER['SCRIPT_FILENAME'], '.php');
39+
return basename(StringHelper::convertToString($_SERVER['SCRIPT_FILENAME']), '.php');
3940
}
4041

4142
/**

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpOffice\PhpSpreadsheet\Helper\Dimension as CssDimension;
1717
use PhpOffice\PhpSpreadsheet\Helper\Html as HelperHtml;
1818
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
19+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
1920
use PhpOffice\PhpSpreadsheet\Spreadsheet;
2021
use PhpOffice\PhpSpreadsheet\Style\Border;
2122
use PhpOffice\PhpSpreadsheet\Style\Color;
@@ -250,6 +251,8 @@ protected function releaseTableStartColumn(): string
250251

251252
/**
252253
* Flush cell.
254+
*
255+
* @param-out string $cellContent In one case, it can be bool
253256
*/
254257
protected function flushCell(Worksheet $sheet, string $column, int|string $row, mixed &$cellContent, array $attributeArray): void
255258
{
@@ -272,7 +275,8 @@ protected function flushCell(Worksheet $sheet, string $column, int|string $row,
272275
}
273276
}
274277
if ($datatype === DataType::TYPE_BOOL) {
275-
$cellContent = self::convertBoolean($cellContent);
278+
// This is the case where we can set cellContent to bool rather than string
279+
$cellContent = self::convertBoolean($cellContent); //* @phpstan-ignore-line
276280
if (!is_bool($cellContent)) {
277281
$attributeArray['data-type'] = DataType::TYPE_STRING;
278282
}
@@ -292,7 +296,7 @@ protected function flushCell(Worksheet $sheet, string $column, int|string $row,
292296
} else {
293297
// We have a Rich Text run
294298
// TODO
295-
$this->dataArray[$row][$column] = 'RICH TEXT: ' . $cellContent;
299+
$this->dataArray[$row][$column] = 'RICH TEXT: ' . StringHelper::convertToString($cellContent);
296300
}
297301
$cellContent = (string) '';
298302
}
@@ -622,6 +626,7 @@ private function processDomElementThTd(Worksheet $sheet, int &$row, string &$col
622626
// apply inline style
623627
$this->applyInlineStyle($sheet, $row, $column, $attributeArray);
624628

629+
/** @var string $cellContent */
625630
$this->flushCell($sheet, $column, $row, $cellContent, $attributeArray);
626631

627632
$this->processDomElementBgcolor($sheet, $row, $column, $attributeArray);

src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ protected function loadSpreadsheetFromFile2(string $filename, Xls $xls): Spreads
480480
case 0x08:
481481
// picture
482482
// get index to BSE entry (1-based)
483+
/** @var int */
483484
$BSEindex = $spContainer->getOPT(0x0104);
484485

485486
// If there is no BSE Index, we will fail here and other fields are not read.

src/PhpSpreadsheet/Shared/CodePage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class CodePage
88
{
99
public const DEFAULT_CODE_PAGE = 'CP1252';
1010

11+
/** @var array<int, array<int, string>|string> */
1112
private static array $pageArray = [
1213
0 => 'CP1252', // CodePage is not always correctly set when the xls file was saved by Apple's Numbers program
1314
367 => 'ASCII', // ASCII

0 commit comments

Comments
 (0)