Skip to content

Commit 4323fcf

Browse files
authored
Merge pull request #4428 from oleibman/stan2lv908
Phpstan Level 9 - Part 8 of Many - TextData
2 parents 8a61a18 + 078846c commit 4323fcf

File tree

7 files changed

+29
-137
lines changed

7 files changed

+29
-137
lines changed

Diff for: phpstan-baseline.neon

-114
Original file line numberDiff line numberDiff line change
@@ -269,117 +269,3 @@ parameters:
269269
identifier: argument.type
270270
count: 9
271271
path: src/PhpSpreadsheet/Calculation/Statistical/Trends.php
272-
273-
-
274-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\CharacterConvert\:\:unicodeToOrd\(\) should return int but returns mixed\.$#'
275-
identifier: return.type
276-
count: 1
277-
path: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php
278-
279-
-
280-
message: '#^Binary operation "\." between mixed and mixed results in an error\.$#'
281-
identifier: binaryOp.invalid
282-
count: 1
283-
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php
284-
285-
-
286-
message: '#^Cannot cast mixed to string\.$#'
287-
identifier: cast.string
288-
count: 1
289-
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php
290-
291-
-
292-
message: '#^Parameter \#1 \$ignoreEmpty of static method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\Concatenate\:\:evaluateTextJoinArray\(\) expects bool, mixed given\.$#'
293-
identifier: argument.type
294-
count: 1
295-
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php
296-
297-
-
298-
message: '#^Parameter \#1 \$separator of function implode expects array\|string, mixed given\.$#'
299-
identifier: argument.type
300-
count: 1
301-
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php
302-
303-
-
304-
message: '#^Cannot cast mixed to int\.$#'
305-
identifier: cast.int
306-
count: 6
307-
path: src/PhpSpreadsheet/Calculation/TextData/Extract.php
308-
309-
-
310-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\Extract\:\:validateTextBeforeAfter\(\) should return array\|string but returns mixed\.$#'
311-
identifier: return.type
312-
count: 1
313-
path: src/PhpSpreadsheet/Calculation/TextData/Extract.php
314-
315-
-
316-
message: '#^Cannot cast mixed to string\.$#'
317-
identifier: cast.string
318-
count: 3
319-
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
320-
321-
-
322-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\Format\:\:VALUE\(\) should return array\|DateTimeInterface\|float\|int\|string but returns mixed\.$#'
323-
identifier: return.type
324-
count: 2
325-
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
326-
327-
-
328-
message: '#^Parameter \#1 \$dateValue of static method PhpOffice\\PhpSpreadsheet\\Calculation\\DateTimeExcel\\DateValue\:\:fromString\(\) expects array\|bool\|float\|int\|string\|null, mixed given\.$#'
329-
identifier: argument.type
330-
count: 1
331-
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
332-
333-
-
334-
message: '#^Parameter \#1 \$haystack of function str_contains expects string, mixed given\.$#'
335-
identifier: argument.type
336-
count: 1
337-
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
338-
339-
-
340-
message: '#^Parameter \#1 \$haystack of function strpos expects string, mixed given\.$#'
341-
identifier: argument.type
342-
count: 1
343-
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
344-
345-
-
346-
message: '#^Parameter \#1 \$string of function trim expects string, mixed given\.$#'
347-
identifier: argument.type
348-
count: 1
349-
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
350-
351-
-
352-
message: '#^Parameter \#1 \$timeValue of static method PhpOffice\\PhpSpreadsheet\\Calculation\\DateTimeExcel\\TimeValue\:\:fromString\(\) expects array\|bool\|float\|int\|string\|null, mixed given\.$#'
353-
identifier: argument.type
354-
count: 1
355-
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
356-
357-
-
358-
message: '#^Parameter \#2 \$subject of static method Composer\\Pcre\\Preg\:\:matchAllWithOffsets\(\) expects string, mixed given\.$#'
359-
identifier: argument.type
360-
count: 1
361-
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
362-
363-
-
364-
message: '#^Parameter \#3 \$subject of function str_replace expects array\<string\>\|string, mixed given\.$#'
365-
identifier: argument.type
366-
count: 2
367-
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
368-
369-
-
370-
message: '#^Cannot cast mixed to int\.$#'
371-
identifier: cast.int
372-
count: 1
373-
path: src/PhpSpreadsheet/Calculation/TextData/Helpers.php
374-
375-
-
376-
message: '#^Cannot cast mixed to string\.$#'
377-
identifier: cast.string
378-
count: 1
379-
path: src/PhpSpreadsheet/Calculation/TextData/Helpers.php
380-
381-
-
382-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\Replace\:\:executeSubstitution\(\) should return string but returns mixed\.$#'
383-
identifier: return.type
384-
count: 1
385-
path: src/PhpSpreadsheet/Calculation/TextData/Replace.php

Diff for: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private static function unicodeToOrd(string $character): int
8181
$retVal = 0;
8282
$iconv = iconv('UTF-8', 'UCS-4LE', $character);
8383
if ($iconv !== false) {
84+
/** @var false|int[] */
8485
$result = unpack('V', $iconv);
8586
if (is_array($result) && isset($result[1])) {
8687
$retVal = $result[1];

Diff for: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ private static function concatenate2Args(array|string $operand1, null|array|bool
7878
return $operand2[$row][$column];
7979
}
8080
$operand1[$row][$column]
81-
= Calculation::boolToString($operand1[$row][$column])
82-
. Calculation::boolToString($operand2[$row][$column]);
81+
= StringHelper::convertToString($operand1[$row][$column], convertBool: true)
82+
. StringHelper::convertToString($operand2[$row][$column], convertBool: true);
8383
if (mb_strlen($operand1[$row][$column]) > DataType::MAX_STRING_LENGTH) {
8484
$operand1 = ExcelError::CALC();
8585
$errorFound = true;
@@ -91,7 +91,7 @@ private static function concatenate2Args(array|string $operand1, null|array|bool
9191
} elseif (ErrorValue::isError($operand2, true) === true) {
9292
$operand1 = (string) $operand2;
9393
} else {
94-
$operand1 .= (string) Calculation::boolToString($operand2);
94+
$operand1 .= StringHelper::convertToString($operand2, convertBool: true);
9595
if (mb_strlen($operand1) > DataType::MAX_STRING_LENGTH) {
9696
$operand1 = ExcelError::CALC();
9797
}
@@ -103,17 +103,17 @@ private static function concatenate2Args(array|string $operand1, null|array|bool
103103
/**
104104
* TEXTJOIN.
105105
*
106-
* @param mixed $delimiter The delimter to use between the joined arguments
106+
* @param null|string|string[] $delimiter The delimiter to use between the joined arguments
107107
* Or can be an array of values
108-
* @param mixed $ignoreEmpty true/false Flag indicating whether empty arguments should be skipped
108+
* @param null|bool|bool[] $ignoreEmpty true/false Flag indicating whether empty arguments should be skipped
109109
* Or can be an array of values
110110
* @param mixed $args The values to join
111111
*
112112
* @return array|string The joined string
113113
* If an array of values is passed for the $delimiter or $ignoreEmpty arguments, then the returned result
114114
* will also be an array with matching dimensions
115115
*/
116-
public static function TEXTJOIN(mixed $delimiter = '', mixed $ignoreEmpty = true, mixed ...$args): array|string
116+
public static function TEXTJOIN($delimiter = '', $ignoreEmpty = true, mixed ...$args): array|string
117117
{
118118
if (is_array($delimiter) || is_array($ignoreEmpty)) {
119119
return self::evaluateArrayArgumentsSubset(
@@ -127,6 +127,7 @@ public static function TEXTJOIN(mixed $delimiter = '', mixed $ignoreEmpty = true
127127

128128
$delimiter ??= '';
129129
$ignoreEmpty ??= true;
130+
/** @var array */
130131
$aArgs = Functions::flattenArray($args);
131132
$returnValue = self::evaluateTextJoinArray($ignoreEmpty, $aArgs);
132133

Diff for: src/PhpSpreadsheet/Calculation/TextData/Extract.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ public static function before(mixed $text, $delimiter, mixed $instance = 1, mixe
139139
return $e->getMessage();
140140
}
141141

142-
$instance = (int) $instance;
143-
$matchMode = (int) $matchMode;
144-
$matchEnd = (int) $matchEnd;
142+
$instance = (int) StringHelper::convertToString($instance);
143+
$matchMode = (int) StringHelper::convertToString($matchMode);
144+
$matchEnd = (int) StringHelper::convertToString($matchEnd);
145145

146146
$split = self::validateTextBeforeAfter($text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound);
147147
if (is_string($split)) {
@@ -203,9 +203,9 @@ public static function after(mixed $text, $delimiter, mixed $instance = 1, mixed
203203
return $e->getMessage();
204204
}
205205

206-
$instance = (int) $instance;
207-
$matchMode = (int) $matchMode;
208-
$matchEnd = (int) $matchEnd;
206+
$instance = (int) StringHelper::convertToString($instance);
207+
$matchMode = (int) StringHelper::convertToString($matchMode);
208+
$matchEnd = (int) StringHelper::convertToString($matchEnd);
209209

210210
$split = self::validateTextBeforeAfter($text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound);
211211
if (is_string($split)) {
@@ -234,7 +234,7 @@ private static function validateTextBeforeAfter(string $text, null|array|string
234234
$delimiter = self::buildDelimiter($delimiter);
235235

236236
if (preg_match('/' . $delimiter . "/{$flags}", $text) === 0 && $matchEnd === 0) {
237-
return $ifNotFound;
237+
return is_array($ifNotFound) ? $ifNotFound : StringHelper::convertToString($ifNotFound);
238238
}
239239

240240
$split = preg_split('/' . $delimiter . "/{$flags}", $text, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

Diff for: src/PhpSpreadsheet/Calculation/TextData/Format.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public static function VALUE(mixed $value = '')
192192
return $e->getMessage();
193193
}
194194
if (!is_numeric($value)) {
195+
$value = StringHelper::convertToString($value);
195196
$numberValue = str_replace(
196197
StringHelper::getThousandsSeparator(),
197198
'',
@@ -212,14 +213,14 @@ public static function VALUE(mixed $value = '')
212213
if ($timeValue !== ExcelError::VALUE()) {
213214
Functions::setReturnDateType($dateSetting);
214215

215-
return $timeValue;
216+
return $timeValue; //* @phpstan-ignore-line
216217
}
217218
}
218219
$dateValue = Functions::scalar(DateTimeExcel\DateValue::fromString($value));
219220
if ($dateValue !== ExcelError::VALUE()) {
220221
Functions::setReturnDateType($dateSetting);
221222

222-
return $dateValue;
223+
return $dateValue; //* @phpstan-ignore-line
223224
}
224225
Functions::setReturnDateType($dateSetting);
225226

@@ -250,23 +251,23 @@ public static function valueToText(mixed $value, mixed $format = false): array|s
250251
$value = $value->getPlainText();
251252
}
252253
if (is_string($value)) {
253-
$value = ($format === true) ? Calculation::wrapResult($value) : $value;
254+
$value = ($format === true) ? StringHelper::convertToString(Calculation::wrapResult($value)) : $value;
254255
$value = str_replace("\n", '', $value);
255256
} elseif (is_bool($value)) {
256257
$value = Calculation::getLocaleBoolean($value ? 'TRUE' : 'FALSE');
257258
}
258259

259-
return (string) $value;
260+
return StringHelper::convertToString($value);
260261
}
261262

262263
private static function getDecimalSeparator(mixed $decimalSeparator): string
263264
{
264-
return empty($decimalSeparator) ? StringHelper::getDecimalSeparator() : (string) $decimalSeparator;
265+
return empty($decimalSeparator) ? StringHelper::getDecimalSeparator() : StringHelper::convertToString($decimalSeparator);
265266
}
266267

267268
private static function getGroupSeparator(mixed $groupSeparator): string
268269
{
269-
return empty($groupSeparator) ? StringHelper::getThousandsSeparator() : (string) $groupSeparator;
270+
return empty($groupSeparator) ? StringHelper::getThousandsSeparator() : StringHelper::convertToString($groupSeparator);
270271
}
271272

272273
/**
@@ -293,7 +294,9 @@ public static function NUMBERVALUE(mixed $value = '', mixed $decimalSeparator =
293294
return $e->getMessage();
294295
}
295296

296-
if (!is_numeric($value)) {
297+
/** @var null|array|scalar $value */
298+
if (!is_array($value) && !is_numeric($value)) {
299+
$value = StringHelper::convertToString($value);
297300
$decimalPositions = Preg::matchAllWithOffsets('/' . preg_quote($decimalSeparator, '/') . '/', $value, $matches);
298301
if ($decimalPositions > 1) {
299302
return ExcelError::VALUE();

Diff for: src/PhpSpreadsheet/Calculation/TextData/Helpers.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
88
use PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue;
99
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
10+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
1011

1112
class Helpers
1213
{
@@ -31,7 +32,7 @@ public static function extractString(mixed $value, bool $throwIfError = false):
3132
throw new CalcExp($value);
3233
}
3334

34-
return (string) $value;
35+
return StringHelper::convertToString($value);
3536
}
3637

3738
public static function extractInt(mixed $value, int $minValue, int $gnumericNull = 0, bool $ooBoolOk = false): int
@@ -87,6 +88,6 @@ public static function validateInt(mixed $value, bool $throwIfError = false): in
8788
throw new CalcExp($value);
8889
}
8990

90-
return (int) $value;
91+
return (int) StringHelper::convertToString($value);
9192
}
9293
}

Diff for: src/PhpSpreadsheet/Calculation/TextData/Replace.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ private static function executeSubstitution(string $text, string $fromText, stri
111111
--$instance;
112112
}
113113

114-
return Functions::scalar(self::REPLACE($text, ++$pos, StringHelper::countCharacters($fromText), $toText));
114+
return StringHelper::convertToString(Functions::scalar(self::REPLACE($text, ++$pos, StringHelper::countCharacters($fromText), $toText)));
115115
}
116116
}

0 commit comments

Comments
 (0)