Skip to content

Commit bf46ff1

Browse files
author
MarkBaker
committed
Explicit type-cast result from preg_replace() calls to string, cleans up a few phpstan issues
1 parent 9e4ff92 commit bf46ff1

27 files changed

+63
-135
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ parameters:
125125
count: 1
126126
path: src/PhpSpreadsheet/Calculation/Calculation.php
127127

128-
-
129-
message: "#^Parameter \\#3 \\$formula of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Calculation\\:\\:translateSeparator\\(\\) expects string, string\\|null given\\.$#"
130-
count: 1
131-
path: src/PhpSpreadsheet/Calculation/Calculation.php
132-
133128
-
134129
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Calculation\\:\\:\\$cellStack has no type specified\\.$#"
135130
count: 1
@@ -750,11 +745,6 @@ parameters:
750745
count: 1
751746
path: src/PhpSpreadsheet/Calculation/LookupRef/Offset.php
752747

753-
-
754-
message: "#^Parameter \\#1 \\$columnAddress of static method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Coordinate\\:\\:columnIndexFromString\\(\\) expects string, string\\|null given\\.$#"
755-
count: 3
756-
path: src/PhpSpreadsheet/Calculation/LookupRef/RowColumnInformation.php
757-
758748
-
759749
message: "#^Binary operation \"/\" between array\\|float\\|int\\|string and array\\|float\\|int\\|string results in an error\\.$#"
760750
count: 2
@@ -1710,11 +1700,6 @@ parameters:
17101700
count: 1
17111701
path: src/PhpSpreadsheet/Helper/Html.php
17121702

1713-
-
1714-
message: "#^Parameter \\#1 \\$text of method PhpOffice\\\\PhpSpreadsheet\\\\RichText\\\\ITextElement\\:\\:setText\\(\\) expects string, string\\|null given\\.$#"
1715-
count: 1
1716-
path: src/PhpSpreadsheet/Helper/Html.php
1717-
17181703
-
17191704
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$bold has no type specified\\.$#"
17201705
count: 1
@@ -1815,11 +1800,6 @@ parameters:
18151800
count: 1
18161801
path: src/PhpSpreadsheet/Helper/Sample.php
18171802

1818-
-
1819-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, string\\|null given\\.$#"
1820-
count: 1
1821-
path: src/PhpSpreadsheet/Helper/Sample.php
1822-
18231803
-
18241804
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\IOFactory\\:\\:createReader\\(\\) should return PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\IReader but returns object\\.$#"
18251805
count: 1
@@ -2725,11 +2705,6 @@ parameters:
27252705
count: 1
27262706
path: src/PhpSpreadsheet/ReferenceHelper.php
27272707

2728-
-
2729-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, string\\|null given\\.$#"
2730-
count: 1
2731-
path: src/PhpSpreadsheet/ReferenceHelper.php
2732-
27332708
-
27342709
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\RichText\\\\Run\\:\\:\\$font \\(PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
27352710
count: 1
@@ -3725,26 +3700,11 @@ parameters:
37253700
count: 1
37263701
path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
37273702

3728-
-
3729-
message: "#^Parameter \\#2 \\$subject of function preg_split expects string, string\\|null given\\.$#"
3730-
count: 1
3731-
path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
3732-
3733-
-
3734-
message: "#^Parameter \\#3 \\$subject of function preg_replace expects array\\|string, string\\|null given\\.$#"
3735-
count: 1
3736-
path: src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
3737-
37383703
-
37393704
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\PercentageFormatter\\:\\:format\\(\\) has parameter \\$value with no type specified\\.$#"
37403705
count: 1
37413706
path: src/PhpSpreadsheet/Style/NumberFormat/PercentageFormatter.php
37423707

3743-
-
3744-
message: "#^Parameter \\#1 \\$format of function sprintf expects string, string\\|null given\\.$#"
3745-
count: 1
3746-
path: src/PhpSpreadsheet/Style/NumberFormat/PercentageFormatter.php
3747-
37483708
-
37493709
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\CellIterator\\:\\:adjustForExistingOnlyRange\\(\\) has no return type specified\\.$#"
37503710
count: 1
@@ -4180,11 +4140,6 @@ parameters:
41804140
count: 1
41814141
path: src/PhpSpreadsheet/Writer/Html.php
41824142

4183-
-
4184-
message: "#^Parameter \\#1 \\$string of function htmlspecialchars expects string, string\\|null given\\.$#"
4185-
count: 1
4186-
path: src/PhpSpreadsheet/Writer/Html.php
4187-
41884143
-
41894144
message: "#^Parameter \\#1 \\$vAlign of method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Html\\:\\:mapVAlign\\(\\) expects string, string\\|null given\\.$#"
41904145
count: 1
@@ -4380,16 +4335,6 @@ parameters:
43804335
count: 7
43814336
path: src/PhpSpreadsheet/Writer/Xls/Parser.php
43824337

4383-
-
4384-
message: "#^Parameter \\#3 \\$subject of function preg_replace expects array\\|string, string\\|null given\\.$#"
4385-
count: 1
4386-
path: src/PhpSpreadsheet/Writer/Xls/Parser.php
4387-
4388-
-
4389-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, string\\|null given\\.$#"
4390-
count: 1
4391-
path: src/PhpSpreadsheet/Writer/Xls/Parser.php
4392-
43934338
-
43944339
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Parser\\:\\:\\$spreadsheet has no type specified\\.$#"
43954340
count: 1
@@ -4490,21 +4435,6 @@ parameters:
44904435
count: 1
44914436
path: src/PhpSpreadsheet/Writer/Xls/Worksheet.php
44924437

4493-
-
4494-
message: "#^Parameter \\#2 \\$subject of function preg_match expects string, string\\|null given\\.$#"
4495-
count: 2
4496-
path: src/PhpSpreadsheet/Writer/Xls/Worksheet.php
4497-
4498-
-
4499-
message: "#^Parameter \\#2 \\$subject of function preg_match_all expects string, string\\|null given\\.$#"
4500-
count: 1
4501-
path: src/PhpSpreadsheet/Writer/Xls/Worksheet.php
4502-
4503-
-
4504-
message: "#^Parameter \\#3 \\$subject of function preg_replace expects array\\|string, string\\|null given\\.$#"
4505-
count: 2
4506-
path: src/PhpSpreadsheet/Writer/Xls/Worksheet.php
4507-
45084438
-
45094439
message: "#^Parameter \\#4 \\$isError of method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Worksheet\\:\\:writeBoolErr\\(\\) expects bool, int given\\.$#"
45104440
count: 3

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3197,7 +3197,7 @@ private static function translateFormulaBlock(
31973197
string $toSeparator
31983198
): string {
31993199
// Function Names
3200-
$formula = preg_replace($from, $to, $formula);
3200+
$formula = (string) preg_replace($from, $to, $formula);
32013201

32023202
// Temporarily adjust matrix separators so that they won't be confused with function arguments
32033203
$formula = self::translateSeparator(';', '|', $formula, $inMatrixBracesLevel, self::FORMULA_OPEN_MATRIX_BRACE, self::FORMULA_CLOSE_MATRIX_BRACE);
@@ -4180,7 +4180,7 @@ private function internalParseFormula($formula, ?Cell $cell = null)
41804180
$length = strlen($val);
41814181

41824182
if (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/miu', $val, $matches)) {
4183-
$val = preg_replace('/\s/u', '', $val);
4183+
$val = (string) preg_replace('/\s/u', '', $val);
41844184
if (isset(self::$phpSpreadsheetFunctions[strtoupper($matches[1])]) || isset(self::$controlFunctions[strtoupper($matches[1])])) { // it's a function
41854185
$valToUpper = strtoupper($val);
41864186
} else {

src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function fromString($dateValue)
4949
$baseYear = SharedDateHelper::getExcelCalendar();
5050
$dateValue = trim($dateValue ?? '', '"');
5151
// Strip any ordinals because they're allowed in Excel (English only)
52-
$dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui', '$1$3', $dateValue) ?? '';
52+
$dateValue = (string) preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui', '$1$3', $dateValue);
5353
// Convert separators (/ . or space) to hyphens (should also handle dot used for ordinals in some countries, e.g. Denmark, Germany)
5454
$dateValue = str_replace(['/', '.', '-', ' '], ' ', $dateValue);
5555

src/PhpSpreadsheet/Calculation/Functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public static function ifCondition($condition)
159159
} elseif (!is_numeric($condition)) {
160160
if ($condition !== '""') { // Not an empty string
161161
// Escape any quotes in the string value
162-
$condition = preg_replace('/"/ui', '""', $condition);
162+
$condition = (string) preg_replace('/"/ui', '""', $condition);
163163
}
164164
$condition = Calculation::wrapResult(strtoupper($condition));
165165
}

src/PhpSpreadsheet/Calculation/LookupRef/Helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function extractCellAddresses(string $cellAddress, bool $a1, Works
4343
if ($namedRange !== null) {
4444
$workSheet = $namedRange->getWorkSheet();
4545
$sheetTitle = ($workSheet === null) ? '' : $workSheet->getTitle();
46-
$value = preg_replace('/^=/', '', $namedRange->getValue());
46+
$value = (string) preg_replace('/^=/', '', $namedRange->getValue());
4747
self::adjustSheetTitle($sheetTitle, $value);
4848
$cellAddress1 = $sheetTitle . $value;
4949
$cellAddress = $cellAddress1;

src/PhpSpreadsheet/Calculation/LookupRef/RowColumnInformation.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function COLUMN($cellAddress = null, ?Cell $cell = null)
5050

5151
if (is_array($cellAddress)) {
5252
foreach ($cellAddress as $columnKey => $value) {
53-
$columnKey = preg_replace('/[^a-z]/i', '', $columnKey);
53+
$columnKey = (string) preg_replace('/[^a-z]/i', '', $columnKey);
5454

5555
return (int) Coordinate::columnIndexFromString($columnKey);
5656
}
@@ -66,16 +66,16 @@ public static function COLUMN($cellAddress = null, ?Cell $cell = null)
6666
[, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true);
6767
if (strpos($cellAddress, ':') !== false) {
6868
[$startAddress, $endAddress] = explode(':', $cellAddress);
69-
$startAddress = preg_replace('/[^a-z]/i', '', $startAddress);
70-
$endAddress = preg_replace('/[^a-z]/i', '', $endAddress);
69+
$startAddress = (string) preg_replace('/[^a-z]/i', '', $startAddress);
70+
$endAddress = (string) preg_replace('/[^a-z]/i', '', $endAddress);
7171

7272
return range(
7373
(int) Coordinate::columnIndexFromString($startAddress),
7474
(int) Coordinate::columnIndexFromString($endAddress)
7575
);
7676
}
7777

78-
$cellAddress = preg_replace('/[^a-z]/i', '', $cellAddress);
78+
$cellAddress = (string) preg_replace('/[^a-z]/i', '', $cellAddress);
7979

8080
return (int) Coordinate::columnIndexFromString($cellAddress);
8181
}
@@ -159,14 +159,13 @@ public static function ROW($cellAddress = null, ?Cell $cell = null)
159159
[, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true);
160160
if (strpos($cellAddress, ':') !== false) {
161161
[$startAddress, $endAddress] = explode(':', $cellAddress);
162-
$startAddress = preg_replace('/\D/', '', $startAddress);
163-
$endAddress = preg_replace('/\D/', '', $endAddress);
162+
$startAddress = (string) preg_replace('/\D/', '', $startAddress);
163+
$endAddress = (string) preg_replace('/\D/', '', $endAddress);
164164

165165
return array_map(
166166
function ($value) {
167167
return [$value];
168168
},
169-
// @phpstan-ignore-next-line
170169
range($startAddress, $endAddress)
171170
);
172171
}

src/PhpSpreadsheet/Document/Properties.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ private static function intOrFloatTimestamp($timestamp)
171171
if (is_numeric($timestamp)) {
172172
$timestamp = (float) $timestamp;
173173
} else {
174-
$timestamp = preg_replace('/[.][0-9]*$/', '', $timestamp) ?? '';
175-
$timestamp = preg_replace('/^(\\d{4})- (\\d)/', '$1-0$2', $timestamp) ?? '';
176-
$timestamp = preg_replace('/^(\\d{4}-\\d{2})- (\\d)/', '$1-0$2', $timestamp) ?? '';
174+
$timestamp = (string) preg_replace('/[.][0-9]*$/', '', $timestamp);
175+
$timestamp = (string) preg_replace('/^(\\d{4})- (\\d)/', '$1-0$2', $timestamp);
176+
$timestamp = (string) preg_replace('/^(\\d{4}-\\d{2})- (\\d)/', '$1-0$2', $timestamp);
177177
$timestamp = (float) (new DateTime($timestamp))->format('U');
178178
}
179179
}

src/PhpSpreadsheet/Helper/Html.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ protected function cleanWhitespace(): void
642642
$text = ltrim($text);
643643
}
644644
// Trim any spaces immediately after a line break
645-
$text = preg_replace('/\n */mu', "\n", $text);
645+
$text = (string) preg_replace('/\n */mu', "\n", $text);
646646
$element->setText($text);
647647
}
648648
}
@@ -792,7 +792,7 @@ protected function breakTag(): void
792792

793793
protected function parseTextNode(DOMText $textNode): void
794794
{
795-
$domText = preg_replace(
795+
$domText = (string) preg_replace(
796796
'/\s+/u',
797797
' ',
798798
str_replace(["\r", "\n"], ' ', $textNode->nodeValue ?? '')

src/PhpSpreadsheet/Helper/Sample.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getSamples()
8585
$file = str_replace(str_replace('\\', '/', $baseDir) . '/', '', str_replace('\\', '/', $file[0]));
8686
$info = pathinfo($file);
8787
$category = str_replace('_', ' ', $info['dirname']);
88-
$name = str_replace('_', ' ', preg_replace('/(|\.php)/', '', $info['filename']));
88+
$name = str_replace('_', ' ', (string) preg_replace('/(|\.php)/', '', $info['filename']));
8989
if (!in_array($category, ['.', 'boostrap', 'templates'])) {
9090
if (!isset($files[$category])) {
9191
$files[$category] = [];

src/PhpSpreadsheet/Reader/Csv/Delimiter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ public function getNextLine()
140140
$line = $line . $newLine;
141141

142142
// Drop everything that is enclosed to avoid counting false positives in enclosures
143-
$line = preg_replace('/(' . $enclosure . '.*' . $enclosure . ')/Us', '', $line);
143+
$line = (string) preg_replace('/(' . $enclosure . '.*' . $enclosure . ')/Us', '', $line);
144144

145145
// See if we have any enclosures left in the line
146146
// if we still have an enclosure then we need to read the next line as well
147-
} while (preg_match('/(' . $enclosure . ')/', $line ?? '') > 0);
147+
} while (preg_match('/(' . $enclosure . ')/', $line) > 0);
148148

149-
return $line ?? false;
149+
return ($line !== '') ? $line : false;
150150
}
151151
}

0 commit comments

Comments
 (0)