Skip to content

Commit d38b7cb

Browse files
authored
Merge pull request #4016 from oleibman/stan9a
Better Definitions for Mixed Parameters and Values Part 1 of Many
2 parents c56a583 + 8d5577a commit d38b7cb

File tree

20 files changed

+225
-183
lines changed

20 files changed

+225
-183
lines changed

src/PhpSpreadsheet/Calculation/Engineering/ConvertUOM.php

+158-158
Large diffs are not rendered by default.

src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php

+1-1
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

+2
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

+17
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

+3-1
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

+1-1
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

+3-1
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

+1
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

+1
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

+1-1
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);

src/PhpSpreadsheet/Worksheet/Table/Column.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private static function updateStructuredReferencesInCells(Worksheet $worksheet,
216216
foreach ($worksheet->getCoordinates(false) as $coordinate) {
217217
$cell = $worksheet->getCell($coordinate);
218218
if ($cell->getDataType() === DataType::TYPE_FORMULA) {
219-
$formula = $cell->getValue();
219+
$formula = $cell->getValueString();
220220
if (preg_match($pattern, $formula) === 1) {
221221
$formula = preg_replace($pattern, "[$1{$newTitle}]", $formula);
222222
$cell->setValueExplicit($formula, DataType::TYPE_FORMULA);

src/PhpSpreadsheet/Worksheet/Worksheet.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2862,7 +2862,7 @@ public function rangeToArray(
28622862
* @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden.
28632863
* True - Don't return values for rows/columns that are defined as hidden.
28642864
*
2865-
* @return Generator
2865+
* @return Generator<array>
28662866
*/
28672867
public function rangeToArrayYieldRows(
28682868
string $range,

src/PhpSpreadsheet/Writer/Csv.php

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7+
use Stringable;
78

89
class Csv extends BaseWriter
910
{
@@ -246,6 +247,8 @@ public function getEnclosureRequired(): bool
246247

247248
/**
248249
* Convert boolean to TRUE/FALSE; otherwise return element cast to string.
250+
*
251+
* @param null|bool|float|int|string|Stringable $element element to be converted
249252
*/
250253
private static function elementToString(mixed $element): string
251254
{
@@ -270,6 +273,7 @@ private function writeLine($fileHandle, array $values): void
270273
// Build the line
271274
$line = '';
272275

276+
/** @var null|bool|float|int|string|Stringable $element */
273277
foreach ($values as $element) {
274278
$element = self::elementToString($element);
275279
// Add delimiter

src/PhpSpreadsheet/Writer/Html.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1310,10 +1310,11 @@ private function generateRowCellDataValue(Worksheet $worksheet, Cell $cell, stri
13101310
$cellData .= $this->generateRowCellDataValueRich($cell->getValue());
13111311
} else {
13121312
$origData = $this->preCalculateFormulas ? $cell->getCalculatedValue() : $cell->getValue();
1313+
$origData2 = $this->preCalculateFormulas ? $cell->getCalculatedValueString() : $cell->getValueString();
13131314
$formatCode = $worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode();
13141315

13151316
$cellData = NumberFormat::toFormattedString(
1316-
$origData ?? '',
1317+
$origData2,
13171318
$formatCode ?? NumberFormat::FORMAT_GENERAL,
13181319
[$this, 'formatColor']
13191320
);

src/PhpSpreadsheet/Writer/Ods/Content.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
209209
switch ($cell->getDataType()) {
210210
case DataType::TYPE_BOOL:
211211
$objWriter->writeAttribute('office:value-type', 'boolean');
212-
$objWriter->writeAttribute('office:value', $cell->getValue());
213-
$objWriter->writeElement('text:p', $cell->getValue());
212+
$objWriter->writeAttribute('office:value', $cell->getValueString());
213+
$objWriter->writeElement('text:p', $cell->getValueString());
214214

215215
break;
216216
case DataType::TYPE_ERROR:
@@ -221,15 +221,15 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
221221

222222
break;
223223
case DataType::TYPE_FORMULA:
224-
$formulaValue = $cell->getValue();
224+
$formulaValue = $cell->getValueString();
225225
if ($this->getParentWriter()->getPreCalculateFormulas()) {
226226
try {
227-
$formulaValue = $cell->getCalculatedValue();
227+
$formulaValue = $cell->getCalculatedValueString();
228228
} catch (CalculationException $e) {
229229
// don't do anything
230230
}
231231
}
232-
$objWriter->writeAttribute('table:formula', $this->formulaConvertor->convertFormula($cell->getValue()));
232+
$objWriter->writeAttribute('table:formula', $this->formulaConvertor->convertFormula($cell->getValueString()));
233233
if (is_numeric($formulaValue)) {
234234
$objWriter->writeAttribute('office:value-type', 'float');
235235
} else {
@@ -241,8 +241,8 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
241241
break;
242242
case DataType::TYPE_NUMERIC:
243243
$objWriter->writeAttribute('office:value-type', 'float');
244-
$objWriter->writeAttribute('office:value', $cell->getValue());
245-
$objWriter->writeElement('text:p', $cell->getValue());
244+
$objWriter->writeAttribute('office:value', $cell->getValueString());
245+
$objWriter->writeElement('text:p', $cell->getValueString());
246246

247247
break;
248248
case DataType::TYPE_INLINE:
@@ -251,7 +251,7 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
251251
$objWriter->writeAttribute('office:value-type', 'string');
252252
$url = $cell->getHyperlink()->getUrl();
253253
if (empty($url)) {
254-
$objWriter->writeElement('text:p', $cell->getValue());
254+
$objWriter->writeElement('text:p', $cell->getValueString());
255255
} else {
256256
$objWriter->startElement('text:p');
257257
$objWriter->startElement('text:a');
@@ -262,7 +262,7 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
262262
}
263263
$objWriter->writeAttribute('xlink:href', $url);
264264
$objWriter->writeAttribute('xlink:type', 'simple');
265-
$objWriter->text($cell->getValue());
265+
$objWriter->text($cell->getValueString());
266266
$objWriter->endElement(); // text:a
267267
$objWriter->endElement(); // text:p
268268
}

src/PhpSpreadsheet/Writer/Xlsx/Table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function writeTable(WorksheetTable $table, int $tableRef): string
8080

8181
$objWriter->startElement('tableColumn');
8282
$objWriter->writeAttribute('id', (string) ($offset + 1));
83-
$objWriter->writeAttribute('name', $table->getShowHeaderRow() ? $cell->getValue() : 'Column' . ($offset + 1));
83+
$objWriter->writeAttribute('name', $table->getShowHeaderRow() ? $cell->getValueString() : ('Column' . ($offset + 1)));
8484

8585
if ($table->getShowTotalsRow()) {
8686
if ($column->getTotalsRowLabel()) {

tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/ConvertUoMTest.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,37 @@ class ConvertUoMTest extends TestCase
1818
public function testGetConversionGroups(): void
1919
{
2020
$result = ConvertUOM::getConversionCategories();
21-
self::assertIsArray($result);
21+
self::assertContains('Weight and Mass', $result);
2222
}
2323

2424
public function testGetConversionGroupUnits(): void
2525
{
2626
$result = ConvertUOM::getConversionCategoryUnits();
27-
self::assertIsArray($result);
27+
self::assertArrayHasKey('Speed', $result);
28+
self::assertIsArray($result['Speed']);
29+
self::assertContains('mph', $result['Speed']);
2830
}
2931

3032
public function testGetConversionGroupUnitDetails(): void
3133
{
3234
$result = ConvertUOM::getConversionCategoryUnitDetails();
33-
self::assertIsArray($result);
35+
self::assertArrayHasKey('Information', $result);
36+
self::assertIsArray($result['Information']);
37+
self::assertContains(['unit' => 'byte', 'description' => 'Byte'], $result['Information']);
3438
}
3539

3640
public function testGetConversionMultipliers(): void
3741
{
3842
$result = ConvertUOM::getConversionMultipliers();
39-
self::assertIsArray($result);
43+
self::assertArrayHasKey('k', $result);
44+
self::assertSame(['multiplier' => 1000.0, 'name' => 'kilo'], $result['k']);
4045
}
4146

4247
public function testGetBinaryConversionMultipliers(): void
4348
{
4449
$result = ConvertUOM::getBinaryConversionMultipliers();
45-
self::assertIsArray($result);
50+
self::assertArrayHasKey('ki', $result);
51+
self::assertSame(['multiplier' => 1024, 'name' => 'kibi'], $result['ki']);
4652
}
4753

4854
/**

tests/PhpSpreadsheetTests/Cell/CellAddressTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testCreateFromColumnAndRow(
8484
/**
8585
* @dataProvider providerCreateFromColumnRowException
8686
*/
87-
public function testCreateFromColumnRowException(mixed $columnId, mixed $rowId): void
87+
public function testCreateFromColumnRowException(int|string $columnId, int|string $rowId): void
8888
{
8989
$this->expectException(Exception::class);
9090
$this->expectExceptionMessage('Row and Column Ids must be positive integer values');

tests/PhpSpreadsheetTests/Document/PropertiesTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function testSetManager(): void
153153
/**
154154
* @dataProvider providerCustomProperties
155155
*/
156-
public function testSetCustomProperties(mixed $expectedType, mixed $expectedValue, string $propertyName, mixed $propertyValue, ?string $propertyType = null): void
156+
public function testSetCustomProperties(mixed $expectedType, mixed $expectedValue, string $propertyName, null|bool|float|int|string $propertyValue, ?string $propertyType = null): void
157157
{
158158
if ($propertyType === null) {
159159
$this->properties->setCustomProperty($propertyName, $propertyValue);

tests/PhpSpreadsheetTests/Style/NumberFormatTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ protected function tearDown(): void
2626

2727
/**
2828
* @dataProvider providerNumberFormat
29+
*
30+
* @param null|bool|float|int|string $args string to be formatted
2931
*/
3032
public function testFormatValueWithMask(mixed $expectedResult, mixed ...$args): void
3133
{
@@ -40,6 +42,8 @@ public static function providerNumberFormat(): array
4042

4143
/**
4244
* @dataProvider providerNumberFormatFractions
45+
*
46+
* @param null|bool|float|int|string $args string to be formatted
4347
*/
4448
public function testFormatValueWithMaskFraction(mixed $expectedResult, mixed ...$args): void
4549
{
@@ -54,6 +58,8 @@ public static function providerNumberFormatFractions(): array
5458

5559
/**
5660
* @dataProvider providerNumberFormatDates
61+
*
62+
* @param null|bool|float|int|string $args string to be formatted
5763
*/
5864
public function testFormatValueWithMaskDate(mixed $expectedResult, mixed ...$args): void
5965
{

0 commit comments

Comments
 (0)