Skip to content

Commit 64e61d8

Browse files
authored
Prep Work for Phpstan Upgrade (#2728)
Dependabot submitted PRs #2719 and #2720 to upgrade Phpstan. As with most Phpstan upgrades, there are new error messages; this PR is an attempt to fix all 58 of the new problems in order to allow the upgrade to proceed. Most of these fixes involve the addition of doc-block type annotations, often involving the assignment of the 'objectionable' portion of the statement to a new variable. Some use explicit casting when I am sure that's safe. Some (Reader/Ods) involve defeating result caching by Phpstan.
1 parent 28bb2cd commit 64e61d8

File tree

19 files changed

+143
-73
lines changed

19 files changed

+143
-73
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,6 @@ parameters:
340340
count: 1
341341
path: src/PhpSpreadsheet/Calculation/Engineering/BesselJ.php
342342

343-
-
344-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Engineering\\\\BesselK\\:\\:besselK2\\(\\) has no return type specified\\.$#"
345-
count: 1
346-
path: src/PhpSpreadsheet/Calculation/Engineering/BesselK.php
347-
348343
-
349344
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Engineering\\\\ConvertBase\\:\\:validatePlaces\\(\\) has parameter \\$places with no type specified\\.$#"
350345
count: 1
@@ -2070,11 +2065,6 @@ parameters:
20702065
count: 1
20712066
path: src/PhpSpreadsheet/Reader/Ods.php
20722067

2073-
-
2074-
message: "#^If condition is always true\\.$#"
2075-
count: 1
2076-
path: src/PhpSpreadsheet/Reader/Ods.php
2077-
20782068
-
20792069
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\:\\:listWorksheetNames\\(\\) should return array\\<string\\> but returns array\\<int, string\\|null\\>\\.$#"
20802070
count: 1
@@ -2095,11 +2085,6 @@ parameters:
20952085
count: 1
20962086
path: src/PhpSpreadsheet/Reader/Ods.php
20972087

2098-
-
2099-
message: "#^While loop condition is always true\\.$#"
2100-
count: 2
2101-
path: src/PhpSpreadsheet/Reader/Ods.php
2102-
21032088
-
21042089
message: "#^Cannot call method getElementsByTagNameNS\\(\\) on DOMElement\\|null\\.$#"
21052090
count: 3
@@ -3880,11 +3865,6 @@ parameters:
38803865
count: 1
38813866
path: src/PhpSpreadsheet/Shared/Trend/Trend.php
38823867

3883-
-
3884-
message: "#^Parameter \\#1 \\$order of class PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Trend\\\\PolynomialBestFit constructor expects int, string given\\.$#"
3885-
count: 2
3886-
path: src/PhpSpreadsheet/Shared/Trend/Trend.php
3887-
38883868
-
38893869
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\XMLWriter\\:\\:getData\\(\\) should return string but returns string\\|false\\.$#"
38903870
count: 1

src/PhpSpreadsheet/Calculation/Engineering/BesselK.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,28 @@ private static function calculate(float $x, int $ord): float
6868
return self::besselK2($x, $ord);
6969
}
7070

71+
/**
72+
* Mollify Phpstan.
73+
*
74+
* @codeCoverageIgnore
75+
*/
76+
private static function callBesselI(float $x, int $ord): float
77+
{
78+
$rslt = BesselI::BESSELI($x, $ord);
79+
if (!is_float($rslt)) {
80+
throw new Exception('Unexpected array or string');
81+
}
82+
83+
return $rslt;
84+
}
85+
7186
private static function besselK0(float $x): float
7287
{
7388
if ($x <= 2) {
7489
$fNum2 = $x * 0.5;
7590
$y = ($fNum2 * $fNum2);
7691

77-
return -log($fNum2) * BesselI::BESSELI($x, 0) +
92+
return -log($fNum2) * self::callBesselI($x, 0) +
7893
(-0.57721566 + $y * (0.42278420 + $y * (0.23069756 + $y * (0.3488590e-1 + $y * (0.262698e-2 + $y *
7994
(0.10750e-3 + $y * 0.74e-5))))));
8095
}
@@ -92,7 +107,7 @@ private static function besselK1(float $x): float
92107
$fNum2 = $x * 0.5;
93108
$y = ($fNum2 * $fNum2);
94109

95-
return log($fNum2) * BesselI::BESSELI($x, 1) +
110+
return log($fNum2) * self::callBesselI($x, 1) +
96111
(1 + $y * (0.15443144 + $y * (-0.67278579 + $y * (-0.18156897 + $y * (-0.1919402e-1 + $y *
97112
(-0.110404e-2 + $y * (-0.4686e-4))))))) / $x;
98113
}
@@ -104,7 +119,7 @@ private static function besselK1(float $x): float
104119
(0.325614e-2 + $y * (-0.68245e-3)))))));
105120
}
106121

107-
private static function besselK2(float $x, int $ord)
122+
private static function besselK2(float $x, int $ord): float
108123
{
109124
$fTox = 2 / $x;
110125
$fBkm = self::besselK0($x);

src/PhpSpreadsheet/Calculation/Engineering/BesselY.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ private static function calculate(float $x, int $ord): float
6666
return self::besselY2($x, $ord);
6767
}
6868

69+
/**
70+
* Mollify Phpstan.
71+
*
72+
* @codeCoverageIgnore
73+
*/
74+
private static function callBesselJ(float $x, int $ord): float
75+
{
76+
$rslt = BesselJ::BESSELJ($x, $ord);
77+
if (!is_float($rslt)) {
78+
throw new Exception('Unexpected array or string');
79+
}
80+
81+
return $rslt;
82+
}
83+
6984
private static function besselY0(float $x): float
7085
{
7186
if ($x < 8.0) {
@@ -75,7 +90,7 @@ private static function besselY0(float $x): float
7590
$ans2 = 40076544269.0 + $y * (745249964.8 + $y * (7189466.438 + $y *
7691
(47447.26470 + $y * (226.1030244 + $y))));
7792

78-
return $ans1 / $ans2 + 0.636619772 * BesselJ::BESSELJ($x, 0) * log($x);
93+
return $ans1 / $ans2 + 0.636619772 * self::callBesselJ($x, 0) * log($x);
7994
}
8095

8196
$z = 8.0 / $x;
@@ -97,7 +112,7 @@ private static function besselY1(float $x): float
97112
$ans2 = 0.2499580570e14 + $y * (0.4244419664e12 + $y * (0.3733650367e10 + $y * (0.2245904002e8 + $y *
98113
(0.1020426050e6 + $y * (0.3549632885e3 + $y)))));
99114

100-
return ($ans1 / $ans2) + 0.636619772 * (BesselJ::BESSELJ($x, 1) * log($x) - 1 / $x);
115+
return ($ans1 / $ans2) + 0.636619772 * (self::callBesselJ($x, 1) * log($x) - 1 / $x);
101116
}
102117

103118
$z = 8.0 / $x;

src/PhpSpreadsheet/Calculation/Financial/Amortization.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ public static function AMORDEGRC(
7070
return $e->getMessage();
7171
}
7272

73-
$yearFrac = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
74-
if (is_string($yearFrac)) {
75-
return $yearFrac;
73+
$yearFracx = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
74+
if (is_string($yearFracx)) {
75+
return $yearFracx;
7676
}
77+
/** @var float */
78+
$yearFrac = $yearFracx;
7779

7880
$amortiseCoeff = self::getAmortizationCoefficient($rate);
7981

@@ -161,10 +163,12 @@ public static function AMORLINC(
161163
$fCostDelta = $cost - $salvage;
162164
// Note, quirky variation for leap years on the YEARFRAC for this function
163165
$purchasedYear = DateTimeExcel\DateParts::year($purchased);
164-
$yearFrac = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
165-
if (is_string($yearFrac)) {
166-
return $yearFrac;
166+
$yearFracx = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
167+
if (is_string($yearFracx)) {
168+
return $yearFracx;
167169
}
170+
/** @var float */
171+
$yearFrac = $yearFracx;
168172

169173
if (
170174
($basis == FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL) &&

src/PhpSpreadsheet/Calculation/Financial/Coupons.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public static function COUPDAYSNC(
199199
return $e->getMessage();
200200
}
201201

202+
/** @var int */
202203
$daysPerYear = Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis);
203204
$next = self::couponFirstPeriodDate($settlement, $maturity, $frequency, self::PERIOD_DATE_NEXT);
204205

src/PhpSpreadsheet/Calculation/Statistical/Confidence.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public static function CONFIDENCE($alpha, $stdDev, $size)
4444
if (($alpha <= 0) || ($alpha >= 1) || ($stdDev <= 0) || ($size < 1)) {
4545
return ExcelError::NAN();
4646
}
47+
/** @var float */
48+
$temp = Distributions\StandardNormal::inverse(1 - $alpha / 2);
4749

48-
return Functions::scalar(Distributions\StandardNormal::inverse(1 - $alpha / 2) * $stdDev / sqrt($size));
50+
return Functions::scalar($temp * $stdDev / sqrt($size));
4951
}
5052
}

src/PhpSpreadsheet/Calculation/Statistical/Distributions/Binomial.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ public static function distribution($value, $trials, $probability, $cumulative)
5656
if ($cumulative) {
5757
return self::calculateCumulativeBinomial($value, $trials, $probability);
5858
}
59+
/** @var float */
60+
$comb = Combinations::withoutRepetition($trials, $value);
5961

60-
return Combinations::withoutRepetition($trials, $value) * $probability ** $value
62+
return $comb * $probability ** $value
6163
* (1 - $probability) ** ($trials - $value);
6264
}
6365

@@ -107,7 +109,9 @@ public static function range($trials, $probability, $successes, $limit = null)
107109

108110
$summer = 0;
109111
for ($i = $successes; $i <= $limit; ++$i) {
110-
$summer += Combinations::withoutRepetition($trials, $i) * $probability ** $i
112+
/** @var float */
113+
$comb = Combinations::withoutRepetition($trials, $i);
114+
$summer += $comb * $probability ** $i
111115
* (1 - $probability) ** ($trials - $i);
112116
}
113117

@@ -159,8 +163,10 @@ public static function negative($failures, $successes, $probability)
159163
return ExcelError::NAN();
160164
}
161165
}
166+
/** @var float */
167+
$comb = Combinations::withoutRepetition($failures + $successes - 1, $successes - 1);
162168

163-
return (Combinations::withoutRepetition($failures + $successes - 1, $successes - 1))
169+
return $comb
164170
* ($probability ** $successes) * ((1 - $probability) ** $failures);
165171
}
166172

@@ -220,7 +226,9 @@ private static function calculateCumulativeBinomial(int $value, int $trials, flo
220226
{
221227
$summer = 0;
222228
for ($i = 0; $i <= $value; ++$i) {
223-
$summer += Combinations::withoutRepetition($trials, $i) * $probability ** $i
229+
/** @var float */
230+
$comb = Combinations::withoutRepetition($trials, $i);
231+
$summer += $comb * $probability ** $i
224232
* (1 - $probability) ** ($trials - $i);
225233
}
226234

src/PhpSpreadsheet/Calculation/Statistical/Distributions/ChiSquared.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ private static function gammp($n, $x)
281281
// Relative error controlled by the eps parameter
282282
private static function gser($n, $x)
283283
{
284+
/** @var float */
284285
$gln = Gamma::ln($n / 2);
285286
$a = 0.5 * $n;
286287
$ap = $a;
@@ -304,6 +305,7 @@ private static function gser($n, $x)
304305
// Relative error controlled by the eps parameter
305306
private static function gcf($n, $x)
306307
{
308+
/** @var float */
307309
$gln = Gamma::ln($n / 2);
308310
$a = 0.5 * $n;
309311
$b = $x + 1 - $a;

src/PhpSpreadsheet/Calculation/Statistical/Distributions/LogNormal.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ public static function inverse($probability, $mean, $stdDev)
131131
if ($stdDev <= 0) {
132132
return ExcelError::NAN();
133133
}
134+
/** @var float */
135+
$inverse = StandardNormal::inverse($probability);
134136

135-
return exp($mean + $stdDev * StandardNormal::inverse($probability));
137+
return exp($mean + $stdDev * $inverse);
136138
}
137139
}

src/PhpSpreadsheet/Calculation/Statistical/Distributions/Poisson.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ public static function distribution($value, $mean, $cumulative)
5151
$summer = 0;
5252
$floor = floor($value);
5353
for ($i = 0; $i <= $floor; ++$i) {
54-
$summer += $mean ** $i / MathTrig\Factorial::fact($i);
54+
/** @var float */
55+
$fact = MathTrig\Factorial::fact($i);
56+
$summer += $mean ** $i / $fact;
5557
}
5658

5759
return exp(0 - $mean) * $summer;
5860
}
61+
/** @var float */
62+
$fact = MathTrig\Factorial::fact($value);
5963

60-
return (exp(0 - $mean) * $mean ** $value) / MathTrig\Factorial::fact($value);
64+
return (exp(0 - $mean) * $mean ** $value) / $fact;
6165
}
6266
}

0 commit comments

Comments
 (0)