Skip to content

Commit 6a2905a

Browse files
author
Mark Baker
authored
Merge pull request #2580 from PHPOffice/Issue-2551-Array-Ready-Function-Financial
Issue 2551 - array ready functions - Text (phase #1)
2 parents ad5532e + d247344 commit 6a2905a

36 files changed

+639
-113
lines changed

phpstan-baseline.neon

+16-26
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,12 @@ parameters:
927927

928928
-
929929
message: "#^Parameter \\#1 \\$year of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Helpers\\:\\:daysPerYear\\(\\) expects int\\|string, array\\|int\\|string given\\.$#"
930-
count: 3
930+
count: 1
931+
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php
932+
933+
-
934+
message: "#^Parameter \\#1 \\$year of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Helpers\\:\\:daysPerYear\\(\\) expects int\\|string, mixed given\\.$#"
935+
count: 2
931936
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php
932937

933938
-
@@ -961,12 +966,12 @@ parameters:
961966
path: src/PhpSpreadsheet/Calculation/Financial/Depreciation.php
962967

963968
-
964-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:atMaturity\\(\\) should return float\\|string but returns array\\|string\\.$#"
969+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:atMaturity\\(\\) should return float\\|string but returns mixed\\.$#"
965970
count: 1
966971
path: src/PhpSpreadsheet/Calculation/Financial/Securities/AccruedInterest.php
967972

968973
-
969-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:periodic\\(\\) should return float\\|string but returns array\\|string\\.$#"
974+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:periodic\\(\\) should return float\\|string but returns mixed\\.$#"
970975
count: 2
971976
path: src/PhpSpreadsheet/Calculation/Financial/Securities/AccruedInterest.php
972977

@@ -976,12 +981,12 @@ parameters:
976981
path: src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php
977982

978983
-
979-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Price\\:\\:priceAtMaturity\\(\\) should return float\\|string but returns array\\|string\\.$#"
980-
count: 3
984+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Price\\:\\:priceAtMaturity\\(\\) should return float\\|string but returns mixed\\.$#"
985+
count: 4
981986
path: src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php
982987

983988
-
984-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Price\\:\\:priceDiscounted\\(\\) should return float\\|string but returns array\\|string\\.$#"
989+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Price\\:\\:priceDiscounted\\(\\) should return float\\|string but returns mixed\\.$#"
985990
count: 1
986991
path: src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php
987992

@@ -996,22 +1001,22 @@ parameters:
9961001
path: src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php
9971002

9981003
-
999-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Rates\\:\\:discount\\(\\) should return float\\|string but returns array\\|string\\.$#"
1004+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Rates\\:\\:discount\\(\\) should return float\\|string but returns mixed\\.$#"
10001005
count: 1
10011006
path: src/PhpSpreadsheet/Calculation/Financial/Securities/Rates.php
10021007

10031008
-
1004-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Rates\\:\\:interest\\(\\) should return float\\|string but returns array\\|string\\.$#"
1009+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Rates\\:\\:interest\\(\\) should return float\\|string but returns mixed\\.$#"
10051010
count: 1
10061011
path: src/PhpSpreadsheet/Calculation/Financial/Securities/Rates.php
10071012

10081013
-
1009-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Yields\\:\\:yieldAtMaturity\\(\\) should return float\\|string but returns array\\|string\\.$#"
1014+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Yields\\:\\:yieldAtMaturity\\(\\) should return float\\|string but returns mixed\\.$#"
10101015
count: 3
10111016
path: src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php
10121017

10131018
-
1014-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Yields\\:\\:yieldDiscounted\\(\\) should return float\\|string but returns array\\|string\\.$#"
1019+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Yields\\:\\:yieldDiscounted\\(\\) should return float\\|string but returns mixed\\.$#"
10151020
count: 1
10161021
path: src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php
10171022

@@ -1021,7 +1026,7 @@ parameters:
10211026
path: src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php
10221027

10231028
-
1024-
message: "#^Parameter \\#1 \\$year of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Helpers\\:\\:daysPerYear\\(\\) expects int\\|string, array\\|int\\|string given\\.$#"
1029+
message: "#^Parameter \\#1 \\$year of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Helpers\\:\\:daysPerYear\\(\\) expects int\\|string, mixed given\\.$#"
10251030
count: 3
10261031
path: src/PhpSpreadsheet/Calculation/Financial/TreasuryBill.php
10271032

@@ -1925,21 +1930,6 @@ parameters:
19251930
count: 1
19261931
path: src/PhpSpreadsheet/Calculation/TextData.php
19271932

1928-
-
1929-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:SEARCHINSENSITIVE\\(\\) should return string but returns int\\|string\\.$#"
1930-
count: 1
1931-
path: src/PhpSpreadsheet/Calculation/TextData.php
1932-
1933-
-
1934-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:SEARCHSENSITIVE\\(\\) should return string but returns int\\|string\\.$#"
1935-
count: 1
1936-
path: src/PhpSpreadsheet/Calculation/TextData.php
1937-
1938-
-
1939-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:TRIMNONPRINTABLE\\(\\) should return string but returns string\\|null\\.$#"
1940-
count: 1
1941-
path: src/PhpSpreadsheet/Calculation/TextData.php
1942-
19431933
-
19441934
message: "#^Parameter \\#1 \\$glue of function implode expects string, mixed given\\.$#"
19451935
count: 1

src/PhpSpreadsheet/Calculation/DateTimeExcel/NetworkDays.php

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class NetworkDays
3030
* @param mixed $dateArgs An array of dates (such as holidays) to exclude from the calculation
3131
*
3232
* @return array|int|string Interval between the dates
33+
* If an array of values is passed for the $startDate or $endDate arguments, then the returned result
34+
* will also be an array with matching dimensions
3335
*/
3436
public static function count($startDate, $endDate, ...$dateArgs)
3537
{

src/PhpSpreadsheet/Calculation/DateTimeExcel/YearFrac.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class YearFrac
2626
*
2727
* @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer),
2828
* PHP DateTime object, or a standard date string
29-
* Or can be an array of methods
29+
* Or can be an array of values
3030
* @param mixed $endDate Excel date serial value (float), PHP date timestamp (integer),
3131
* PHP DateTime object, or a standard date string
3232
* Or can be an array of methods

src/PhpSpreadsheet/Calculation/Financial.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,10 @@ public static function DISC($settlement, $maturity, $price, $redemption, $basis
587587
* @see Financial\Dollar::decimal()
588588
* Use the decimal() method in the Financial\Dollar class instead
589589
*
590-
* @param float $fractional_dollar Fractional Dollar
591-
* @param int $fraction Fraction
590+
* @param array|float $fractional_dollar Fractional Dollar
591+
* @param array|int $fraction Fraction
592592
*
593-
* @return float|string
593+
* @return array|float|string
594594
*/
595595
public static function DOLLARDE($fractional_dollar = null, $fraction = 0)
596596
{
@@ -612,10 +612,10 @@ public static function DOLLARDE($fractional_dollar = null, $fraction = 0)
612612
* @see Financial\Dollar::fractional()
613613
* Use the fractional() method in the Financial\Dollar class instead
614614
*
615-
* @param float $decimal_dollar Decimal Dollar
616-
* @param int $fraction Fraction
615+
* @param array|float $decimal_dollar Decimal Dollar
616+
* @param array|int $fraction Fraction
617617
*
618-
* @return float|string
618+
* @return array|float|string
619619
*/
620620
public static function DOLLARFR($decimal_dollar = null, $fraction = 0)
621621
{

src/PhpSpreadsheet/Calculation/Financial/Amortization.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public static function AMORLINC(
168168

169169
if (
170170
($basis == FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL) &&
171-
($yearFrac < 1) && (DateTimeExcel\Helpers::isLeapYear($purchasedYear))
171+
($yearFrac < 1) && (Functions::scalar(DateTimeExcel\Helpers::isLeapYear($purchasedYear)))
172172
) {
173173
$yearFrac *= 365 / 366;
174174
}

src/PhpSpreadsheet/Calculation/Financial/Coupons.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static function COUPDAYBS(
6464
return $e->getMessage();
6565
}
6666

67-
$daysPerYear = Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis);
67+
$daysPerYear = Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis);
6868
if (is_string($daysPerYear)) {
6969
return Functions::VALUE();
7070
}
@@ -134,7 +134,7 @@ public static function COUPDAYS(
134134
case FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL:
135135
// Actual/actual
136136
if ($frequency == FinancialConstants::FREQUENCY_ANNUAL) {
137-
$daysPerYear = (int) Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis);
137+
$daysPerYear = (int) Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis);
138138

139139
return $daysPerYear / $frequency;
140140
}

src/PhpSpreadsheet/Calculation/Financial/Dollar.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78
use PhpOffice\PhpSpreadsheet\Calculation\TextData\Format;
89

910
class Dollar
1011
{
12+
use ArrayEnabled;
13+
1114
/**
1215
* DOLLAR.
1316
*
1417
* This function converts a number to text using currency format, with the decimals rounded to the specified place.
1518
* The format used is $#,##0.00_);($#,##0.00)..
1619
*
17-
* @param mixed $number The value to format
20+
* @param mixed $number The value to format, or can be an array of numbers
1821
* @param mixed $precision The number of digits to display to the right of the decimal point (as an integer).
1922
* If precision is negative, number is rounded to the left of the decimal point.
2023
* If you omit precision, it is assumed to be 2
24+
* Or can be an array of precision values
2125
*/
2226
public static function format($number, $precision = 2): string
2327
{
@@ -35,12 +39,18 @@ public static function format($number, $precision = 2): string
3539
* DOLLARDE(fractional_dollar,fraction)
3640
*
3741
* @param mixed $fractionalDollar Fractional Dollar
42+
* Or can be an array of values
3843
* @param mixed $fraction Fraction
44+
* Or can be an array of values
3945
*
40-
* @return float|string
46+
* @return array|float|string
4147
*/
4248
public static function decimal($fractionalDollar = null, $fraction = 0)
4349
{
50+
if (is_array($fractionalDollar) || is_array($fraction)) {
51+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $fractionalDollar, $fraction);
52+
}
53+
4454
try {
4555
$fractionalDollar = FinancialValidations::validateFloat(
4656
Functions::flattenSingleValue($fractionalDollar) ?? 0.0
@@ -58,8 +68,8 @@ public static function decimal($fractionalDollar = null, $fraction = 0)
5868
return Functions::DIV0();
5969
}
6070

61-
$dollars = ($fractionalDollar < 0.0) ? ceil($fractionalDollar) : floor($fractionalDollar);
62-
$cents = fmod($fractionalDollar, 1);
71+
$dollars = ($fractionalDollar < 0) ? ceil($fractionalDollar) : floor($fractionalDollar);
72+
$cents = fmod($fractionalDollar, 1.0);
6373
$cents /= $fraction;
6474
$cents *= 10 ** ceil(log10($fraction));
6575

@@ -77,12 +87,18 @@ public static function decimal($fractionalDollar = null, $fraction = 0)
7787
* DOLLARFR(decimal_dollar,fraction)
7888
*
7989
* @param mixed $decimalDollar Decimal Dollar
90+
* Or can be an array of values
8091
* @param mixed $fraction Fraction
92+
* Or can be an array of values
8193
*
82-
* @return float|string
94+
* @return array|float|string
8395
*/
8496
public static function fractional($decimalDollar = null, $fraction = 0)
8597
{
98+
if (is_array($decimalDollar) || is_array($fraction)) {
99+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $decimalDollar, $fraction);
100+
}
101+
86102
try {
87103
$decimalDollar = FinancialValidations::validateFloat(
88104
Functions::flattenSingleValue($decimalDollar) ?? 0.0

src/PhpSpreadsheet/Calculation/Financial/Securities/AccruedInterest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ public static function periodic(
7878
return $e->getMessage();
7979
}
8080

81-
$daysBetweenIssueAndSettlement = YearFrac::fraction($issue, $settlement, $basis);
81+
$daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis));
8282
if (!is_numeric($daysBetweenIssueAndSettlement)) {
8383
// return date error
8484
return $daysBetweenIssueAndSettlement;
8585
}
86-
$daysBetweenFirstInterestAndSettlement = YearFrac::fraction($firstInterest, $settlement, $basis);
86+
$daysBetweenFirstInterestAndSettlement = Functions::scalar(YearFrac::fraction($firstInterest, $settlement, $basis));
8787
if (!is_numeric($daysBetweenFirstInterestAndSettlement)) {
8888
// return date error
8989
return $daysBetweenFirstInterestAndSettlement;
@@ -140,7 +140,7 @@ public static function atMaturity(
140140
return $e->getMessage();
141141
}
142142

143-
$daysBetweenIssueAndSettlement = YearFrac::fraction($issue, $settlement, $basis);
143+
$daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis));
144144
if (!is_numeric($daysBetweenIssueAndSettlement)) {
145145
// return date error
146146
return $daysBetweenIssueAndSettlement;

src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static function priceDiscounted(
134134
return $e->getMessage();
135135
}
136136

137-
$daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
137+
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis));
138138
if (!is_numeric($daysBetweenSettlementAndMaturity)) {
139139
// return date error
140140
return $daysBetweenSettlementAndMaturity;
@@ -194,23 +194,23 @@ public static function priceAtMaturity(
194194
return $e->getMessage();
195195
}
196196

197-
$daysPerYear = Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis);
197+
$daysPerYear = Functions::scalar(Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis));
198198
if (!is_numeric($daysPerYear)) {
199199
return $daysPerYear;
200200
}
201-
$daysBetweenIssueAndSettlement = DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis);
201+
$daysBetweenIssueAndSettlement = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis));
202202
if (!is_numeric($daysBetweenIssueAndSettlement)) {
203203
// return date error
204204
return $daysBetweenIssueAndSettlement;
205205
}
206206
$daysBetweenIssueAndSettlement *= $daysPerYear;
207-
$daysBetweenIssueAndMaturity = DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis);
207+
$daysBetweenIssueAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis));
208208
if (!is_numeric($daysBetweenIssueAndMaturity)) {
209209
// return date error
210210
return $daysBetweenIssueAndMaturity;
211211
}
212212
$daysBetweenIssueAndMaturity *= $daysPerYear;
213-
$daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
213+
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis));
214214
if (!is_numeric($daysBetweenSettlementAndMaturity)) {
215215
// return date error
216216
return $daysBetweenSettlementAndMaturity;

src/PhpSpreadsheet/Calculation/Financial/Securities/Rates.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function discount(
6363
return Functions::NAN();
6464
}
6565

66-
$daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
66+
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis));
6767
if (!is_numeric($daysBetweenSettlementAndMaturity)) {
6868
// return date error
6969
return $daysBetweenSettlementAndMaturity;
@@ -126,7 +126,7 @@ public static function interest(
126126
return Functions::NAN();
127127
}
128128

129-
$daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
129+
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis));
130130
if (!is_numeric($daysBetweenSettlementAndMaturity)) {
131131
// return date error
132132
return $daysBetweenSettlementAndMaturity;

src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function yieldDiscounted(
6161
if (!is_numeric($daysPerYear)) {
6262
return $daysPerYear;
6363
}
64-
$daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
64+
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis));
6565
if (!is_numeric($daysBetweenSettlementAndMaturity)) {
6666
// return date error
6767
return $daysBetweenSettlementAndMaturity;
@@ -126,19 +126,19 @@ public static function yieldAtMaturity(
126126
if (!is_numeric($daysPerYear)) {
127127
return $daysPerYear;
128128
}
129-
$daysBetweenIssueAndSettlement = DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis);
129+
$daysBetweenIssueAndSettlement = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis));
130130
if (!is_numeric($daysBetweenIssueAndSettlement)) {
131131
// return date error
132132
return $daysBetweenIssueAndSettlement;
133133
}
134134
$daysBetweenIssueAndSettlement *= $daysPerYear;
135-
$daysBetweenIssueAndMaturity = DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis);
135+
$daysBetweenIssueAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis));
136136
if (!is_numeric($daysBetweenIssueAndMaturity)) {
137137
// return date error
138138
return $daysBetweenIssueAndMaturity;
139139
}
140140
$daysBetweenIssueAndMaturity *= $daysPerYear;
141-
$daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
141+
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis));
142142
if (!is_numeric($daysBetweenSettlementAndMaturity)) {
143143
// return date error
144144
return $daysBetweenSettlementAndMaturity;

0 commit comments

Comments
 (0)