Skip to content

Commit 05020a9

Browse files
committed
fix: address phpmd embedder warnings
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent a63243e commit 05020a9

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/Pdf/FilesystemPdfImageEmbedder.php

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

1010
use InvalidArgumentException;
1111

12+
/**
13+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
14+
*/
1215
final readonly class FilesystemPdfImageEmbedder implements PdfImageEmbedderInterface
1316
{
1417
public function embed(string $source): EmbeddedPdfImage
@@ -284,7 +287,7 @@ private function createImageDictionary(int $width, int $height, string $colorSpa
284287
*/
285288
private function unfilterPngScanlines(string $idat, int $height, int $rowLength, int $bytesPerPixel): array
286289
{
287-
$inflated = @gzuncompress($idat);
290+
$inflated = $this->runWithoutWarnings(static fn (): string|false => gzuncompress($idat));
288291
if (!is_string($inflated)) {
289292
throw new InvalidArgumentException('PNG image data could not be decompressed.');
290293
}
@@ -322,13 +325,13 @@ private function unfilterPngRow(
322325
): string {
323326
$row = '';
324327
$rowLength = strlen($filteredRow);
325-
$previousRowWithPadding = str_repeat("\x00", $bytesPerPixel) . $previousRow;
328+
$paddedPreviousRow = str_repeat("\x00", $bytesPerPixel) . $previousRow;
326329

327330
for ($index = 0; $index < $rowLength; $index++) {
328331
$rawByte = ord($filteredRow[$index]);
329332
$left = $index >= $bytesPerPixel ? ord($row[$index - $bytesPerPixel]) : 0;
330333
$above = ord($previousRow[$index]);
331-
$upperLeft = ord($previousRowWithPadding[$index]);
334+
$upperLeft = ord($paddedPreviousRow[$index]);
332335

333336
$decodedByte = match ($filterType) {
334337
0 => $rawByte,
@@ -408,7 +411,7 @@ private function assertReadableSource(string $source): void
408411

409412
private function readSourceContents(string $source): string
410413
{
411-
$contents = @file_get_contents($source);
414+
$contents = $this->runWithoutWarnings(static fn (): string|false => file_get_contents($source));
412415
if (!is_string($contents)) {
413416
throw new InvalidArgumentException(sprintf('Failed to read image source "%s".', $source));
414417
}
@@ -460,4 +463,15 @@ private function resolveJpegColorSpace(mixed $channels): string
460463
default => '/DeviceRGB',
461464
};
462465
}
466+
467+
private function runWithoutWarnings(callable $operation): mixed
468+
{
469+
set_error_handler(static fn (): bool => true);
470+
471+
try {
472+
return $operation();
473+
} finally {
474+
restore_error_handler();
475+
}
476+
}
463477
}

0 commit comments

Comments
 (0)