Skip to content

Commit 6c59ca8

Browse files
authored
String Increment Deprecated in Php8.5 - release390 (#4602)
1 parent e88ca06 commit 6c59ca8

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

src/PhpSpreadsheet/Shared/StringHelper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,4 +643,19 @@ public static function strlenAllowNull(?string $string): int
643643
{
644644
return strlen("$string");
645645
}
646+
647+
/**
648+
* Php introduced str_increment with Php8.3,
649+
* but didn't issue deprecation notices till 8.5.
650+
*
651+
* @codeCoverageIgnore
652+
*/
653+
public static function stringIncrement(string &$str): void
654+
{
655+
if (function_exists('str_increment')) {
656+
$str = str_increment($str);
657+
} else {
658+
++$str;
659+
}
660+
}
646661
}

src/PhpSpreadsheet/Writer/Html.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ public function generateSheetData(): string
514514
$rowData[$column] = ($sheet->getCellCollection()->has($cellAddress)) ? $cellAddress : '';
515515
}
516516
++$column;
517-
++$colStr;
517+
StringHelper::stringIncrement($colStr);
518518
}
519519
$html .= $this->generateRow($sheet, $rowData, $row - 1, $cellType);
520520
}
@@ -880,7 +880,7 @@ private function buildCssPerSheet(Worksheet $sheet, array &$css): void
880880
if ($this->shouldGenerateColumn($sheet, $colStr)) {
881881
$css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = self::DEFAULT_CELL_WIDTH_POINTS . 'pt';
882882
}
883-
++$colStr;
883+
StringHelper::stringIncrement($colStr);
884884
}
885885

886886
// col elements, loop through columnDimensions and set width

src/PhpSpreadsheet/Writer/Pdf/Dompdf.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public function save($filename, int $flags = 0): void
4444
$orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
4545

4646
// Create PDF
47+
$restoreHandler = false;
48+
if (PHP_VERSION_ID >= self::$temporaryVersionCheck) {
49+
// @codeCoverageIgnoreStart
50+
set_error_handler(self::specialErrorHandler(...));
51+
$restoreHandler = true;
52+
// @codeCoverageIgnoreEnd
53+
}
4754
$pdf = $this->createExternalWriterInstance();
4855
$pdf->setPaper($paperSize, $orientation);
4956

@@ -53,6 +60,27 @@ public function save($filename, int $flags = 0): void
5360
// Write to file
5461
fwrite($fileHandle, $pdf->output() ?? '');
5562

63+
if ($restoreHandler) {
64+
restore_error_handler(); // @codeCoverageIgnore
65+
}
5666
parent::restoreStateAfterSave();
5767
}
68+
69+
protected static int $temporaryVersionCheck = 80500;
70+
71+
/**
72+
* Temporary handler for Php8.5 waiting for Dompdf release.
73+
*
74+
* @codeCoverageIgnore
75+
*/
76+
public function specialErrorHandler(int $errno, string $errstr, string $filename, int $lineno): bool
77+
{
78+
if ($errno === E_DEPRECATED) {
79+
if (preg_match('/canonical|imagedestroy/', $errstr) === 1) {
80+
return true;
81+
}
82+
}
83+
84+
return false; // continue error handling
85+
}
5886
}

tests/bootstrap.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
function phpunit10ErrorHandler(int $errno, string $errstr, string $filename, int $lineno): bool
88
{
9+
if (strIncrement85(PHP_VERSION_ID, $errno, $errstr, $filename)) {
10+
return true; // message suppressed - stop error handling
11+
}
912
$x = error_reporting() & $errno;
1013
if (
1114
in_array(
@@ -31,6 +34,21 @@ function phpunit10ErrorHandler(int $errno, string $errstr, string $filename, int
3134
return false; // continue error handling
3235
}
3336

37+
function strIncrement85(int $version, int $errno, string $errstr, string $filename): bool
38+
{
39+
if ($version < 80500 || $errno !== E_DEPRECATED) {
40+
return false;
41+
}
42+
if (preg_match('/Increment on non-numeric string/', $errstr) === 1) {
43+
return true;
44+
}
45+
if (preg_match('/canonical/', $errstr) === 1 && preg_match('/mitoteam/', $filename) === 1) {
46+
return true;
47+
}
48+
49+
return false;
50+
}
51+
3452
if (!method_exists(PHPUnit\Framework\TestCase::class, 'setOutputCallback')) {
3553
set_error_handler('phpunit10ErrorHandler');
3654
}

0 commit comments

Comments
 (0)