Skip to content

Commit

Permalink
Merge pull request #4016 from oleibman/stan9a
Browse files Browse the repository at this point in the history
Better Definitions for Mixed Parameters and Values Part 1 of Many
  • Loading branch information
oleibman committed May 9, 2024
2 parents c56a583 + 8d5577a commit d38b7cb
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 183 deletions.
316 changes: 158 additions & 158 deletions src/PhpSpreadsheet/Calculation/Engineering/ConvertUOM.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static function calculateArabic(array $roman, int &$sum = 0, int $subtra
* Excel Function:
* ARABIC(text)
*
* @param mixed $roman Should be a string, or can be an array of strings
* @param string|string[] $roman Should be a string, or can be an array of strings
*
* @return array|int|string the arabic numberal contrived from the roman numeral
* If an array of numbers is passed as the argument, then the returned result will also be an array
Expand Down
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Calculation/TextData/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static function DOLLAR(mixed $value = 0, mixed $decimals = 2)
if ($value < 0) {
$round = 0 - $round;
}
/** @var float|int|string */
$value = MathTrig\Round::multiple($value, $round);
}
$mask = "{$mask};-{$mask}";
Expand Down Expand Up @@ -129,6 +130,7 @@ public static function TEXTFORMAT(mixed $value, mixed $format): array|string
if (!is_numeric($value) && Date::isDateTimeFormatCode($format)) {
$value1 = DateTimeExcel\DateValue::fromString($value);
$value2 = DateTimeExcel\TimeValue::fromString($value);
/** @var float|int|string */
$value = (is_numeric($value1) && is_numeric($value2)) ? ($value1 + $value2) : (is_numeric($value1) ? $value2 : $value1);
}

Expand Down
17 changes: 17 additions & 0 deletions src/PhpSpreadsheet/Cell/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ public function getValue(): mixed
return $this->value;
}

public function getValueString(): string
{
$value = $this->value;

return ($value === '' || is_scalar($value) || $value instanceof Stringable) ? "$value" : '';
}

/**
* Get cell value with formatting.
*/
Expand Down Expand Up @@ -336,6 +343,16 @@ private function convertDateTimeInt(mixed $result): mixed
return $result;
}

/**
* Get calculated cell value converted to string.
*/
public function getCalculatedValueString(): string
{
$value = $this->getCalculatedValue();

return ($value === '' || is_scalar($value) || $value instanceof Stringable) ? "$value" : '';
}

/**
* Get calculated cell value.
*
Expand Down
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Shared/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,10 @@ private static function findFontFile(string $startDirectory, string $desiredFont
$it,
RecursiveIteratorIterator::LEAVES_ONLY,
RecursiveIteratorIterator::CATCH_GET_CHILD
) as $file
) as $filex
) {
/** @var string */
$file = $filex;
if (basename($file) === $desiredFont) {
$fontPath = $file;

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Shared/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public static function formatNumber(float|int|string|null $numericValue): string
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3.
*
* @param string $textValue UTF-8 encoded string
* @param mixed[] $arrcRuns Details of rich text runs in $value
* @param array<int, array{strlen: int, fontidx: int}> $arrcRuns Details of rich text runs in $value
*/
public static function UTF8toBIFF8UnicodeShort(string $textValue, array $arrcRuns = []): string
{
Expand Down
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Style/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpOffice\PhpSpreadsheet\Style;

use PhpOffice\PhpSpreadsheet\RichText\RichText;

class NumberFormat extends Supervisor
{
// Pre-defined formats
Expand Down Expand Up @@ -453,7 +455,7 @@ public function getHashCode(): string
/**
* Convert a value in a pre-defined format to a PHP string.
*
* @param mixed $value Value to format
* @param null|bool|float|int|RichText|string $value Value to format
* @param string $format Format code: see = self::FORMAT_* for predefined values;
* or can be any valid MS Excel custom format string
* @param ?array $callBack Callback function for additional formatting of string
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private static function splitFormatComparison(
};
}

/** @param float|int|string $value value to be formatted */
private static function splitFormatForSectionSelection(array $sections, mixed $value): array
{
// Extract the relevant section depending on whether number is positive, negative, or zero?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class FractionFormatter extends BaseFormatter
{
/** @param null|bool|float|int|string $value value to be formatted */
public static function format(mixed $value, string $format): string
{
$format = self::stripQuotes($format);
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Worksheet/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private function updateStructuredReferencesInCells(Worksheet $worksheet, string
foreach ($worksheet->getCoordinates(false) as $coordinate) {
$cell = $worksheet->getCell($coordinate);
if ($cell->getDataType() === DataType::TYPE_FORMULA) {
$formula = $cell->getValue();
$formula = $cell->getValueString();
if (preg_match($pattern, $formula) === 1) {
$formula = preg_replace($pattern, "{$newName}[", $formula);
$cell->setValueExplicit($formula, DataType::TYPE_FORMULA);
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Worksheet/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private static function updateStructuredReferencesInCells(Worksheet $worksheet,
foreach ($worksheet->getCoordinates(false) as $coordinate) {
$cell = $worksheet->getCell($coordinate);
if ($cell->getDataType() === DataType::TYPE_FORMULA) {
$formula = $cell->getValue();
$formula = $cell->getValueString();
if (preg_match($pattern, $formula) === 1) {
$formula = preg_replace($pattern, "[$1{$newTitle}]", $formula);
$cell->setValueExplicit($formula, DataType::TYPE_FORMULA);
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2862,7 +2862,7 @@ public function rangeToArray(
* @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden.
* True - Don't return values for rows/columns that are defined as hidden.
*
* @return Generator
* @return Generator<array>
*/
public function rangeToArrayYieldRows(
string $range,
Expand Down
4 changes: 4 additions & 0 deletions src/PhpSpreadsheet/Writer/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Stringable;

class Csv extends BaseWriter
{
Expand Down Expand Up @@ -246,6 +247,8 @@ public function getEnclosureRequired(): bool

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

/** @var null|bool|float|int|string|Stringable $element */
foreach ($values as $element) {
$element = self::elementToString($element);
// Add delimiter
Expand Down
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Writer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1310,10 +1310,11 @@ private function generateRowCellDataValue(Worksheet $worksheet, Cell $cell, stri
$cellData .= $this->generateRowCellDataValueRich($cell->getValue());
} else {
$origData = $this->preCalculateFormulas ? $cell->getCalculatedValue() : $cell->getValue();
$origData2 = $this->preCalculateFormulas ? $cell->getCalculatedValueString() : $cell->getValueString();
$formatCode = $worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode();

$cellData = NumberFormat::toFormattedString(
$origData ?? '',
$origData2,
$formatCode ?? NumberFormat::FORMAT_GENERAL,
[$this, 'formatColor']
);
Expand Down
18 changes: 9 additions & 9 deletions src/PhpSpreadsheet/Writer/Ods/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
switch ($cell->getDataType()) {
case DataType::TYPE_BOOL:
$objWriter->writeAttribute('office:value-type', 'boolean');
$objWriter->writeAttribute('office:value', $cell->getValue());
$objWriter->writeElement('text:p', $cell->getValue());
$objWriter->writeAttribute('office:value', $cell->getValueString());
$objWriter->writeElement('text:p', $cell->getValueString());

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

break;
case DataType::TYPE_FORMULA:
$formulaValue = $cell->getValue();
$formulaValue = $cell->getValueString();
if ($this->getParentWriter()->getPreCalculateFormulas()) {
try {
$formulaValue = $cell->getCalculatedValue();
$formulaValue = $cell->getCalculatedValueString();
} catch (CalculationException $e) {
// don't do anything
}
}
$objWriter->writeAttribute('table:formula', $this->formulaConvertor->convertFormula($cell->getValue()));
$objWriter->writeAttribute('table:formula', $this->formulaConvertor->convertFormula($cell->getValueString()));
if (is_numeric($formulaValue)) {
$objWriter->writeAttribute('office:value-type', 'float');
} else {
Expand All @@ -241,8 +241,8 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
break;
case DataType::TYPE_NUMERIC:
$objWriter->writeAttribute('office:value-type', 'float');
$objWriter->writeAttribute('office:value', $cell->getValue());
$objWriter->writeElement('text:p', $cell->getValue());
$objWriter->writeAttribute('office:value', $cell->getValueString());
$objWriter->writeElement('text:p', $cell->getValueString());

break;
case DataType::TYPE_INLINE:
Expand All @@ -251,7 +251,7 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
$objWriter->writeAttribute('office:value-type', 'string');
$url = $cell->getHyperlink()->getUrl();
if (empty($url)) {
$objWriter->writeElement('text:p', $cell->getValue());
$objWriter->writeElement('text:p', $cell->getValueString());
} else {
$objWriter->startElement('text:p');
$objWriter->startElement('text:a');
Expand All @@ -262,7 +262,7 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
}
$objWriter->writeAttribute('xlink:href', $url);
$objWriter->writeAttribute('xlink:type', 'simple');
$objWriter->text($cell->getValue());
$objWriter->text($cell->getValueString());
$objWriter->endElement(); // text:a
$objWriter->endElement(); // text:p
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Writer/Xlsx/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function writeTable(WorksheetTable $table, int $tableRef): string

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

if ($table->getShowTotalsRow()) {
if ($column->getTotalsRowLabel()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,37 @@ class ConvertUoMTest extends TestCase
public function testGetConversionGroups(): void
{
$result = ConvertUOM::getConversionCategories();
self::assertIsArray($result);
self::assertContains('Weight and Mass', $result);
}

public function testGetConversionGroupUnits(): void
{
$result = ConvertUOM::getConversionCategoryUnits();
self::assertIsArray($result);
self::assertArrayHasKey('Speed', $result);
self::assertIsArray($result['Speed']);
self::assertContains('mph', $result['Speed']);
}

public function testGetConversionGroupUnitDetails(): void
{
$result = ConvertUOM::getConversionCategoryUnitDetails();
self::assertIsArray($result);
self::assertArrayHasKey('Information', $result);
self::assertIsArray($result['Information']);
self::assertContains(['unit' => 'byte', 'description' => 'Byte'], $result['Information']);
}

public function testGetConversionMultipliers(): void
{
$result = ConvertUOM::getConversionMultipliers();
self::assertIsArray($result);
self::assertArrayHasKey('k', $result);
self::assertSame(['multiplier' => 1000.0, 'name' => 'kilo'], $result['k']);
}

public function testGetBinaryConversionMultipliers(): void
{
$result = ConvertUOM::getBinaryConversionMultipliers();
self::assertIsArray($result);
self::assertArrayHasKey('ki', $result);
self::assertSame(['multiplier' => 1024, 'name' => 'kibi'], $result['ki']);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpSpreadsheetTests/Cell/CellAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testCreateFromColumnAndRow(
/**
* @dataProvider providerCreateFromColumnRowException
*/
public function testCreateFromColumnRowException(mixed $columnId, mixed $rowId): void
public function testCreateFromColumnRowException(int|string $columnId, int|string $rowId): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Row and Column Ids must be positive integer values');
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpSpreadsheetTests/Document/PropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function testSetManager(): void
/**
* @dataProvider providerCustomProperties
*/
public function testSetCustomProperties(mixed $expectedType, mixed $expectedValue, string $propertyName, mixed $propertyValue, ?string $propertyType = null): void
public function testSetCustomProperties(mixed $expectedType, mixed $expectedValue, string $propertyName, null|bool|float|int|string $propertyValue, ?string $propertyType = null): void
{
if ($propertyType === null) {
$this->properties->setCustomProperty($propertyName, $propertyValue);
Expand Down
6 changes: 6 additions & 0 deletions tests/PhpSpreadsheetTests/Style/NumberFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ protected function tearDown(): void

/**
* @dataProvider providerNumberFormat
*
* @param null|bool|float|int|string $args string to be formatted
*/
public function testFormatValueWithMask(mixed $expectedResult, mixed ...$args): void
{
Expand All @@ -40,6 +42,8 @@ public static function providerNumberFormat(): array

/**
* @dataProvider providerNumberFormatFractions
*
* @param null|bool|float|int|string $args string to be formatted
*/
public function testFormatValueWithMaskFraction(mixed $expectedResult, mixed ...$args): void
{
Expand All @@ -54,6 +58,8 @@ public static function providerNumberFormatFractions(): array

/**
* @dataProvider providerNumberFormatDates
*
* @param null|bool|float|int|string $args string to be formatted
*/
public function testFormatValueWithMaskDate(mixed $expectedResult, mixed ...$args): void
{
Expand Down

0 comments on commit d38b7cb

Please sign in to comment.