Skip to content

Commit 4a55e40

Browse files
committed
Fix PHP8.2 str_split function returns empty arrays for empty strings
1 parent cf33247 commit 4a55e40

File tree

10 files changed

+17
-13
lines changed

10 files changed

+17
-13
lines changed

src/PhpSpreadsheet/Calculation/Engineering/ConvertHex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static function toDecimal($value)
9696
}
9797

9898
$binX = '';
99-
foreach (str_split($value) as $char) {
99+
foreach (mb_str_split($value) as $char) {
100100
$binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
101101
}
102102
if (strlen($binX) == 40 && $binX[0] == '1') {

src/PhpSpreadsheet/Calculation/Engineering/ConvertOctal.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static function toDecimal($value)
9696
}
9797

9898
$binX = '';
99-
foreach (str_split($value) as $char) {
99+
foreach (mb_str_split($value) as $char) {
100100
$binX .= str_pad(decbin((int) $char), 3, '0', STR_PAD_LEFT);
101101
}
102102
if (strlen($binX) == 30 && $binX[0] == '1') {

src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static function mollifyScrutinizer($value): array
6060

6161
private static function strSplit(string $roman): array
6262
{
63-
$rslt = str_split($roman);
63+
$rslt = mb_str_split($roman);
6464

6565
return self::mollifyScrutinizer($rslt);
6666
}

src/PhpSpreadsheet/Reader/Csv/Delimiter.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,12 @@ protected function countPotentialDelimiters(): void
6060

6161
protected function countDelimiterValues(string $line, array $delimiterKeys): void
6262
{
63-
$splitString = str_split($line, 1);
64-
if (is_array($splitString)) {
65-
$distribution = array_count_values($splitString);
66-
$countLine = array_intersect_key($distribution, $delimiterKeys);
63+
$splitString = mb_str_split($line, 1);
64+
$distribution = array_count_values($splitString);
65+
$countLine = array_intersect_key($distribution, $delimiterKeys);
6766

68-
foreach (self::POTENTIAL_DELIMETERS as $delimiter) {
69-
$this->counts[$delimiter][] = $countLine[$delimiter] ?? 0;
70-
}
67+
foreach (self::POTENTIAL_DELIMETERS as $delimiter) {
68+
$this->counts[$delimiter][] = $countLine[$delimiter] ?? 0;
7169
}
7270
}
7371

src/PhpSpreadsheet/Reader/Security/XmlScanner.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function scan($xml)
145145
$xml = $this->toUtf8($xml);
146146

147147
// Don't rely purely on libxml_disable_entity_loader()
148-
$pattern = '/\\0?' . implode('\\0?', /** @scrutinizer ignore-type */ str_split($this->pattern)) . '\\0?/';
148+
$pattern = '/\\0?' . implode('\\0?', /** @scrutinizer ignore-type */ mb_str_split($this->pattern)) . '\\0?/';
149149

150150
if (preg_match($pattern, $xml)) {
151151
throw new Reader\Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');

src/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalFormattingRuleExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(?string $id = null, string $cfRule = self::CONDITION
4040

4141
private function generateUuid(): string
4242
{
43-
$chars = str_split('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx');
43+
$chars = mb_str_split('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx');
4444

4545
foreach ($chars as $i => $char) {
4646
if ($char === 'x') {

src/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,6 @@ private static function setLowercaseCallback(array $matches): string
177177

178178
private static function escapeQuotesCallback(array $matches): string
179179
{
180-
return '\\' . implode('\\', /** @scrutinizer ignore-type */ str_split($matches[1]));
180+
return '\\' . implode('\\', /** @scrutinizer ignore-type */ mb_str_split($matches[1]));
181181
}
182182
}

tests/data/Calculation/Engineering/HEX2DEC.php

+1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
[-2147483648, '"ff80000000"'],
2525
[2147483648, '"80000000"'],
2626
[2147483647, '"7fffffff"'],
27+
[0, '""'],
2728
];

tests/data/Calculation/Engineering/OCT2DEC.php

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
['#NUM!', '"37777777770"'], // too many digits
1818
[536870911, '"3777777777"'], // highest positive
1919
[-536870912, '"4000000000"'], // lowest negative
20+
['0', '""'],
2021
];

tests/data/Calculation/MathTrig/ARABIC.php

+4
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,8 @@
5757
'#VALUE!',
5858
'WRONG',
5959
],
60+
[
61+
0,
62+
'',
63+
],
6064
];

0 commit comments

Comments
 (0)