Skip to content

Commit b260011

Browse files
committed
Php8.5 Accommodations
Dompdf (temporary till 8.5-compatible release available), `chr` in Reader/MsDoc, `setAccessible` in AbstractStyleTest.
1 parent 821e0a5 commit b260011

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/PhpWord/Reader/MsDoc.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ private function readRecordSttbfFfn(): void
12181218
$char = self::getInt2d($this->data1Table, $posMem);
12191219
$posMem += 2;
12201220
if ($char > 0) {
1221-
$xszFfn .= chr($char);
1221+
$xszFfn .= mb_chr($char, 'UTF-8');
12221222
}
12231223
} while ($char != 0);
12241224
// xszAlt
@@ -1230,7 +1230,7 @@ private function readRecordSttbfFfn(): void
12301230
if ($char == 0) {
12311231
break;
12321232
}
1233-
$xszAlt .= chr($char);
1233+
$xszAlt .= mb_chr($char, 'UTF-8');
12341234
} while ($char != 0);
12351235
}
12361236
$this->arrayFonts[] = [

src/PhpWord/Writer/PDF/DomPDF.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public function save(string $filename): void
6464
$orientation = 'portrait';
6565

6666
// Create PDF
67+
$restoreHandler = false;
68+
if (PHP_VERSION_ID >= self::$temporaryVersionCheck) {
69+
// @codeCoverageIgnoreStart
70+
$errhandler = [$this, 'specialErrorHandler'];
71+
set_error_handler($errhandler); // @phpstan-ignore-line
72+
$restoreHandler = true;
73+
// @codeCoverageIgnoreEnd
74+
}
6775
$pdf = $this->createExternalWriterInstance();
6876
$pdf->setPaper(strtolower($paperSize), $orientation);
6977
$pdf->loadHtml(str_replace(PHP_EOL, '', $this->getContent()));
@@ -72,6 +80,27 @@ public function save(string $filename): void
7280
// Write to file
7381
fwrite($fileHandle, $pdf->output());
7482

83+
if ($restoreHandler) {
84+
restore_error_handler(); // @codecoverageignore
85+
}
7586
parent::restoreStateAfterSave($fileHandle);
7687
}
88+
89+
protected static int $temporaryVersionCheck = 80500;
90+
91+
/*
92+
* Temporary handler for Php8.5 waiting for Dompdf release.
93+
*
94+
* @codeCoverageIgnore
95+
*/
96+
public function specialErrorHandler(int $errno, string $errstr, string $filename, int $lineno): bool
97+
{
98+
if ($errno === E_DEPRECATED) {
99+
if (preg_match('/canonical|imagedestroy/', $errstr) === 1) {
100+
return true;
101+
}
102+
}
103+
104+
return false; // continue error handling
105+
}
77106
}

tests/PhpWordTests/Style/AbstractStyleTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public function testSetValEnumException(): void
124124
self::assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', ['z', ['a', 'b'], 'b']));
125125
}
126126

127+
protected static int $temporaryVersionCheck = 80500;
128+
127129
/**
128130
* Helper function to call protected method.
129131
*
@@ -134,7 +136,9 @@ public static function callProtectedMethod($object, $method, array $args = [])
134136
{
135137
$class = new ReflectionClass(get_class($object));
136138
$method = $class->getMethod($method);
137-
$method->setAccessible(true);
139+
if (PHP_VERSION_ID < self::$temporaryVersionCheck) {
140+
$method->setAccessible(true);
141+
}
138142

139143
return $method->invokeArgs($object, $args);
140144
}

0 commit comments

Comments
 (0)