Skip to content

Phpstan Level 9 - Part 8 of Many - TextData #4428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 0 additions & 114 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -419,117 +419,3 @@ parameters:
identifier: argument.type
count: 9
path: src/PhpSpreadsheet/Calculation/Statistical/Trends.php

-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\CharacterConvert\:\:unicodeToOrd\(\) should return int but returns mixed\.$#'
identifier: return.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php

-
message: '#^Binary operation "\." between mixed and mixed results in an error\.$#'
identifier: binaryOp.invalid
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php

-
message: '#^Cannot cast mixed to string\.$#'
identifier: cast.string
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php

-
message: '#^Parameter \#1 \$ignoreEmpty of static method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\Concatenate\:\:evaluateTextJoinArray\(\) expects bool, mixed given\.$#'
identifier: argument.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php

-
message: '#^Parameter \#1 \$separator of function implode expects array\|string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Concatenate.php

-
message: '#^Cannot cast mixed to int\.$#'
identifier: cast.int
count: 6
path: src/PhpSpreadsheet/Calculation/TextData/Extract.php

-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\Extract\:\:validateTextBeforeAfter\(\) should return array\|string but returns mixed\.$#'
identifier: return.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Extract.php

-
message: '#^Cannot cast mixed to string\.$#'
identifier: cast.string
count: 3
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\Format\:\:VALUE\(\) should return array\|DateTimeInterface\|float\|int\|string but returns mixed\.$#'
identifier: return.type
count: 2
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: '#^Parameter \#1 \$dateValue of static method PhpOffice\\PhpSpreadsheet\\Calculation\\DateTimeExcel\\DateValue\:\:fromString\(\) expects array\|bool\|float\|int\|string\|null, mixed given\.$#'
identifier: argument.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: '#^Parameter \#1 \$haystack of function str_contains expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: '#^Parameter \#1 \$haystack of function strpos expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: '#^Parameter \#1 \$string of function trim expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: '#^Parameter \#1 \$timeValue of static method PhpOffice\\PhpSpreadsheet\\Calculation\\DateTimeExcel\\TimeValue\:\:fromString\(\) expects array\|bool\|float\|int\|string\|null, mixed given\.$#'
identifier: argument.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: '#^Parameter \#2 \$subject of static method Composer\\Pcre\\Preg\:\:matchAllWithOffsets\(\) expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: '#^Parameter \#3 \$subject of function str_replace expects array\<string\>\|string, mixed given\.$#'
identifier: argument.type
count: 2
path: src/PhpSpreadsheet/Calculation/TextData/Format.php

-
message: '#^Cannot cast mixed to int\.$#'
identifier: cast.int
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Helpers.php

-
message: '#^Cannot cast mixed to string\.$#'
identifier: cast.string
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Helpers.php

-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\TextData\\Replace\:\:executeSubstitution\(\) should return string but returns mixed\.$#'
identifier: return.type
count: 1
path: src/PhpSpreadsheet/Calculation/TextData/Replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private static function unicodeToOrd(string $character): int
$retVal = 0;
$iconv = iconv('UTF-8', 'UCS-4LE', $character);
if ($iconv !== false) {
/** @var false|int[] */
$result = unpack('V', $iconv);
if (is_array($result) && isset($result[1])) {
$retVal = $result[1];
Expand Down
13 changes: 7 additions & 6 deletions src/PhpSpreadsheet/Calculation/TextData/Concatenate.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ private static function concatenate2Args(array|string $operand1, null|array|bool
return $operand2[$row][$column];
}
$operand1[$row][$column]
= Calculation::boolToString($operand1[$row][$column])
. Calculation::boolToString($operand2[$row][$column]);
= StringHelper::convertToString($operand1[$row][$column], convertBool: true)
. StringHelper::convertToString($operand2[$row][$column], convertBool: true);
if (mb_strlen($operand1[$row][$column]) > DataType::MAX_STRING_LENGTH) {
$operand1 = ExcelError::CALC();
$errorFound = true;
Expand All @@ -91,7 +91,7 @@ private static function concatenate2Args(array|string $operand1, null|array|bool
} elseif (ErrorValue::isError($operand2, true) === true) {
$operand1 = (string) $operand2;
} else {
$operand1 .= (string) Calculation::boolToString($operand2);
$operand1 .= StringHelper::convertToString($operand2, convertBool: true);
if (mb_strlen($operand1) > DataType::MAX_STRING_LENGTH) {
$operand1 = ExcelError::CALC();
}
Expand All @@ -103,17 +103,17 @@ private static function concatenate2Args(array|string $operand1, null|array|bool
/**
* TEXTJOIN.
*
* @param mixed $delimiter The delimter to use between the joined arguments
* @param null|string|string[] $delimiter The delimiter to use between the joined arguments
* Or can be an array of values
* @param mixed $ignoreEmpty true/false Flag indicating whether empty arguments should be skipped
* @param null|bool|bool[] $ignoreEmpty true/false Flag indicating whether empty arguments should be skipped
* Or can be an array of values
* @param mixed $args The values to join
*
* @return array|string The joined string
* If an array of values is passed for the $delimiter or $ignoreEmpty arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function TEXTJOIN(mixed $delimiter = '', mixed $ignoreEmpty = true, mixed ...$args): array|string
public static function TEXTJOIN($delimiter = '', $ignoreEmpty = true, mixed ...$args): array|string
{
if (is_array($delimiter) || is_array($ignoreEmpty)) {
return self::evaluateArrayArgumentsSubset(
Expand All @@ -127,6 +127,7 @@ public static function TEXTJOIN(mixed $delimiter = '', mixed $ignoreEmpty = true

$delimiter ??= '';
$ignoreEmpty ??= true;
/** @var array */
$aArgs = Functions::flattenArray($args);
$returnValue = self::evaluateTextJoinArray($ignoreEmpty, $aArgs);

Expand Down
14 changes: 7 additions & 7 deletions src/PhpSpreadsheet/Calculation/TextData/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public static function before(mixed $text, $delimiter, mixed $instance = 1, mixe
return $e->getMessage();
}

$instance = (int) $instance;
$matchMode = (int) $matchMode;
$matchEnd = (int) $matchEnd;
$instance = (int) StringHelper::convertToString($instance);
$matchMode = (int) StringHelper::convertToString($matchMode);
$matchEnd = (int) StringHelper::convertToString($matchEnd);

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

$instance = (int) $instance;
$matchMode = (int) $matchMode;
$matchEnd = (int) $matchEnd;
$instance = (int) StringHelper::convertToString($instance);
$matchMode = (int) StringHelper::convertToString($matchMode);
$matchEnd = (int) StringHelper::convertToString($matchEnd);

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

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

$split = preg_split('/' . $delimiter . "/{$flags}", $text, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
Expand Down
17 changes: 10 additions & 7 deletions src/PhpSpreadsheet/Calculation/TextData/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public static function VALUE(mixed $value = '')
return $e->getMessage();
}
if (!is_numeric($value)) {
$value = StringHelper::convertToString($value);
$numberValue = str_replace(
StringHelper::getThousandsSeparator(),
'',
Expand All @@ -212,14 +213,14 @@ public static function VALUE(mixed $value = '')
if ($timeValue !== ExcelError::VALUE()) {
Functions::setReturnDateType($dateSetting);

return $timeValue;
return $timeValue; //* @phpstan-ignore-line
}
}
$dateValue = Functions::scalar(DateTimeExcel\DateValue::fromString($value));
if ($dateValue !== ExcelError::VALUE()) {
Functions::setReturnDateType($dateSetting);

return $dateValue;
return $dateValue; //* @phpstan-ignore-line
}
Functions::setReturnDateType($dateSetting);

Expand Down Expand Up @@ -250,23 +251,23 @@ public static function valueToText(mixed $value, mixed $format = false): array|s
$value = $value->getPlainText();
}
if (is_string($value)) {
$value = ($format === true) ? Calculation::wrapResult($value) : $value;
$value = ($format === true) ? StringHelper::convertToString(Calculation::wrapResult($value)) : $value;
$value = str_replace("\n", '', $value);
} elseif (is_bool($value)) {
$value = Calculation::getLocaleBoolean($value ? 'TRUE' : 'FALSE');
}

return (string) $value;
return StringHelper::convertToString($value);
}

private static function getDecimalSeparator(mixed $decimalSeparator): string
{
return empty($decimalSeparator) ? StringHelper::getDecimalSeparator() : (string) $decimalSeparator;
return empty($decimalSeparator) ? StringHelper::getDecimalSeparator() : StringHelper::convertToString($decimalSeparator);
}

private static function getGroupSeparator(mixed $groupSeparator): string
{
return empty($groupSeparator) ? StringHelper::getThousandsSeparator() : (string) $groupSeparator;
return empty($groupSeparator) ? StringHelper::getThousandsSeparator() : StringHelper::convertToString($groupSeparator);
}

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

if (!is_numeric($value)) {
/** @var null|array|scalar $value */
if (!is_array($value) && !is_numeric($value)) {
$value = StringHelper::convertToString($value);
$decimalPositions = Preg::matchAllWithOffsets('/' . preg_quote($decimalSeparator, '/') . '/', $value, $matches);
if ($decimalPositions > 1) {
return ExcelError::VALUE();
Expand Down
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/Calculation/TextData/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;

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

return (string) $value;
return StringHelper::convertToString($value);
}

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

return (int) $value;
return (int) StringHelper::convertToString($value);
}
}
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/TextData/Replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ private static function executeSubstitution(string $text, string $fromText, stri
--$instance;
}

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