Skip to content

Commit

Permalink
Fix PHP8.2 str_split function returns empty arrays for empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Feb 8, 2023
1 parent cf33247 commit 4a55e40
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/Engineering/ConvertHex.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function toDecimal($value)
}

$binX = '';
foreach (str_split($value) as $char) {
foreach (mb_str_split($value) as $char) {
$binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
}
if (strlen($binX) == 40 && $binX[0] == '1') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function toDecimal($value)
}

$binX = '';
foreach (str_split($value) as $char) {
foreach (mb_str_split($value) as $char) {
$binX .= str_pad(decbin((int) $char), 3, '0', STR_PAD_LEFT);
}
if (strlen($binX) == 30 && $binX[0] == '1') {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static function mollifyScrutinizer($value): array

private static function strSplit(string $roman): array
{
$rslt = str_split($roman);
$rslt = mb_str_split($roman);

return self::mollifyScrutinizer($rslt);
}
Expand Down
12 changes: 5 additions & 7 deletions src/PhpSpreadsheet/Reader/Csv/Delimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ protected function countPotentialDelimiters(): void

protected function countDelimiterValues(string $line, array $delimiterKeys): void
{
$splitString = str_split($line, 1);
if (is_array($splitString)) {
$distribution = array_count_values($splitString);
$countLine = array_intersect_key($distribution, $delimiterKeys);
$splitString = mb_str_split($line, 1);
$distribution = array_count_values($splitString);
$countLine = array_intersect_key($distribution, $delimiterKeys);

foreach (self::POTENTIAL_DELIMETERS as $delimiter) {
$this->counts[$delimiter][] = $countLine[$delimiter] ?? 0;
}
foreach (self::POTENTIAL_DELIMETERS as $delimiter) {
$this->counts[$delimiter][] = $countLine[$delimiter] ?? 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Reader/Security/XmlScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function scan($xml)
$xml = $this->toUtf8($xml);

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

if (preg_match($pattern, $xml)) {
throw new Reader\Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(?string $id = null, string $cfRule = self::CONDITION

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

foreach ($chars as $i => $char) {
if ($char === 'x') {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@ private static function setLowercaseCallback(array $matches): string

private static function escapeQuotesCallback(array $matches): string
{
return '\\' . implode('\\', /** @scrutinizer ignore-type */ str_split($matches[1]));
return '\\' . implode('\\', /** @scrutinizer ignore-type */ mb_str_split($matches[1]));
}
}
1 change: 1 addition & 0 deletions tests/data/Calculation/Engineering/HEX2DEC.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
[-2147483648, '"ff80000000"'],
[2147483648, '"80000000"'],
[2147483647, '"7fffffff"'],
[0, '""'],
];
1 change: 1 addition & 0 deletions tests/data/Calculation/Engineering/OCT2DEC.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
['#NUM!', '"37777777770"'], // too many digits
[536870911, '"3777777777"'], // highest positive
[-536870912, '"4000000000"'], // lowest negative
['0', '""'],
];
4 changes: 4 additions & 0 deletions tests/data/Calculation/MathTrig/ARABIC.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@
'#VALUE!',
'WRONG',
],
[
0,
'',
],
];

0 comments on commit 4a55e40

Please sign in to comment.