Skip to content

Commit 64f354e

Browse files
committed
Better Definitions for Mixed Parameters and Values Part 1 of Many
Improve documentation within program by making explicit what types of values are allowed for variables described as "mixed". In order to avoid broken functionality, this is done mainly through doc-blocks. This will get us closer to Phpstan Level 9, but many changes will be needed before we can consider that. Executable code change is to add 2 new methods to Cell - `getValueString` and `getCalculatedValueString`, which are the same as the methods without the String suffix, except that they always return a string result. These are invoked at several places in the code where it makes sense, in particular by Writer code.
1 parent 35030fa commit 64f354e

File tree

19 files changed

+60
-24
lines changed

19 files changed

+60
-24
lines changed

src/PhpSpreadsheet/Calculation/Engineering/ConvertUOM.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ConvertUOM
2727
/**
2828
* Details of the Units of measure that can be used in CONVERTUOM().
2929
*
30-
* @var mixed[]
30+
* @var array<string, array{Group: string, 'Unit Name': string, AllowPrefix: bool}>
3131
*/
3232
private static array $conversionUnits = [
3333
// Weight and Mass
@@ -201,7 +201,7 @@ class ConvertUOM
201201
/**
202202
* Details of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM().
203203
*
204-
* @var mixed[]
204+
* @var array<string, array{multiplier: float, name: string}>
205205
*/
206206
private static array $conversionMultipliers = [
207207
'Y' => ['multiplier' => 1E24, 'name' => 'yotta'],
@@ -230,7 +230,7 @@ class ConvertUOM
230230
/**
231231
* Details of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM().
232232
*
233-
* @var mixed[]
233+
** @var array<string, array{multiplier: float|int, name: string}>
234234
*/
235235
private static array $binaryConversionMultipliers = [
236236
'Yi' => ['multiplier' => 2 ** 80, 'name' => 'yobi'],
@@ -246,7 +246,7 @@ class ConvertUOM
246246
/**
247247
* Details of the Units of measure conversion factors, organised by group.
248248
*
249-
* @var mixed[]
249+
* @var array<string, array<string, float>>
250250
*/
251251
private static array $unitConversions = [
252252
// Conversion uses gram (g) as an intermediate unit

src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static function calculateArabic(array $roman, int &$sum = 0, int $subtra
5353
* Excel Function:
5454
* ARABIC(text)
5555
*
56-
* @param mixed $roman Should be a string, or can be an array of strings
56+
* @param string|string[] $roman Should be a string, or can be an array of strings
5757
*
5858
* @return array|int|string the arabic numberal contrived from the roman numeral
5959
* If an array of numbers is passed as the argument, then the returned result will also be an array

src/PhpSpreadsheet/Calculation/TextData/Format.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static function DOLLAR(mixed $value = 0, mixed $decimals = 2)
5656
if ($value < 0) {
5757
$round = 0 - $round;
5858
}
59+
/** @var float|int|string */
5960
$value = MathTrig\Round::multiple($value, $round);
6061
}
6162
$mask = "{$mask};-{$mask}";
@@ -129,6 +130,7 @@ public static function TEXTFORMAT(mixed $value, mixed $format): array|string
129130
if (!is_numeric($value) && Date::isDateTimeFormatCode($format)) {
130131
$value1 = DateTimeExcel\DateValue::fromString($value);
131132
$value2 = DateTimeExcel\TimeValue::fromString($value);
133+
/** @var float|int|string */
132134
$value = (is_numeric($value1) && is_numeric($value2)) ? ($value1 + $value2) : (is_numeric($value1) ? $value2 : $value1);
133135
}
134136

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ public function getValue(): mixed
175175
return $this->value;
176176
}
177177

178+
public function getValueString(): string
179+
{
180+
$value = $this->value;
181+
182+
return ($value === '' || is_scalar($value) || $value instanceof Stringable) ? "$value" : '';
183+
}
184+
178185
/**
179186
* Get cell value with formatting.
180187
*/
@@ -336,6 +343,16 @@ private function convertDateTimeInt(mixed $result): mixed
336343
return $result;
337344
}
338345

346+
/**
347+
* Get calculated cell value converted to string.
348+
*/
349+
public function getCalculatedValueString(): string
350+
{
351+
$value = $this->getCalculatedValue();
352+
353+
return ($value === '' || is_scalar($value) || $value instanceof Stringable) ? "$value" : '';
354+
}
355+
339356
/**
340357
* Get calculated cell value.
341358
*

src/PhpSpreadsheet/Shared/Font.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,10 @@ private static function findFontFile(string $startDirectory, string $desiredFont
695695
$it,
696696
RecursiveIteratorIterator::LEAVES_ONLY,
697697
RecursiveIteratorIterator::CATCH_GET_CHILD
698-
) as $file
698+
) as $filex
699699
) {
700+
/** @var string */
701+
$file = $filex;
700702
if (basename($file) === $desiredFont) {
701703
$fontPath = $file;
702704

src/PhpSpreadsheet/Shared/StringHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public static function formatNumber(float|int|string|null $numericValue): string
346346
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3.
347347
*
348348
* @param string $textValue UTF-8 encoded string
349-
* @param mixed[] $arrcRuns Details of rich text runs in $value
349+
* @param array<int, array{strlen: int, fontidx: int}> $arrcRuns Details of rich text runs in $value
350350
*/
351351
public static function UTF8toBIFF8UnicodeShort(string $textValue, array $arrcRuns = []): string
352352
{

src/PhpSpreadsheet/Style/NumberFormat.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Style;
44

5+
use PhpOffice\PhpSpreadsheet\RichText\RichText;
6+
57
class NumberFormat extends Supervisor
68
{
79
// Pre-defined formats
@@ -453,7 +455,7 @@ public function getHashCode(): string
453455
/**
454456
* Convert a value in a pre-defined format to a PHP string.
455457
*
456-
* @param mixed $value Value to format
458+
* @param null|bool|float|int|RichText|string $value Value to format
457459
* @param string $format Format code: see = self::FORMAT_* for predefined values;
458460
* or can be any valid MS Excel custom format string
459461
* @param ?array $callBack Callback function for additional formatting of string

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private static function splitFormatComparison(
4242
};
4343
}
4444

45+
/** @param float|int|string $value value to be formatted */
4546
private static function splitFormatForSectionSelection(array $sections, mixed $value): array
4647
{
4748
// Extract the relevant section depending on whether number is positive, negative, or zero?

src/PhpSpreadsheet/Style/NumberFormat/FractionFormatter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class FractionFormatter extends BaseFormatter
88
{
9+
/** @param null|bool|float|int|string $value value to be formatted */
910
public static function format(mixed $value, string $format): string
1011
{
1112
$format = self::stripQuotes($format);

src/PhpSpreadsheet/Worksheet/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private function updateStructuredReferencesInCells(Worksheet $worksheet, string
178178
foreach ($worksheet->getCoordinates(false) as $coordinate) {
179179
$cell = $worksheet->getCell($coordinate);
180180
if ($cell->getDataType() === DataType::TYPE_FORMULA) {
181-
$formula = $cell->getValue();
181+
$formula = $cell->getValueString();
182182
if (preg_match($pattern, $formula) === 1) {
183183
$formula = preg_replace($pattern, "{$newName}[", $formula);
184184
$cell->setValueExplicit($formula, DataType::TYPE_FORMULA);

0 commit comments

Comments
 (0)