Correct Some Problems Which Will Show Up for PHP8.1 (#2191)

* Reader/Gnumeric vs. Scrutinizer

Just reviewing Scrutinizer's list of "bugs". There are 19 ascribed to me. For some, I will definitely take no action (e.g. use of bitwise operators in AND, OR, and XOR functions). However, where I can clean things up so that Scrutinizer is satisfied and the resulting code is not too contorted, I will make an attempt.

I believe this is the only one with which will involve more than 2 or 3 changes. It fixes 5 items ascribed to me, and 4 to others.

* Use Strict Checking for in_array

* Correct Some Problems Which Will Show Up for PHP8.1

PHP8.1 wants to issue a message when you use a float where it thinks you ought to be using an int (it wants its implicit casts made explicit). This is causing unit tests to fail. The following corrections are made in this PR:
- Calculation.php tests `isset(self::binaryOperators[$token])`, where token can be a float. No numeric values are members of that array, so we can test for numeric before isset.
- SharedOle.php packs a float, intending it as an int, in 2 places. I simplified the logic here, and added explicit casts to avoid the problem. This is used by Xls Reader and Writer; as added confirmation, I added some timestamps from before 1970 (i.e. negative values) to Document/EpochTest. Because of this, the test suite has been verified for 32-bit PHP as well as PHP 8.1.
- Writer/Xlsx/StringTable tests `isset($aFlippedStringTable[$cellValue])`. This is the same problem as in Calculation, but requires a different solution. The same if statement here also tests that the datatype is string, but it does so after the isset test. Changing the order of these tests avoids the problem.

* Update OLE.php
This commit is contained in:
oleibman 2021-06-29 10:54:08 -07:00 committed by GitHub
parent 3ddd12a49b
commit 49e97f0914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 19 deletions

View File

@ -4477,7 +4477,7 @@ class Calculation
}
// if the token is a binary operator, pop the top two values off the stack, do the operation, and push the result back on the stack
if (isset(self::$binaryOperators[$token])) {
if (!is_numeric($token) && isset(self::$binaryOperators[$token])) {
// We must have two operands, error if we don't
if (($operand2Data = $stack->pop()) === null) {
return $this->raiseFormulaError('Internal error - Operand value missing from stack');

View File

@ -502,9 +502,6 @@ class OLE
}
$dateTime = Date::dateTimeFromTimestamp("$date");
// factor used for separating numbers into 4 bytes parts
$factor = 2 ** 32;
// days from 1-1-1601 until the beggining of UNIX era
$days = 134774;
// calculate seconds
@ -512,22 +509,15 @@ class OLE
// multiply just to make MS happy
$big_date *= 10000000;
$high_part = floor($big_date / $factor);
// lower 4 bytes
$low_part = floor((($big_date / $factor) - $high_part) * $factor);
// Make HEX string
$res = '';
for ($i = 0; $i < 4; ++$i) {
$hex = $low_part % 0x100;
$res .= pack('c', $hex);
$low_part /= 0x100;
}
for ($i = 0; $i < 4; ++$i) {
$hex = $high_part % 0x100;
$res .= pack('c', $hex);
$high_part /= 0x100;
$factor = 2 ** 56;
while ($factor >= 1) {
$hex = (int) floor($big_date / $factor);
$res = pack('c', $hex) . $res;
$big_date = fmod($big_date, $factor);
$factor /= 256;
}
return $res;

View File

@ -42,8 +42,8 @@ class StringTable extends WriterPart
!is_object($cellValue) &&
($cellValue !== null) &&
$cellValue !== '' &&
!isset($aFlippedStringTable[$cellValue]) &&
($cell->getDataType() == DataType::TYPE_STRING || $cell->getDataType() == DataType::TYPE_STRING2 || $cell->getDataType() == DataType::TYPE_NULL)
($cell->getDataType() == DataType::TYPE_STRING || $cell->getDataType() == DataType::TYPE_STRING2 || $cell->getDataType() == DataType::TYPE_NULL) &&
!isset($aFlippedStringTable[$cellValue])
) {
$aStringTable[] = $cellValue;
$aFlippedStringTable[$cellValue] = true;

View File

@ -11,10 +11,13 @@ class EpochTest extends AbstractFunctional
public function providerFormats(): array
{
return [
['Ods', '1921-03-17 11:30:00Z'],
['Ods', '2021-03-17 11:30:00Z'],
['Ods', '2041-03-17 11:30:00Z'],
['Xls', '1921-03-17 11:30:00Z'],
['Xls', '2021-03-17 11:30:00Z'],
['Xls', '2041-03-17 11:30:00Z'],
['Xlsx', '1921-03-17 11:30:00Z'],
['Xlsx', '2021-03-17 11:30:00Z'],
['Xlsx', '2041-03-17 11:30:00Z'],
];