Skip to content

Commit

Permalink
Fixes latest coding style and PHPStan issues (#563)
Browse files Browse the repository at this point in the history
* fixes coding style issues

* fixes PHPStan errors
  • Loading branch information
k00ni committed Nov 25, 2022
1 parent 988efcb commit 13a8178
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function __toString(): string
{
try {
return $this->getEncodingClass();
} catch (Exception $e) {
} catch (\Exception $e) {
// prior to PHP 7.4 toString has to return an empty string.
if (version_compare(\PHP_VERSION, '7.4.0', '<')) {
return '';
Expand Down
36 changes: 17 additions & 19 deletions src/Smalot/PdfParser/RawData/FilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@

namespace Smalot\PdfParser\RawData;

use Exception;

class FilterHelper
{
protected $availableFilters = ['ASCIIHexDecode', 'ASCII85Decode', 'LZWDecode', 'FlateDecode', 'RunLengthDecode'];
Expand All @@ -56,7 +54,7 @@ class FilterHelper
*
* @return string Decoded data string
*
* @throws Exception if a certain decode function is not implemented yet
* @throws \Exception if a certain decode function is not implemented yet
*/
public function decodeFilter(string $filter, string $data, int $decodeMemoryLimit = 0): string
{
Expand All @@ -77,15 +75,15 @@ public function decodeFilter(string $filter, string $data, int $decodeMemoryLimi
return $this->decodeFilterRunLengthDecode($data);

case 'CCITTFaxDecode':
throw new Exception('Decode CCITTFaxDecode not implemented yet.');
throw new \Exception('Decode CCITTFaxDecode not implemented yet.');
case 'JBIG2Decode':
throw new Exception('Decode JBIG2Decode not implemented yet.');
throw new \Exception('Decode JBIG2Decode not implemented yet.');
case 'DCTDecode':
throw new Exception('Decode DCTDecode not implemented yet.');
throw new \Exception('Decode DCTDecode not implemented yet.');
case 'JPXDecode':
throw new Exception('Decode JPXDecode not implemented yet.');
throw new \Exception('Decode JPXDecode not implemented yet.');
case 'Crypt':
throw new Exception('Decode Crypt not implemented yet.');
throw new \Exception('Decode Crypt not implemented yet.');
default:
return $data;
}
Expand All @@ -100,7 +98,7 @@ public function decodeFilter(string $filter, string $data, int $decodeMemoryLimi
*
* @return string data string
*
* @throws Exception
* @throws \Exception
*/
protected function decodeFilterASCIIHexDecode(string $data): string
{
Expand All @@ -121,12 +119,12 @@ protected function decodeFilterASCIIHexDecode(string $data): string
// EOD shall behave as if a 0 (zero) followed the last digit
$data = substr($data, 0, -1).'0'.substr($data, -1);
} else {
throw new Exception('decodeFilterASCIIHexDecode: invalid code');
throw new \Exception('decodeFilterASCIIHexDecode: invalid code');
}
}
// check for invalid characters
if (preg_match('/[^a-fA-F\d]/', $data) > 0) {
throw new Exception('decodeFilterASCIIHexDecode: invalid code');
throw new \Exception('decodeFilterASCIIHexDecode: invalid code');
}
// get one byte of binary data for each pair of ASCII hexadecimal digits
$decoded = pack('H*', $data);
Expand All @@ -143,7 +141,7 @@ protected function decodeFilterASCIIHexDecode(string $data): string
*
* @return string data string
*
* @throws Exception
* @throws \Exception
*/
protected function decodeFilterASCII85Decode(string $data): string
{
Expand All @@ -166,7 +164,7 @@ protected function decodeFilterASCII85Decode(string $data): string
$data_length = \strlen($data);
// check for invalid characters
if (preg_match('/[^\x21-\x75,\x74]/', $data) > 0) {
throw new Exception('decodeFilterASCII85Decode: invalid code');
throw new \Exception('decodeFilterASCII85Decode: invalid code');
}
// z sequence
$zseq = \chr(0).\chr(0).\chr(0).\chr(0);
Expand All @@ -183,7 +181,7 @@ protected function decodeFilterASCII85Decode(string $data): string
if (0 == $group_pos) {
$decoded .= $zseq;
} else {
throw new Exception('decodeFilterASCII85Decode: invalid code');
throw new \Exception('decodeFilterASCII85Decode: invalid code');
}
} else {
// the value represented by a group of 5 characters should never be greater than 2^32 - 1
Expand Down Expand Up @@ -215,7 +213,7 @@ protected function decodeFilterASCII85Decode(string $data): string
break;

case 1:
throw new Exception('decodeFilterASCII85Decode: invalid code');
throw new \Exception('decodeFilterASCII85Decode: invalid code');
}

return $decoded;
Expand All @@ -231,7 +229,7 @@ protected function decodeFilterASCII85Decode(string $data): string
*
* @return string data string
*
* @throws Exception
* @throws \Exception
*/
protected function decodeFilterFlateDecode(string $data, int $decodeMemoryLimit): ?string
{
Expand All @@ -241,7 +239,7 @@ protected function decodeFilterFlateDecode(string $data, int $decodeMemoryLimit)
*/
set_error_handler(function ($errNo, $errStr) {
if (\E_WARNING === $errNo) {
throw new Exception($errStr);
throw new \Exception($errStr);
} else {
// fallback to default php error handler
return false;
Expand All @@ -254,9 +252,9 @@ protected function decodeFilterFlateDecode(string $data, int $decodeMemoryLimit)
try {
$decoded = gzuncompress($data, $decodeMemoryLimit);
if (false === $decoded) {
throw new Exception('decodeFilterFlateDecode: invalid code');
throw new \Exception('decodeFilterFlateDecode: invalid code');
}
} catch (Exception $e) {
} catch (\Exception $e) {
throw $e;
} finally {
// Restore old handler just in case it was customized outside of PDFParser.
Expand Down
46 changes: 24 additions & 22 deletions src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

namespace Smalot\PdfParser\RawData;

use Exception;
use Smalot\PdfParser\Config;

class RawDataParser
Expand Down Expand Up @@ -86,7 +85,7 @@ public function __construct($cfg = [], Config $config = null)
*
* @return array containing decoded stream data and remaining filters
*
* @throws Exception
* @throws \Exception
*/
protected function decodeStream(string $pdfData, array $xref, array $sdic, string $stream): array
{
Expand Down Expand Up @@ -129,12 +128,12 @@ protected function decodeStream(string $pdfData, array $xref, array $sdic, strin
if (\in_array($filter, $this->filterHelper->getAvailableFilters())) {
try {
$stream = $this->filterHelper->decodeFilter($filter, $stream, $this->config->getDecodeMemoryLimit());
} catch (Exception $e) {
} catch (\Exception $e) {
$emsg = $e->getMessage();
if ((('~' == $emsg[0]) && !$this->cfg['ignore_missing_filter_decoders'])
|| (('~' != $emsg[0]) && !$this->cfg['ignore_filter_decoding_errors'])
) {
throw new Exception($e->getMessage());
throw new \Exception($e->getMessage());
}
}
} else {
Expand All @@ -155,7 +154,7 @@ protected function decodeStream(string $pdfData, array $xref, array $sdic, strin
*
* @return array containing xref and trailer data
*
* @throws Exception
* @throws \Exception
*/
protected function decodeXref(string $pdfData, int $startxref, array $xref = []): array
{
Expand Down Expand Up @@ -217,7 +216,7 @@ protected function decodeXref(string $pdfData, int $startxref, array $xref = [])
$xref = $this->getXrefData($pdfData, (int) $matches[1], $xref);
}
} else {
throw new Exception('Unable to find trailer');
throw new \Exception('Unable to find trailer');
}

return $xref;
Expand All @@ -232,7 +231,7 @@ protected function decodeXref(string $pdfData, int $startxref, array $xref = [])
*
* @return array containing xref and trailer data
*
* @throws Exception if unknown PNG predictor detected
* @throws \Exception if unknown PNG predictor detected
*/
protected function decodeXrefStream(string $pdfData, int $startxref, array $xref = []): array
{
Expand Down Expand Up @@ -330,7 +329,10 @@ protected function decodeXrefStream(string $pdfData, int $startxref, array $xref
// number of bytes in a row
$rowlen = ($columns + 1);
// convert the stream into an array of integers
/** @var array<int> */
$sdata = unpack('C*', $xrefcrs[1][3][0]);
// TODO: Handle the case when unpack returns false

// split the rows
$sdata = array_chunk($sdata, $rowlen);

Expand Down Expand Up @@ -398,7 +400,7 @@ protected function decodeXrefStream(string $pdfData, int $startxref, array $xref
break;

default: // PNG prediction (on encoding, PNG optimum)
throw new Exception('Unknown PNG predictor: '.$predictor);
throw new \Exception('Unknown PNG predictor: '.$predictor);
}
}
$prev_row = $ddata[$k];
Expand Down Expand Up @@ -497,7 +499,7 @@ protected function decodeXrefStream(string $pdfData, int $startxref, array $xref
protected function getObjectHeaderPattern(array $objRefs): string
{
// consider all whitespace character (PDF specifications)
return '/'.$objRefs[0].$this->config->getPdfWhitespacesRegex().$objRefs[1].$this->config->getPdfWhitespacesRegex().'obj'.'/';
return '/'.$objRefs[0].$this->config->getPdfWhitespacesRegex().$objRefs[1].$this->config->getPdfWhitespacesRegex().'obj/';
}

protected function getObjectHeaderLen(array $objRefs): int
Expand All @@ -517,7 +519,7 @@ protected function getObjectHeaderLen(array $objRefs): int
*
* @return array containing object data
*
* @throws Exception if invalid object reference found
* @throws \Exception if invalid object reference found
*/
protected function getIndirectObject(string $pdfData, array $xref, string $objRef, int $offset = 0, bool $decoding = true): array
{
Expand All @@ -527,7 +529,7 @@ protected function getIndirectObject(string $pdfData, array $xref, string $objRe
// $objHeader = "[object number] [generation number] obj"
$objRefArr = explode('_', $objRef);
if (2 !== \count($objRefArr)) {
throw new Exception('Invalid object reference for $obj.');
throw new \Exception('Invalid object reference for $obj.');
}

$objHeaderLen = $this->getObjectHeaderLen($objRefArr);
Expand Down Expand Up @@ -580,7 +582,7 @@ protected function getIndirectObject(string $pdfData, array $xref, string $objRe
*
* @return array containing object data
*
* @throws Exception
* @throws \Exception
*/
protected function getObjectVal(string $pdfData, $xref, array $obj): array
{
Expand Down Expand Up @@ -807,8 +809,8 @@ protected function getRawObject(string $pdfData, int $offset = 0): array
*
* @return array containing xref and trailer data
*
* @throws Exception if it was unable to find startxref
* @throws Exception if it was unable to find xref
* @throws \Exception if it was unable to find startxref
* @throws \Exception if it was unable to find xref
*/
protected function getXrefData(string $pdfData, int $offset = 0, array $xref = []): array
{
Expand All @@ -829,7 +831,7 @@ protected function getXrefData(string $pdfData, int $offset = 0, array $xref = [
$offset
);
if (0 == $pregResult) {
throw new Exception('Unable to find startxref');
throw new \Exception('Unable to find startxref');
}
$matches = array_pop($matches);
$startxref = $matches[1];
Expand All @@ -843,11 +845,11 @@ protected function getXrefData(string $pdfData, int $offset = 0, array $xref = [
// startxref found
$startxref = $matches[1][0];
} else {
throw new Exception('Unable to find startxref');
throw new \Exception('Unable to find startxref');
}

if ($startxref > \strlen($pdfData)) {
throw new Exception('Unable to find xref (PDF corrupted?)');
throw new \Exception('Unable to find xref (PDF corrupted?)');
}

// check xref position
Expand All @@ -859,7 +861,7 @@ protected function getXrefData(string $pdfData, int $offset = 0, array $xref = [
$xref = $this->decodeXrefStream($pdfData, $startxref, $xref);
}
if (empty($xref)) {
throw new Exception('Unable to find xref');
throw new \Exception('Unable to find xref');
}

return $xref;
Expand All @@ -872,17 +874,17 @@ protected function getXrefData(string $pdfData, int $offset = 0, array $xref = [
*
* @return array array of parsed PDF document objects
*
* @throws Exception if empty PDF data given
* @throws Exception if PDF data missing %PDF header
* @throws \Exception if empty PDF data given
* @throws \Exception if PDF data missing %PDF header
*/
public function parseData(string $data): array
{
if (empty($data)) {
throw new Exception('Empty PDF data given.');
throw new \Exception('Empty PDF data given.');
}
// find the pdf header starting position
if (false === ($trimpos = strpos($data, '%PDF-'))) {
throw new Exception('Invalid PDF data: missing %PDF header.');
throw new \Exception('Invalid PDF data: missing %PDF header.');
}

// get PDF content string
Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

namespace Tests\Smalot\PdfParser\Integration;

use Exception;
use Smalot\PdfParser\Document;
use Smalot\PdfParser\Header;
use Smalot\PdfParser\Page;
Expand Down Expand Up @@ -223,7 +222,7 @@ public function testGetPages(): void

public function testGetPagesMissingCatalog(): void
{
$this->expectException(Exception::class);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Missing catalog.');

// Missing catalog
Expand Down
Loading

0 comments on commit 13a8178

Please sign in to comment.