Eliminate Some Scrutinizer 'Major' Problems Part 2 (#3122)

* Eliminate Some Scrutinizer 'Major' Problems Part 2

Mostly Scrutinizer bizarre false positives. From Calculation/Engineering/Bessell (and several others):
```php
     private static function besselI2(float $x, int $ord): float
     {
        if ($x === 0.0) {
             return 0.0;
         }
```
Scrutinizer complains that `$x` can never equal 0.0 here. Huh???

Another example repeated several times, from Calculation/Engineering/BesselJ:
```php
$bool = false;
foreach (whatever) {
    ...
    $bool = !$bool;
    ...
}
```
Scrutinizer complains about the `!$bool` assignment because it says `$bool` is always false. Again, huh???

* Change Logical Not Handling

From a suggestion from @MarkBaker, sidestep bug in Scrutinizer by recoding
```php
$jsum = !$jsum
```
as:
```php
$jsum = $jsum === false;
```
This commit is contained in:
oleibman 2022-10-18 19:34:37 -07:00 committed by GitHub
parent e4e99b8a73
commit f1d73d8dba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 55 additions and 21 deletions

View File

@ -72,10 +72,10 @@ class Date
$baseYear = SharedDateHelper::getExcelCalendar(); $baseYear = SharedDateHelper::getExcelCalendar();
try { try {
$year = self::getYear($year, $baseYear); $year = self::getYear($year, $baseYear); // must be int - Scrutinizer is wrong
$month = self::getMonth($month); $month = self::getMonth($month);
$day = self::getDay($day); $day = self::getDay($day);
self::adjustYearMonth($year, $month, $baseYear); self::adjustYearMonth(/** @scrutinizer ignore-type */ $year, $month, $baseYear);
} catch (Exception $e) { } catch (Exception $e) {
return $e->getMessage(); return $e->getMessage();
} }

View File

@ -47,12 +47,12 @@ class Difference
// Execute function // Execute function
$PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate); $PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate);
$startDays = (int) $PHPStartDateObject->format('j'); $startDays = (int) $PHPStartDateObject->format('j');
$startMonths = (int) $PHPStartDateObject->format('n'); //$startMonths = (int) $PHPStartDateObject->format('n');
$startYears = (int) $PHPStartDateObject->format('Y'); $startYears = (int) $PHPStartDateObject->format('Y');
$PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate); $PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate);
$endDays = (int) $PHPEndDateObject->format('j'); $endDays = (int) $PHPEndDateObject->format('j');
$endMonths = (int) $PHPEndDateObject->format('n'); //$endMonths = (int) $PHPEndDateObject->format('n');
$endYears = (int) $PHPEndDateObject->format('Y'); $endYears = (int) $PHPEndDateObject->format('Y');
$PHPDiffDateObject = $PHPEndDateObject->diff($PHPStartDateObject); $PHPDiffDateObject = $PHPEndDateObject->diff($PHPStartDateObject);
@ -138,7 +138,7 @@ class Difference
$PHPEndDateObject->modify('+1 year'); $PHPEndDateObject->modify('+1 year');
// Get the result // Get the result
$retVal = $PHPEndDateObject->diff($PHPStartDateObject)->days; $retVal = (int) $PHPEndDateObject->diff($PHPStartDateObject)->days;
// Adjust for leap years cases // Adjust for leap years cases
$isLeapEndYear = $PHPEndDateObject->format('L'); $isLeapEndYear = $PHPEndDateObject->format('L');

View File

@ -67,7 +67,7 @@ class TimeValue
if ($retType === Functions::RETURNDATE_EXCEL) { if ($retType === Functions::RETURNDATE_EXCEL) {
$retValue = (float) $excelDateValue; $retValue = (float) $excelDateValue;
} elseif ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) { } elseif ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
$retValue = (int) $phpDateValue = SharedDateHelper::excelToTimestamp($excelDateValue + 25569) - 3600; $retValue = (int) SharedDateHelper::excelToTimestamp($excelDateValue + 25569) - 3600;
} else { } else {
$retValue = new DateTime('1900-01-01 ' . $PHPDateArray['hour'] . ':' . $PHPDateArray['minute'] . ':' . $PHPDateArray['second']); $retValue = new DateTime('1900-01-01 ' . $PHPDateArray['hour'] . ':' . $PHPDateArray['minute'] . ':' . $PHPDateArray['second']);
} }

View File

@ -111,9 +111,16 @@ class BesselI
return ($x < 0.0) ? -$ans : $ans; return ($x < 0.0) ? -$ans : $ans;
} }
/**
* Sop to Scrutinizer.
*
* @var float
*/
private static $zeroPointZero = 0.0;
private static function besselI2(float $x, int $ord): float private static function besselI2(float $x, int $ord): float
{ {
if ($x === 0.0) { if ($x === self::$zeroPointZero) {
return 0.0; return 0.0;
} }

View File

@ -167,7 +167,7 @@ class BesselJ
if ($jsum === true) { if ($jsum === true) {
$sum += $bj; $sum += $bj;
} }
$jsum = !$jsum; $jsum = $jsum === false;
if ($j === $ord) { if ($j === $ord) {
$ans = $bjp; $ans = $bjp;
} }

View File

@ -59,7 +59,7 @@ class ErfC
$a = $n = 1; $a = $n = 1;
$b = $c = $value; $b = $c = $value;
$d = ($value * $value) + 0.5; $d = ($value * $value) + 0.5;
$q1 = $q2 = $b / $d; $q2 = $b / $d;
do { do {
$t = $a * $n + $b * $value; $t = $a * $n + $b * $value;
$a = $b; $a = $b;

View File

@ -112,7 +112,7 @@ class Periodic
$rr = 1.0 + $reinvestmentRate; $rr = 1.0 + $reinvestmentRate;
$fr = 1.0 + $financeRate; $fr = 1.0 + $financeRate;
$npvPos = $npvNeg = 0.0; $npvPos = $npvNeg = self::$zeroPointZero;
foreach ($values as $i => $v) { foreach ($values as $i => $v) {
if ($v >= 0) { if ($v >= 0) {
$npvPos += $v / $rr ** $i; $npvPos += $v / $rr ** $i;
@ -121,7 +121,7 @@ class Periodic
} }
} }
if (($npvNeg === 0.0) || ($npvPos === 0.0) || ($reinvestmentRate <= -1.0)) { if (($npvNeg === self::$zeroPointZero) || ($npvPos === self::$zeroPointZero) || ($reinvestmentRate <= -1.0)) {
return ExcelError::VALUE(); return ExcelError::VALUE();
} }
@ -131,6 +131,13 @@ class Periodic
return is_finite($mirr) ? $mirr : ExcelError::VALUE(); return is_finite($mirr) ? $mirr : ExcelError::VALUE();
} }
/**
* Sop to Scrutinizer.
*
* @var float
*/
private static $zeroPointZero = 0.0;
/** /**
* NPV. * NPV.
* *

View File

@ -261,6 +261,7 @@ class Coupons
self::validateCouponPeriod($settlement, $maturity); self::validateCouponPeriod($settlement, $maturity);
$frequency = FinancialValidations::validateFrequency($frequency); $frequency = FinancialValidations::validateFrequency($frequency);
$basis = FinancialValidations::validateBasis($basis); $basis = FinancialValidations::validateBasis($basis);
self::doNothing($basis);
} catch (Exception $e) { } catch (Exception $e) {
return $e->getMessage(); return $e->getMessage();
} }
@ -315,6 +316,7 @@ class Coupons
self::validateCouponPeriod($settlement, $maturity); self::validateCouponPeriod($settlement, $maturity);
$frequency = FinancialValidations::validateFrequency($frequency); $frequency = FinancialValidations::validateFrequency($frequency);
$basis = FinancialValidations::validateBasis($basis); $basis = FinancialValidations::validateBasis($basis);
self::doNothing($basis);
} catch (Exception $e) { } catch (Exception $e) {
return $e->getMessage(); return $e->getMessage();
} }
@ -375,6 +377,7 @@ class Coupons
self::validateCouponPeriod($settlement, $maturity); self::validateCouponPeriod($settlement, $maturity);
$frequency = FinancialValidations::validateFrequency($frequency); $frequency = FinancialValidations::validateFrequency($frequency);
$basis = FinancialValidations::validateBasis($basis); $basis = FinancialValidations::validateBasis($basis);
self::doNothing($basis);
} catch (Exception $e) { } catch (Exception $e) {
return $e->getMessage(); return $e->getMessage();
} }
@ -414,4 +417,10 @@ class Coupons
throw new Exception(ExcelError::NAN()); throw new Exception(ExcelError::NAN());
} }
} }
/** @param mixed $basis */
private static function doNothing($basis): bool
{
return $basis;
}
} }

View File

@ -8,6 +8,9 @@ use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
class Depreciation class Depreciation
{ {
/** @var float */
private static $zeroPointZero = 0.0;
/** /**
* DB. * DB.
* *
@ -51,7 +54,7 @@ class Depreciation
return $e->getMessage(); return $e->getMessage();
} }
if ($cost === 0.0) { if ($cost === self::$zeroPointZero) {
return 0.0; return 0.0;
} }
@ -161,7 +164,7 @@ class Depreciation
return $e->getMessage(); return $e->getMessage();
} }
if ($life === 0.0) { if ($life === self::$zeroPointZero) {
return ExcelError::DIV0(); return ExcelError::DIV0();
} }

View File

@ -54,6 +54,7 @@ class AccruedInterest
$basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD, $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD,
$calcMethod = self::ACCRINT_CALCMODE_ISSUE_TO_SETTLEMENT $calcMethod = self::ACCRINT_CALCMODE_ISSUE_TO_SETTLEMENT
) { ) {
self::doNothing($calcMethod);
$issue = Functions::flattenSingleValue($issue); $issue = Functions::flattenSingleValue($issue);
$firstInterest = Functions::flattenSingleValue($firstInterest); $firstInterest = Functions::flattenSingleValue($firstInterest);
$settlement = Functions::flattenSingleValue($settlement); $settlement = Functions::flattenSingleValue($settlement);
@ -73,6 +74,7 @@ class AccruedInterest
$rate = SecurityValidations::validateRate($rate); $rate = SecurityValidations::validateRate($rate);
$parValue = SecurityValidations::validateParValue($parValue); $parValue = SecurityValidations::validateParValue($parValue);
$frequency = SecurityValidations::validateFrequency($frequency); $frequency = SecurityValidations::validateFrequency($frequency);
self::doNothing($frequency);
$basis = SecurityValidations::validateBasis($basis); $basis = SecurityValidations::validateBasis($basis);
} catch (Exception $e) { } catch (Exception $e) {
return $e->getMessage(); return $e->getMessage();
@ -148,4 +150,10 @@ class AccruedInterest
return $parValue * $rate * $daysBetweenIssueAndSettlement; return $parValue * $rate * $daysBetweenIssueAndSettlement;
} }
/** @param mixed $arg */
private static function doNothing($arg): bool
{
return (bool) $arg;
}
} }

View File

@ -576,7 +576,7 @@ class Functions
$flattened = []; $flattened = [];
$stack = array_values($array); $stack = array_values($array);
while ($stack) { while (!empty($stack)) {
$value = array_shift($stack); $value = array_shift($stack);
if (is_array($value)) { if (is_array($value)) {

View File

@ -463,8 +463,8 @@ class Chart
/** /**
* Set the offset position within the Top Left cell for the chart. * Set the offset position within the Top Left cell for the chart.
* *
* @param int $xOffset * @param ?int $xOffset
* @param int $yOffset * @param ?int $yOffset
* *
* @return $this * @return $this
*/ */
@ -587,8 +587,8 @@ class Chart
/** /**
* Set the offset position within the Bottom Right cell for the chart. * Set the offset position within the Bottom Right cell for the chart.
* *
* @param int $xOffset * @param ?int $xOffset
* @param int $yOffset * @param ?int $yOffset
* *
* @return $this * @return $this
*/ */

View File

@ -496,7 +496,7 @@ class DataSeriesValues extends Properties
if (($dimensions[0] == 1) || ($dimensions[1] == 1)) { if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
$this->dataValues = Functions::flattenArray($newDataValues); $this->dataValues = Functions::flattenArray($newDataValues);
} else { } else {
$newArray = array_values(array_shift($newDataValues)); $newArray = array_values(array_shift(/** @scrutinizer ignore-type */ $newDataValues));
foreach ($newArray as $i => $newDataSet) { foreach ($newArray as $i => $newDataSet) {
$newArray[$i] = [$newDataSet]; $newArray[$i] = [$newDataSet];
} }

View File

@ -539,7 +539,7 @@ abstract class Properties
/** /**
* Set Soft Edges Size. * Set Soft Edges Size.
* *
* @param float $size * @param ?float $size
*/ */
public function setSoftEdges($size): void public function setSoftEdges($size): void
{ {

View File

@ -16,7 +16,7 @@ class Settings
* Class name of the chart renderer used for rendering charts * Class name of the chart renderer used for rendering charts
* eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph. * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph.
* *
* @var string * @var ?string
*/ */
private static $chartRenderer; private static $chartRenderer;