Skip to content

Commit

Permalink
Eliminate Some "Mixed" Variables in Samples
Browse files Browse the repository at this point in the history
A continuation of PR #3859. Change code that would be flagged if we were to run Phpstan at level 9 (we currently run level 8). I may or may not follow up with source code (454 level-9 problems remain for src), but there is no reason to avoid the effort for samples.

No changes are made to src.
  • Loading branch information
oleibman committed Jan 21, 2024
1 parent ae72efe commit 641f86d
Show file tree
Hide file tree
Showing 87 changed files with 654 additions and 507 deletions.
24 changes: 24 additions & 0 deletions infra/LocaleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ public function generateLocales(): void
protected function buildConfigFileForLocale(string $column, string $locale): void
{
$language = $this->localeTranslations->getCell($column . self::ENGLISH_LANGUAGE_NAME_ROW)->getValue();
if (!is_string($language)) {
throw new Exception('Non-string language value at ' . $column . self::ENGLISH_LANGUAGE_NAME_ROW);
}
$localeLanguage = $this->localeTranslations->getCell($column . self::LOCALE_LANGUAGE_NAME_ROW)->getValue();
if (!is_string($localeLanguage)) {
throw new Exception('Non-string locale language value at ' . $column . self::LOCALE_LANGUAGE_NAME_ROW);
}
$configFile = $this->openConfigFile($locale, $language, $localeLanguage);

$this->writeConfigArgumentSeparator($configFile, $column);
Expand All @@ -96,6 +102,9 @@ protected function buildConfigFileForLocale(string $column, string $locale): voi
foreach ($this->errorCodeMap as $errorCode => $row) {
$translationCell = $this->localeTranslations->getCell($column . $row);
$translationValue = $translationCell->getValue();
if ($translationValue !== null && !is_string($translationValue)) {
throw new Exception('Non-string translation value at ' . $column . $row);
}
if (!empty($translationValue)) {
$errorCodeTranslation = "{$errorCode} = {$translationValue}" . self::EOL;
fwrite($configFile, $errorCodeTranslation);
Expand All @@ -114,6 +123,9 @@ protected function writeConfigArgumentSeparator($configFile, string $column): vo
{
$translationCell = $this->localeTranslations->getCell($column . self::ARGUMENT_SEPARATOR_ROW);
$localeValue = $translationCell->getValue();
if ($localeValue !== null && !is_string($localeValue)) {
throw new Exception('Non-string locale value at ' . $column . self::CURRENCY_SYMBOL_ROW);
}
if (!empty($localeValue)) {
$functionTranslation = "ArgumentSeparator = {$localeValue}" . self::EOL;
fwrite($configFile, $functionTranslation);
Expand All @@ -127,6 +139,9 @@ protected function writeConfigCurrencySymbol($configFile, string $column): void
{
$translationCell = $this->localeTranslations->getCell($column . self::CURRENCY_SYMBOL_ROW);
$localeValue = $translationCell->getValue();
if ($localeValue !== null && !is_string($localeValue)) {
throw new Exception('Non-string locale value at ' . $column . self::CURRENCY_SYMBOL_ROW);
}
if (!empty($localeValue)) {
$functionTranslation = "currencySymbol = {$localeValue}" . self::EOL;
fwrite($configFile, '##' . self::EOL);
Expand All @@ -141,13 +156,22 @@ protected function writeConfigCurrencySymbol($configFile, string $column): void
protected function buildFunctionsFileForLocale(string $column, string $locale): void
{
$language = $this->functionNameTranslations->getCell($column . self::ENGLISH_LANGUAGE_NAME_ROW)->getValue();
if (!is_string($language)) {
throw new Exception('Non-string language value at ' . $column . self::ENGLISH_LANGUAGE_NAME_ROW);
}
$localeLanguage = $this->functionNameTranslations->getCell($column . self::LOCALE_LANGUAGE_NAME_ROW)
->getValue();
if (!is_string($localeLanguage)) {
throw new Exception('Non-string locale language value at ' . $column . self::LOCALE_LANGUAGE_NAME_ROW);
}
$functionFile = $this->openFunctionNameFile($locale, $language, $localeLanguage);

foreach ($this->functionNameMap as $functionName => $row) {
$translationCell = $this->functionNameTranslations->getCell($column . $row);
$translationValue = $translationCell->getValue();
if ($translationValue !== null && !is_string($translationValue)) {
throw new Exception('Non-string translation value at ' . $column . $row);
}
if ($this->isFunctionCategoryEntry($translationCell)) {
$this->writeFileSectionHeader($functionFile, "{$translationValue} ({$functionName})");
} elseif (!array_key_exists($functionName, $this->phpSpreadsheetFunctions) && substr($functionName, 0, 1) !== '*') {
Expand Down
3 changes: 2 additions & 1 deletion samples/Basic/13_Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@
$helper->log('Calculated data');
for ($col = 'B'; $col != 'G'; ++$col) {
for ($row = 14; $row <= 41; ++$row) {
$formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue();
if (
(($formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue()) !== null)
is_string($formula)
&& ($formula[0] == '=')
) {
$helper->log('Value of ' . $col . $row . ' [' . $formula . ']: ' . $spreadsheet->getActiveSheet()->getCell($col . $row)->getCalculatedValue());
Expand Down
3 changes: 2 additions & 1 deletion samples/Basic/13_CalculationCyclicFormulae.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
$helper->log('Calculated data');
for ($row = 1; $row <= 2; ++$row) {
for ($col = 'A'; $col != 'C'; ++$col) {
$formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue();
if (
(($formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue()) !== null)
is_string($formula)
&& ($formula[0] == '=')
) {
$helper->log('Value of ' . $col . $row . ' [' . $formula . ']: ' . $spreadsheet->getActiveSheet()->getCell($col . $row)->getCalculatedValue());
Expand Down
2 changes: 1 addition & 1 deletion samples/Basic/31_Document_properties_write.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
$propertyValue = $spreadsheet->getProperties()->getCustomPropertyValue($customProperty);
$propertyType = $spreadsheet->getProperties()->getCustomPropertyType($customProperty);
if ($propertyType == Properties::PROPERTY_TYPE_DATE) {
$formattedValue = date('d-M-Y H:i:s', (int) $propertyValue);
$formattedValue = is_numeric($propertyValue) ? date('d-M-Y H:i:s', (int) $propertyValue) : '*****INVALID*****';
} elseif ($propertyType == Properties::PROPERTY_TYPE_BOOLEAN) {
$formattedValue = $propertyValue ? 'TRUE' : 'FALSE';
} else {
Expand Down
2 changes: 1 addition & 1 deletion samples/Basic/31_Document_properties_write_xls.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
$propertyValue = $spreadsheet->getProperties()->getCustomPropertyValue($customProperty);
$propertyType = $spreadsheet->getProperties()->getCustomPropertyType($customProperty);
if ($propertyType == Properties::PROPERTY_TYPE_DATE) {
$formattedValue = date('d-M-Y H:i:s', (int) $propertyValue);
$formattedValue = is_numeric($propertyValue) ? date('d-M-Y H:i:s', (int) $propertyValue) : '*****INVALID*****';
} elseif ($propertyType == Properties::PROPERTY_TYPE_BOOLEAN) {
$formattedValue = $propertyValue ? 'TRUE' : 'FALSE';
} else {
Expand Down
8 changes: 6 additions & 2 deletions samples/Basic/45_Quadratic_equation_solver.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@
$r1Formula = '=IMDIV(IMSUM(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . ')';
$r2Formula = '=IF(' . $discriminant . '=0,"Only one root",IMDIV(IMSUB(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . '))';

$helper->log(Calculation::getInstance()->calculateFormula($r1Formula));
$helper->log(Calculation::getInstance()->calculateFormula($r2Formula));
/** @var string */
$output = Calculation::getInstance()->calculateFormula($r1Formula);
$helper->log("$output");
/** @var string */
$output = Calculation::getInstance()->calculateFormula($r2Formula);
$helper->log("$output");
$callEndTime = microtime(true);
$helper->logEndingNotes();
}
Expand Down
12 changes: 7 additions & 5 deletions samples/Calculations/DateTime/DAYS360.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@
$worksheet->getCell('E' . $row)->getFormattedValue(),
$worksheet->getCell('F' . $row)->getFormattedValue()
));
$helper->log(sprintf(
'Days: %d (US) %d (European)',
$worksheet->getCell('G' . $row)->getCalculatedValue(),
$worksheet->getCell('H' . $row)->getCalculatedValue()
));
$helper->log(
'Days: '
. $worksheet->getCell('G' . $row)->getCalculatedValue()
. ' (US) '
. $worksheet->getCell('H' . $row)->getCalculatedValue()
. ' (European)'
);
}
19 changes: 12 additions & 7 deletions samples/Calculations/DateTime/EDATE.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@

// Test the formulae
for ($row = 1; $row <= $testDateCount; ++$row) {
$helper->log(sprintf(
'%s and %d months is %d (%s)',
$worksheet->getCell('B' . $row)->getFormattedValue(),
$worksheet->getCell('C' . $row)->getFormattedValue(),
$worksheet->getCell('D' . $row)->getCalculatedValue(),
$worksheet->getCell('D' . $row)->getFormattedValue()
));
/** @var null|bool|float|int|string */
$calc = $worksheet->getCell('D' . $row)->getCalculatedValue();
$helper->log(
$worksheet->getCell('B' . $row)->getFormattedValue()
. ' and '
. $worksheet->getCell('C' . $row)->getFormattedValue()
. ' months is '
. $worksheet->getCell('D' . $row)->getCalculatedValue()
. ' ('
. $worksheet->getCell('D' . $row)->getFormattedValue()
. ')'
);
}
17 changes: 10 additions & 7 deletions samples/Calculations/DateTime/EOMONTH.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@

// Test the formulae
for ($row = 1; $row <= $testDateCount; ++$row) {
$helper->log(sprintf(
'%s and %d months is %d (%s)',
$worksheet->getCell('B' . $row)->getFormattedValue(),
$worksheet->getCell('C' . $row)->getFormattedValue(),
$worksheet->getCell('D' . $row)->getCalculatedValue(),
$worksheet->getCell('D' . $row)->getFormattedValue()
));
$helper->log(
$worksheet->getCell('B' . $row)->getFormattedValue()
. ' and '
. $worksheet->getCell('C' . $row)->getFormattedValue()
. ' months is '
. $worksheet->getCell('D' . $row)->getCalculatedValue()
. ' ('
. $worksheet->getCell('D' . $row)->getFormattedValue()
. ')'
);
}
18 changes: 11 additions & 7 deletions samples/Calculations/DateTime/NETWORKDAYS.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@
$helper->displayGrid($holidayData);

for ($row = 1; $row <= 12; ++$row) {
$helper->log(sprintf(
'Between %s and %s is %d working days; %d with public holidays',
$worksheet->getCell('A1')->getFormattedValue(),
$worksheet->getCell('B' . $row)->getFormattedValue(),
$worksheet->getCell('C' . $row)->getCalculatedValue(),
$worksheet->getCell('D' . $row)->getCalculatedValue()
));
$helper->log(
'Between '
. $worksheet->getCell('A1')->getFormattedValue()
. ' and '
. $worksheet->getCell('B' . $row)->getFormattedValue()
. ' is '
. $worksheet->getCell('C' . $row)->getCalculatedValue()
. ' working days; '
. $worksheet->getCell('D' . $row)->getCalculatedValue()
. ' excluding public holidays'
);
}
12 changes: 7 additions & 5 deletions samples/Calculations/DateTime/NOW.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
->setFormatCode('yyyy-mm-dd hh:mm:ss');

// Test the formulae
$helper->log(sprintf(
'Today is %f (%s)',
$worksheet->getCell('A1')->getCalculatedValue(),
$worksheet->getCell('A1')->getFormattedValue()
));
$helper->log(
'Today is '
. $worksheet->getCell('A1')->getCalculatedValue()
. ' ('
. $worksheet->getCell('A1')->getFormattedValue()
. ')'
);
12 changes: 7 additions & 5 deletions samples/Calculations/DateTime/TODAY.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
->setFormatCode('yyyy-mm-dd');

// Test the formulae
$helper->log(sprintf(
'Today is %d (%s)',
$worksheet->getCell('A1')->getCalculatedValue(),
$worksheet->getCell('A1')->getFormattedValue()
));
$helper->log(
'Today is '
. $worksheet->getCell('A1')->getCalculatedValue()
. ' ('
. $worksheet->getCell('A1')->getFormattedValue()
. ')'
);
18 changes: 10 additions & 8 deletions samples/Calculations/DateTime/WEEKDAY.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@
// Test the formulae
for ($row = 1; $row <= $testDateCount; ++$row) {
$helper->log(sprintf('(E%d): %s', $row, $worksheet->getCell('E' . $row)->getFormattedValue()));
$helper->log(sprintf(
'Weekday is: %d (1-7 = Sun-Sat)',
$worksheet->getCell('F' . $row)->getCalculatedValue()
));
$helper->log(sprintf(
'Weekday is: %d (1-7 = Mon-Sun)',
$worksheet->getCell('G' . $row)->getCalculatedValue()
));
$helper->log(
'Weekday is: '
. $worksheet->getCell('F' . $row)->getCalculatedValue()
. ' (1-7 = Sun-Sat)'
);
$helper->log(
'Weekday is: '
. $worksheet->getCell('G' . $row)->getCalculatedValue()
. ' (1-7 = Mon-Sun)'
);
}
45 changes: 25 additions & 20 deletions samples/Calculations/DateTime/YEARFRAC.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,29 @@
$worksheet->getCell('E' . $row)->getFormattedValue(),
$worksheet->getCell('F' . $row)->getFormattedValue()
));
$helper->log(sprintf(
'Days: %f - US (NASD) 30/360',
$worksheet->getCell('G' . $row)->getCalculatedValue()
));
$helper->log(sprintf(
'Days: %f - Actual',
$worksheet->getCell('H' . $row)->getCalculatedValue()
));
$helper->log(sprintf(
'Days: %f - Actual/360',
$worksheet->getCell('I' . $row)->getCalculatedValue()
));
$helper->log(sprintf(
'Days: %f - Actual/365',
$worksheet->getCell('J' . $row)->getCalculatedValue()
));
$helper->log(sprintf(
'Days: %f - European 30/360',
$worksheet->getCell('K' . $row)->getCalculatedValue()
));
$helper->log(
'Days: '
. $worksheet->getCell('G' . $row)->getCalculatedValue()
. ' - US (NASD) 30/360'
);
$helper->log(
'Days: '
. $worksheet->getCell('H' . $row)->getCalculatedValue()
. ' - Actual'
);
$helper->log(
'Days: '
. $worksheet->getCell('I' . $row)->getCalculatedValue()
. ' - Actual/360'
);
$helper->log(
'Days: '
. $worksheet->getCell('J' . $row)->getCalculatedValue()
. ' - Actual/365'
);
$helper->log(
'Days: '
. $worksheet->getCell('K' . $row)->getCalculatedValue()
. ' - European 30/360'
);
}
9 changes: 3 additions & 6 deletions samples/Calculations/Engineering/BESSELI.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
for ($n = 0; $n <= 5; ++$n) {
for ($x = 0; $x <= 5; $x = $x + 0.25) {
Calculation::getInstance($spreadsheet)->flushInstance();
$worksheet->setCellValue('A1', "=BESSELI({$x}, {$n})");
$formula = "BESSELI({$x}, {$n})";
$worksheet->setCellValue('A1', "=$formula");

$helper->log(sprintf(
'%s = %f',
$worksheet->getCell('A1')->getValue(),
$worksheet->getCell('A1')->getCalculatedValue()
));
$helper->log("$formula = " . $worksheet->getCell('A1')->getCalculatedValue());
}
}
9 changes: 3 additions & 6 deletions samples/Calculations/Engineering/BESSELJ.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
for ($n = 0; $n <= 5; ++$n) {
for ($x = 0; $x <= 5; $x = $x + 0.25) {
Calculation::getInstance($spreadsheet)->flushInstance();
$worksheet->setCellValue('A1', "=BESSELJ({$x}, {$n})");
$formula = "BESSELJ({$x}, {$n})";
$worksheet->setCellValue('A1', "=$formula");

$helper->log(sprintf(
'%s = %f',
$worksheet->getCell('A1')->getValue(),
$worksheet->getCell('A1')->getCalculatedValue()
));
$helper->log("$formula = " . $worksheet->getCell('A1')->getCalculatedValue());
}
}
9 changes: 3 additions & 6 deletions samples/Calculations/Engineering/BESSELK.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
for ($n = 0; $n <= 5; ++$n) {
for ($x = 0; $x <= 5; $x = $x + 0.25) {
Calculation::getInstance($spreadsheet)->flushInstance();
$worksheet->setCellValue('A1', "=BESSELK({$x}, {$n})");
$formula = "BESSELK({$x}, {$n})";
$worksheet->setCellValue('A1', "=$formula");

$helper->log(sprintf(
'%s = %f',
$worksheet->getCell('A1')->getValue(),
$worksheet->getCell('A1')->getCalculatedValue()
));
$helper->log("$formula = " . $worksheet->getCell('A1')->getCalculatedValue());
}
}
9 changes: 3 additions & 6 deletions samples/Calculations/Engineering/BESSELY.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
for ($n = 0; $n <= 5; ++$n) {
for ($x = 0; $x <= 5; $x = $x + 0.25) {
Calculation::getInstance($spreadsheet)->flushInstance();
$worksheet->setCellValue('A1', "=BESSELY({$x}, {$n})");
$formula = "BESSELY({$x}, {$n})";
$worksheet->setCellValue('A1', "=$formula");

$helper->log(sprintf(
'%s = %f',
$worksheet->getCell('A1')->getValue(),
$worksheet->getCell('A1')->getCalculatedValue()
));
$helper->log("$formula = " . $worksheet->getCell('A1')->getCalculatedValue());
}
}
11 changes: 5 additions & 6 deletions samples/Calculations/Engineering/BIN2DEC.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@

// Test the formulae
for ($row = 1; $row <= $testDataCount; ++$row) {
$helper->log(sprintf(
'(B%d): Binary %s is decimal %s',
$row,
$worksheet->getCell('A' . $row)->getValue(),
$worksheet->getCell('B' . $row)->getCalculatedValue(),
));
$helper->log(
"(B$row): "
. 'Binary ' . $worksheet->getCell("A$row")->getValue()
. ' is decimal ' . $worksheet->getCell("B$row")->getCalculatedValue()
);
}
Loading

0 comments on commit 641f86d

Please sign in to comment.