Skip to content

Commit 936805a

Browse files
authored
Merge pull request #3871 from oleibman/mixedsamples
Eliminate Some "Mixed" Variables in Samples
2 parents 0824fd5 + d6e4f4c commit 936805a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+654
-507
lines changed

infra/LocaleGenerator.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ public function generateLocales(): void
8686
protected function buildConfigFileForLocale(string $column, string $locale): void
8787
{
8888
$language = $this->localeTranslations->getCell($column . self::ENGLISH_LANGUAGE_NAME_ROW)->getValue();
89+
if (!is_string($language)) {
90+
throw new Exception('Non-string language value at ' . $column . self::ENGLISH_LANGUAGE_NAME_ROW);
91+
}
8992
$localeLanguage = $this->localeTranslations->getCell($column . self::LOCALE_LANGUAGE_NAME_ROW)->getValue();
93+
if (!is_string($localeLanguage)) {
94+
throw new Exception('Non-string locale language value at ' . $column . self::LOCALE_LANGUAGE_NAME_ROW);
95+
}
9096
$configFile = $this->openConfigFile($locale, $language, $localeLanguage);
9197

9298
$this->writeConfigArgumentSeparator($configFile, $column);
@@ -96,6 +102,9 @@ protected function buildConfigFileForLocale(string $column, string $locale): voi
96102
foreach ($this->errorCodeMap as $errorCode => $row) {
97103
$translationCell = $this->localeTranslations->getCell($column . $row);
98104
$translationValue = $translationCell->getValue();
105+
if ($translationValue !== null && !is_string($translationValue)) {
106+
throw new Exception('Non-string translation value at ' . $column . $row);
107+
}
99108
if (!empty($translationValue)) {
100109
$errorCodeTranslation = "{$errorCode} = {$translationValue}" . self::EOL;
101110
fwrite($configFile, $errorCodeTranslation);
@@ -114,6 +123,9 @@ protected function writeConfigArgumentSeparator($configFile, string $column): vo
114123
{
115124
$translationCell = $this->localeTranslations->getCell($column . self::ARGUMENT_SEPARATOR_ROW);
116125
$localeValue = $translationCell->getValue();
126+
if ($localeValue !== null && !is_string($localeValue)) {
127+
throw new Exception('Non-string locale value at ' . $column . self::CURRENCY_SYMBOL_ROW);
128+
}
117129
if (!empty($localeValue)) {
118130
$functionTranslation = "ArgumentSeparator = {$localeValue}" . self::EOL;
119131
fwrite($configFile, $functionTranslation);
@@ -127,6 +139,9 @@ protected function writeConfigCurrencySymbol($configFile, string $column): void
127139
{
128140
$translationCell = $this->localeTranslations->getCell($column . self::CURRENCY_SYMBOL_ROW);
129141
$localeValue = $translationCell->getValue();
142+
if ($localeValue !== null && !is_string($localeValue)) {
143+
throw new Exception('Non-string locale value at ' . $column . self::CURRENCY_SYMBOL_ROW);
144+
}
130145
if (!empty($localeValue)) {
131146
$functionTranslation = "currencySymbol = {$localeValue}" . self::EOL;
132147
fwrite($configFile, '##' . self::EOL);
@@ -141,13 +156,22 @@ protected function writeConfigCurrencySymbol($configFile, string $column): void
141156
protected function buildFunctionsFileForLocale(string $column, string $locale): void
142157
{
143158
$language = $this->functionNameTranslations->getCell($column . self::ENGLISH_LANGUAGE_NAME_ROW)->getValue();
159+
if (!is_string($language)) {
160+
throw new Exception('Non-string language value at ' . $column . self::ENGLISH_LANGUAGE_NAME_ROW);
161+
}
144162
$localeLanguage = $this->functionNameTranslations->getCell($column . self::LOCALE_LANGUAGE_NAME_ROW)
145163
->getValue();
164+
if (!is_string($localeLanguage)) {
165+
throw new Exception('Non-string locale language value at ' . $column . self::LOCALE_LANGUAGE_NAME_ROW);
166+
}
146167
$functionFile = $this->openFunctionNameFile($locale, $language, $localeLanguage);
147168

148169
foreach ($this->functionNameMap as $functionName => $row) {
149170
$translationCell = $this->functionNameTranslations->getCell($column . $row);
150171
$translationValue = $translationCell->getValue();
172+
if ($translationValue !== null && !is_string($translationValue)) {
173+
throw new Exception('Non-string translation value at ' . $column . $row);
174+
}
151175
if ($this->isFunctionCategoryEntry($translationCell)) {
152176
$this->writeFileSectionHeader($functionFile, "{$translationValue} ({$functionName})");
153177
} elseif (!array_key_exists($functionName, $this->phpSpreadsheetFunctions) && substr($functionName, 0, 1) !== '*') {

samples/Basic/13_Calculation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@
157157
$helper->log('Calculated data');
158158
for ($col = 'B'; $col != 'G'; ++$col) {
159159
for ($row = 14; $row <= 41; ++$row) {
160+
$formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue();
160161
if (
161-
(($formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue()) !== null)
162+
is_string($formula)
162163
&& ($formula[0] == '=')
163164
) {
164165
$helper->log('Value of ' . $col . $row . ' [' . $formula . ']: ' . $spreadsheet->getActiveSheet()->getCell($col . $row)->getCalculatedValue());

samples/Basic/13_CalculationCyclicFormulae.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
$helper->log('Calculated data');
2323
for ($row = 1; $row <= 2; ++$row) {
2424
for ($col = 'A'; $col != 'C'; ++$col) {
25+
$formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue();
2526
if (
26-
(($formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue()) !== null)
27+
is_string($formula)
2728
&& ($formula[0] == '=')
2829
) {
2930
$helper->log('Value of ' . $col . $row . ' [' . $formula . ']: ' . $spreadsheet->getActiveSheet()->getCell($col . $row)->getCalculatedValue());

samples/Basic/31_Document_properties_write.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
$propertyValue = $spreadsheet->getProperties()->getCustomPropertyValue($customProperty);
5757
$propertyType = $spreadsheet->getProperties()->getCustomPropertyType($customProperty);
5858
if ($propertyType == Properties::PROPERTY_TYPE_DATE) {
59-
$formattedValue = date('d-M-Y H:i:s', (int) $propertyValue);
59+
$formattedValue = is_numeric($propertyValue) ? date('d-M-Y H:i:s', (int) $propertyValue) : '*****INVALID*****';
6060
} elseif ($propertyType == Properties::PROPERTY_TYPE_BOOLEAN) {
6161
$formattedValue = $propertyValue ? 'TRUE' : 'FALSE';
6262
} else {

samples/Basic/31_Document_properties_write_xls.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
$propertyValue = $spreadsheet->getProperties()->getCustomPropertyValue($customProperty);
5757
$propertyType = $spreadsheet->getProperties()->getCustomPropertyType($customProperty);
5858
if ($propertyType == Properties::PROPERTY_TYPE_DATE) {
59-
$formattedValue = date('d-M-Y H:i:s', (int) $propertyValue);
59+
$formattedValue = is_numeric($propertyValue) ? date('d-M-Y H:i:s', (int) $propertyValue) : '*****INVALID*****';
6060
} elseif ($propertyType == Properties::PROPERTY_TYPE_BOOLEAN) {
6161
$formattedValue = $propertyValue ? 'TRUE' : 'FALSE';
6262
} else {

samples/Basic/45_Quadratic_equation_solver.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@
5959
$r1Formula = '=IMDIV(IMSUM(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . ')';
6060
$r2Formula = '=IF(' . $discriminant . '=0,"Only one root",IMDIV(IMSUB(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . '))';
6161

62-
$helper->log(Calculation::getInstance()->calculateFormula($r1Formula));
63-
$helper->log(Calculation::getInstance()->calculateFormula($r2Formula));
62+
/** @var string */
63+
$output = Calculation::getInstance()->calculateFormula($r1Formula);
64+
$helper->log("$output");
65+
/** @var string */
66+
$output = Calculation::getInstance()->calculateFormula($r2Formula);
67+
$helper->log("$output");
6468
$callEndTime = microtime(true);
6569
$helper->logEndingNotes();
6670
}

samples/Calculations/DateTime/DAYS360.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@
4949
$worksheet->getCell('E' . $row)->getFormattedValue(),
5050
$worksheet->getCell('F' . $row)->getFormattedValue()
5151
));
52-
$helper->log(sprintf(
53-
'Days: %d (US) %d (European)',
54-
$worksheet->getCell('G' . $row)->getCalculatedValue(),
55-
$worksheet->getCell('H' . $row)->getCalculatedValue()
56-
));
52+
$helper->log(
53+
'Days: '
54+
. $worksheet->getCell('G' . $row)->getCalculatedValue()
55+
. ' (US) '
56+
. $worksheet->getCell('H' . $row)->getCalculatedValue()
57+
. ' (European)'
58+
);
5759
}

samples/Calculations/DateTime/EDATE.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@
3333

3434
// Test the formulae
3535
for ($row = 1; $row <= $testDateCount; ++$row) {
36-
$helper->log(sprintf(
37-
'%s and %d months is %d (%s)',
38-
$worksheet->getCell('B' . $row)->getFormattedValue(),
39-
$worksheet->getCell('C' . $row)->getFormattedValue(),
40-
$worksheet->getCell('D' . $row)->getCalculatedValue(),
41-
$worksheet->getCell('D' . $row)->getFormattedValue()
42-
));
36+
/** @var null|bool|float|int|string */
37+
$calc = $worksheet->getCell('D' . $row)->getCalculatedValue();
38+
$helper->log(
39+
$worksheet->getCell('B' . $row)->getFormattedValue()
40+
. ' and '
41+
. $worksheet->getCell('C' . $row)->getFormattedValue()
42+
. ' months is '
43+
. $worksheet->getCell('D' . $row)->getCalculatedValue()
44+
. ' ('
45+
. $worksheet->getCell('D' . $row)->getFormattedValue()
46+
. ')'
47+
);
4348
}

samples/Calculations/DateTime/EOMONTH.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333

3434
// Test the formulae
3535
for ($row = 1; $row <= $testDateCount; ++$row) {
36-
$helper->log(sprintf(
37-
'%s and %d months is %d (%s)',
38-
$worksheet->getCell('B' . $row)->getFormattedValue(),
39-
$worksheet->getCell('C' . $row)->getFormattedValue(),
40-
$worksheet->getCell('D' . $row)->getCalculatedValue(),
41-
$worksheet->getCell('D' . $row)->getFormattedValue()
42-
));
36+
$helper->log(
37+
$worksheet->getCell('B' . $row)->getFormattedValue()
38+
. ' and '
39+
. $worksheet->getCell('C' . $row)->getFormattedValue()
40+
. ' months is '
41+
. $worksheet->getCell('D' . $row)->getCalculatedValue()
42+
. ' ('
43+
. $worksheet->getCell('D' . $row)->getFormattedValue()
44+
. ')'
45+
);
4346
}

samples/Calculations/DateTime/NETWORKDAYS.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@
5656
$helper->displayGrid($holidayData);
5757

5858
for ($row = 1; $row <= 12; ++$row) {
59-
$helper->log(sprintf(
60-
'Between %s and %s is %d working days; %d with public holidays',
61-
$worksheet->getCell('A1')->getFormattedValue(),
62-
$worksheet->getCell('B' . $row)->getFormattedValue(),
63-
$worksheet->getCell('C' . $row)->getCalculatedValue(),
64-
$worksheet->getCell('D' . $row)->getCalculatedValue()
65-
));
59+
$helper->log(
60+
'Between '
61+
. $worksheet->getCell('A1')->getFormattedValue()
62+
. ' and '
63+
. $worksheet->getCell('B' . $row)->getFormattedValue()
64+
. ' is '
65+
. $worksheet->getCell('C' . $row)->getCalculatedValue()
66+
. ' working days; '
67+
. $worksheet->getCell('D' . $row)->getCalculatedValue()
68+
. ' excluding public holidays'
69+
);
6670
}

0 commit comments

Comments
 (0)