Skip to content

Commit b300e18

Browse files
author
Mark Baker
authored
Merge pull request #2582 from PHPOffice/Issue-2551_Array-ready-Functions-Text-Phase-2
Issue 2551 - Array-ready Functions - Text Phase 2
2 parents 6a2905a + b3ff2e3 commit b300e18

File tree

16 files changed

+369
-23
lines changed

16 files changed

+369
-23
lines changed

phpstan-baseline.neon

+6-1
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,7 @@ parameters:
19461946
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
19471947

19481948
-
1949-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Format\\:\\:VALUE\\(\\) should return DateTimeInterface\\|float\\|int\\|string but returns mixed\\.$#"
1949+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Format\\:\\:VALUE\\(\\) should return array\\|DateTimeInterface\\|float\\|int\\|string but returns mixed\\.$#"
19501950
count: 2
19511951
path: src/PhpSpreadsheet/Calculation/TextData/Format.php
19521952

@@ -1990,6 +1990,11 @@ parameters:
19901990
count: 1
19911991
path: src/PhpSpreadsheet/Calculation/TextData/Helpers.php
19921992

1993+
-
1994+
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Replace\\:\\:substitute\\(\\) should return array\\|string but returns mixed\\.$#"
1995+
count: 1
1996+
path: src/PhpSpreadsheet/Calculation/TextData/Replace.php
1997+
19931998
-
19941999
message: "#^Variable \\$value on left side of \\?\\? always exists and is not nullable\\.$#"
19952000
count: 1

src/PhpSpreadsheet/Calculation/Financial/Dollar.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ class Dollar
1818
* The format used is $#,##0.00_);($#,##0.00)..
1919
*
2020
* @param mixed $number The value to format, or can be an array of numbers
21+
* Or can be an array of values
2122
* @param mixed $precision The number of digits to display to the right of the decimal point (as an integer).
2223
* If precision is negative, number is rounded to the left of the decimal point.
2324
* If you omit precision, it is assumed to be 2
2425
* Or can be an array of precision values
26+
*
27+
* @return array|string
28+
* If an array of values is passed for either of the arguments, then the returned result
29+
* will also be an array with matching dimensions
2530
*/
26-
public static function format($number, $precision = 2): string
31+
public static function format($number, $precision = 2)
2732
{
2833
return Format::DOLLAR($number, $precision);
2934
}

src/PhpSpreadsheet/Calculation/TextData.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static function CONCATENATE(...$args)
102102
* If decimals is negative, number is rounded to the left of the decimal point.
103103
* If you omit decimals, it is assumed to be 2
104104
*
105-
* @return string
105+
* @return array|string
106106
*/
107107
public static function DOLLAR($value = 0, $decimals = 2)
108108
{
@@ -156,7 +156,7 @@ public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1)
156156
* @param int $decimals
157157
* @param bool $no_commas
158158
*
159-
* @return string
159+
* @return array|string
160160
*/
161161
public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false)
162162
{
@@ -224,7 +224,7 @@ public static function RIGHT($value = '', $chars = 1)
224224
*
225225
* @param string $value Value
226226
*
227-
* @return int
227+
* @return array|int
228228
*/
229229
public static function STRINGLENGTH($value = '')
230230
{
@@ -297,7 +297,7 @@ public static function PROPERCASE($mixedCaseString)
297297
* @param int $chars Number of characters
298298
* @param string $newText String to replace in defined position
299299
*
300-
* @return string
300+
* @return array|string
301301
*/
302302
public static function REPLACE($oldText, $start, $chars, $newText)
303303
{
@@ -316,7 +316,7 @@ public static function REPLACE($oldText, $start, $chars, $newText)
316316
* @param string $toText To Value
317317
* @param int $instance Instance Number
318318
*
319-
* @return string
319+
* @return array|string
320320
*/
321321
public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0)
322322
{
@@ -332,7 +332,7 @@ public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $ins
332332
*
333333
* @param mixed $testValue Value to check
334334
*
335-
* @return null|string
335+
* @return null|array|string
336336
*/
337337
public static function RETURNSTRING($testValue = '')
338338
{
@@ -349,7 +349,7 @@ public static function RETURNSTRING($testValue = '')
349349
* @param mixed $value Value to check
350350
* @param string $format Format mask to use
351351
*
352-
* @return string
352+
* @return array|string
353353
*/
354354
public static function TEXTFORMAT($value, $format)
355355
{
@@ -365,7 +365,7 @@ public static function TEXTFORMAT($value, $format)
365365
*
366366
* @param mixed $value Value to check
367367
*
368-
* @return DateTimeInterface|float|int|string A string if arguments are invalid
368+
* @return array|DateTimeInterface|float|int|string A string if arguments are invalid
369369
*/
370370
public static function VALUE($value = '')
371371
{
@@ -383,7 +383,7 @@ public static function VALUE($value = '')
383383
* @param string $decimalSeparator decimal separator, defaults to locale defined value
384384
* @param string $groupSeparator group/thosands separator, defaults to locale defined value
385385
*
386-
* @return float|string
386+
* @return array|float|string
387387
*/
388388
public static function NUMBERVALUE($value = '', $decimalSeparator = null, $groupSeparator = null)
389389
{
@@ -402,7 +402,7 @@ public static function NUMBERVALUE($value = '', $decimalSeparator = null, $group
402402
* @param mixed $value1
403403
* @param mixed $value2
404404
*
405-
* @return bool
405+
* @return array|bool
406406
*/
407407
public static function EXACT($value1, $value2)
408408
{

src/PhpSpreadsheet/Calculation/TextData/Format.php

+53-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
44

55
use DateTimeInterface;
6+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
67
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
78
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
89
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
@@ -13,19 +14,31 @@
1314

1415
class Format
1516
{
17+
use ArrayEnabled;
18+
1619
/**
1720
* DOLLAR.
1821
*
1922
* This function converts a number to text using currency format, with the decimals rounded to the specified place.
2023
* The format used is $#,##0.00_);($#,##0.00)..
2124
*
2225
* @param mixed $value The value to format
26+
* Or can be an array of values
2327
* @param mixed $decimals The number of digits to display to the right of the decimal point (as an integer).
2428
* If decimals is negative, number is rounded to the left of the decimal point.
2529
* If you omit decimals, it is assumed to be 2
30+
* Or can be an array of values
31+
*
32+
* @return array|string
33+
* If an array of values is passed for either of the arguments, then the returned result
34+
* will also be an array with matching dimensions
2635
*/
27-
public static function DOLLAR($value = 0, $decimals = 2): string
36+
public static function DOLLAR($value = 0, $decimals = 2)
2837
{
38+
if (is_array($value) || is_array($decimals)) {
39+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $decimals);
40+
}
41+
2942
try {
3043
$value = Helpers::extractFloat($value);
3144
$decimals = Helpers::extractInt($decimals, -100, 0, true);
@@ -52,11 +65,22 @@ public static function DOLLAR($value = 0, $decimals = 2): string
5265
* FIXED.
5366
*
5467
* @param mixed $value The value to format
68+
* Or can be an array of values
5569
* @param mixed $decimals Integer value for the number of decimal places that should be formatted
70+
* Or can be an array of values
5671
* @param mixed $noCommas Boolean value indicating whether the value should have thousands separators or not
72+
* Or can be an array of values
73+
*
74+
* @return array|string
75+
* If an array of values is passed for either of the arguments, then the returned result
76+
* will also be an array with matching dimensions
5777
*/
58-
public static function FIXEDFORMAT($value, $decimals = 2, $noCommas = false): string
78+
public static function FIXEDFORMAT($value, $decimals = 2, $noCommas = false)
5979
{
80+
if (is_array($value) || is_array($decimals) || is_array($noCommas)) {
81+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $decimals, $noCommas);
82+
}
83+
6084
try {
6185
$value = Helpers::extractFloat($value);
6286
$decimals = Helpers::extractInt($decimals, -100, 0, true);
@@ -85,10 +109,20 @@ public static function FIXEDFORMAT($value, $decimals = 2, $noCommas = false): st
85109
* TEXT.
86110
*
87111
* @param mixed $value The value to format
112+
* Or can be an array of values
88113
* @param mixed $format A string with the Format mask that should be used
114+
* Or can be an array of values
115+
*
116+
* @return array|string
117+
* If an array of values is passed for either of the arguments, then the returned result
118+
* will also be an array with matching dimensions
89119
*/
90-
public static function TEXTFORMAT($value, $format): string
120+
public static function TEXTFORMAT($value, $format)
91121
{
122+
if (is_array($value) || is_array($format)) {
123+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $format);
124+
}
125+
92126
$value = Helpers::extractString($value);
93127
$format = Helpers::extractString($format);
94128

@@ -122,11 +156,18 @@ private static function convertValue($value)
122156
* VALUE.
123157
*
124158
* @param mixed $value Value to check
159+
* Or can be an array of values
125160
*
126-
* @return DateTimeInterface|float|int|string A string if arguments are invalid
161+
* @return array|DateTimeInterface|float|int|string A string if arguments are invalid
162+
* If an array of values is passed for the argument, then the returned result
163+
* will also be an array with matching dimensions
127164
*/
128165
public static function VALUE($value = '')
129166
{
167+
if (is_array($value)) {
168+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
169+
}
170+
130171
try {
131172
$value = self::convertValue($value);
132173
} catch (CalcExp $e) {
@@ -191,13 +232,20 @@ private static function getGroupSeparator($groupSeparator): string
191232
* NUMBERVALUE.
192233
*
193234
* @param mixed $value The value to format
235+
* Or can be an array of values
194236
* @param mixed $decimalSeparator A string with the decimal separator to use, defaults to locale defined value
237+
* Or can be an array of values
195238
* @param mixed $groupSeparator A string with the group/thousands separator to use, defaults to locale defined value
239+
* Or can be an array of values
196240
*
197-
* @return float|string
241+
* @return array|float|string
198242
*/
199243
public static function NUMBERVALUE($value = '', $decimalSeparator = null, $groupSeparator = null)
200244
{
245+
if (is_array($value) || is_array($decimalSeparator) || is_array($groupSeparator)) {
246+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $decimalSeparator, $groupSeparator);
247+
}
248+
201249
try {
202250
$value = self::convertValue($value);
203251
$decimalSeparator = self::getDecimalSeparator($decimalSeparator);

src/PhpSpreadsheet/Calculation/TextData/Replace.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,36 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class Replace
910
{
11+
use ArrayEnabled;
12+
1013
/**
1114
* REPLACE.
1215
*
1316
* @param mixed $oldText The text string value to modify
17+
* Or can be an array of values
1418
* @param mixed $start Integer offset for start character of the replacement
19+
* Or can be an array of values
1520
* @param mixed $chars Integer number of characters to replace from the start offset
21+
* Or can be an array of values
1622
* @param mixed $newText String to replace in the defined position
23+
* Or can be an array of values
24+
*
25+
* @return array|string
26+
* If an array of values is passed for either of the arguments, then the returned result
27+
* will also be an array with matching dimensions
1728
*/
18-
public static function replace($oldText, $start, $chars, $newText): string
29+
public static function replace($oldText, $start, $chars, $newText)
1930
{
31+
if (is_array($oldText) || is_array($start) || is_array($chars) || is_array($newText)) {
32+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $oldText, $start, $chars, $newText);
33+
}
34+
2035
try {
2136
$start = Helpers::extractInt($start, 1, 0, true);
2237
$chars = Helpers::extractInt($chars, 0, 0, true);
@@ -36,12 +51,24 @@ public static function replace($oldText, $start, $chars, $newText): string
3651
* SUBSTITUTE.
3752
*
3853
* @param mixed $text The text string value to modify
54+
* Or can be an array of values
3955
* @param mixed $fromText The string value that we want to replace in $text
56+
* Or can be an array of values
4057
* @param mixed $toText The string value that we want to replace with in $text
58+
* Or can be an array of values
4159
* @param mixed $instance Integer instance Number for the occurrence of frmText to change
60+
* Or can be an array of values
61+
*
62+
* @return array|string
63+
* If an array of values is passed for either of the arguments, then the returned result
64+
* will also be an array with matching dimensions
4265
*/
43-
public static function substitute($text = '', $fromText = '', $toText = '', $instance = null): string
66+
public static function substitute($text = '', $fromText = '', $toText = '', $instance = null)
4467
{
68+
if (is_array($text) || is_array($fromText) || is_array($toText) || is_array($instance)) {
69+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $text, $fromText, $toText, $instance);
70+
}
71+
4572
try {
4673
$text = Helpers::extractString($text);
4774
$fromText = Helpers::extractString($fromText);
@@ -71,7 +98,7 @@ public static function substitute($text = '', $fromText = '', $toText = '', $ins
7198
}
7299

73100
if ($pos !== false) {
74-
return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText);
101+
return Functions::scalar(self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText));
75102
}
76103

77104
return $text;

0 commit comments

Comments
 (0)