diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 85f56fea..c860c516 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -25,6 +25,8 @@ 'ordered_imports' => true, 'phpdoc_summary' => false, 'protected_to_private' => false, + 'get_class_to_class_keyword' => false, // override for PHP < 8.0 (because ::class usage is not allowed there) + 'modernize_strpos' => false, // override for PHP < 8.0 (because str_contains not available in PHP 7.x) ]) ; diff --git a/src/Smalot/PdfParser/Document.php b/src/Smalot/PdfParser/Document.php index 9c6835c0..f6a90abd 100644 --- a/src/Smalot/PdfParser/Document.php +++ b/src/Smalot/PdfParser/Document.php @@ -59,12 +59,12 @@ class Document /** * @var Header */ - protected $trailer = null; + protected $trailer; /** * @var array */ - protected $details = null; + protected $details; public function __construct() { @@ -182,12 +182,12 @@ public function getObjectById(string $id) return null; } - public function hasObjectsByType(string $type, ?string $subtype = null): bool + public function hasObjectsByType(string $type, string $subtype = null): bool { return 0 < \count($this->getObjectsByType($type, $subtype)); } - public function getObjectsByType(string $type, ?string $subtype = null): array + public function getObjectsByType(string $type, string $subtype = null): array { if (!isset($this->dictionary[$type])) { return []; @@ -264,7 +264,7 @@ public function getPages() throw new \Exception('Missing catalog.'); } - public function getText(?int $pageLimit = null): string + public function getText(int $pageLimit = null): string { $texts = []; $pages = $this->getPages(); diff --git a/src/Smalot/PdfParser/Element.php b/src/Smalot/PdfParser/Element.php index bdd4afc3..df61c583 100644 --- a/src/Smalot/PdfParser/Element.php +++ b/src/Smalot/PdfParser/Element.php @@ -51,11 +51,11 @@ class Element /** * @var Document */ - protected $document = null; + protected $document; - protected $value = null; + protected $value; - public function __construct($value, ?Document $document = null) + public function __construct($value, Document $document = null) { $this->value = $value; $this->document = $document; @@ -96,7 +96,7 @@ public function __toString(): string return (string) $this->value; } - public static function parse(string $content, ?Document $document = null, int &$position = 0) + public static function parse(string $content, Document $document = null, int &$position = 0) { $args = \func_get_args(); $only_values = isset($args[3]) ? $args[3] : false; diff --git a/src/Smalot/PdfParser/Element/ElementArray.php b/src/Smalot/PdfParser/Element/ElementArray.php index b54bf843..6ad22204 100644 --- a/src/Smalot/PdfParser/Element/ElementArray.php +++ b/src/Smalot/PdfParser/Element/ElementArray.php @@ -42,7 +42,7 @@ */ class ElementArray extends Element { - public function __construct($value, ?Document $document = null) + public function __construct($value, Document $document = null) { parent::__construct($value, $document); } @@ -107,7 +107,7 @@ protected function resolveXRef(string $name) * * @return bool|ElementArray */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*\[(?P.*)/is', $content, $match)) { preg_match_all('/(.*?)(\[|\])/s', trim($content), $matches); diff --git a/src/Smalot/PdfParser/Element/ElementBoolean.php b/src/Smalot/PdfParser/Element/ElementBoolean.php index 55fb4638..4831a4ab 100644 --- a/src/Smalot/PdfParser/Element/ElementBoolean.php +++ b/src/Smalot/PdfParser/Element/ElementBoolean.php @@ -61,7 +61,7 @@ public function equals($value): bool /** * @return bool|ElementBoolean */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*(?Ptrue|false)/is', $content, $match)) { $value = $match['value']; diff --git a/src/Smalot/PdfParser/Element/ElementDate.php b/src/Smalot/PdfParser/Element/ElementDate.php index 5f3d29f4..c4d39840 100644 --- a/src/Smalot/PdfParser/Element/ElementDate.php +++ b/src/Smalot/PdfParser/Element/ElementDate.php @@ -98,7 +98,7 @@ public function __toString(): string /** * @return bool|ElementDate */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*\(D\:(?P.*?)\)/s', $content, $match)) { $name = $match['name']; diff --git a/src/Smalot/PdfParser/Element/ElementHexa.php b/src/Smalot/PdfParser/Element/ElementHexa.php index e3265d23..d0314618 100644 --- a/src/Smalot/PdfParser/Element/ElementHexa.php +++ b/src/Smalot/PdfParser/Element/ElementHexa.php @@ -42,7 +42,7 @@ class ElementHexa extends ElementString /** * @return bool|ElementHexa|ElementDate */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*\<(?P[A-F0-9]+)\>/is', $content, $match)) { $name = $match['name']; diff --git a/src/Smalot/PdfParser/Element/ElementName.php b/src/Smalot/PdfParser/Element/ElementName.php index 6e8d97ac..0f8d06b9 100644 --- a/src/Smalot/PdfParser/Element/ElementName.php +++ b/src/Smalot/PdfParser/Element/ElementName.php @@ -54,7 +54,7 @@ public function equals($value): bool /** * @return bool|ElementName */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*\/([A-Z0-9\-\+,#\.]+)/is', $content, $match)) { $name = $match[1]; diff --git a/src/Smalot/PdfParser/Element/ElementNull.php b/src/Smalot/PdfParser/Element/ElementNull.php index 9af88434..8757630e 100644 --- a/src/Smalot/PdfParser/Element/ElementNull.php +++ b/src/Smalot/PdfParser/Element/ElementNull.php @@ -58,7 +58,7 @@ public function equals($value): bool /** * @return bool|ElementNull */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*(null)/s', $content, $match)) { $offset += strpos($content, 'null') + \strlen('null'); diff --git a/src/Smalot/PdfParser/Element/ElementNumeric.php b/src/Smalot/PdfParser/Element/ElementNumeric.php index 5454acc0..80885c17 100644 --- a/src/Smalot/PdfParser/Element/ElementNumeric.php +++ b/src/Smalot/PdfParser/Element/ElementNumeric.php @@ -48,7 +48,7 @@ public function __construct(string $value) /** * @return bool|ElementNumeric */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*(?P\-?[0-9\.]+)/s', $content, $match)) { $value = $match['value']; diff --git a/src/Smalot/PdfParser/Element/ElementString.php b/src/Smalot/PdfParser/Element/ElementString.php index 011bcf46..a18ba5f5 100644 --- a/src/Smalot/PdfParser/Element/ElementString.php +++ b/src/Smalot/PdfParser/Element/ElementString.php @@ -54,7 +54,7 @@ public function equals($value): bool /** * @return bool|ElementString */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*\((?P.*)/s', $content, $match)) { $name = $match['name']; diff --git a/src/Smalot/PdfParser/Element/ElementStruct.php b/src/Smalot/PdfParser/Element/ElementStruct.php index c37b6da4..7c95559c 100644 --- a/src/Smalot/PdfParser/Element/ElementStruct.php +++ b/src/Smalot/PdfParser/Element/ElementStruct.php @@ -44,7 +44,7 @@ class ElementStruct extends Element /** * @return false|Header */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*<<(?P.*)/is', $content)) { preg_match_all('/(.*?)(<<|>>)/s', trim($content), $matches); diff --git a/src/Smalot/PdfParser/Element/ElementXRef.php b/src/Smalot/PdfParser/Element/ElementXRef.php index ebba71a1..50531a79 100644 --- a/src/Smalot/PdfParser/Element/ElementXRef.php +++ b/src/Smalot/PdfParser/Element/ElementXRef.php @@ -83,7 +83,7 @@ public function __toString(): string /** * @return bool|ElementXRef */ - public static function parse(string $content, ?Document $document = null, int &$offset = 0) + public static function parse(string $content, Document $document = null, int &$offset = 0) { if (preg_match('/^\s*(?P[0-9]+\s+[0-9]+\s+R)/s', $content, $match)) { $id = $match['id']; diff --git a/src/Smalot/PdfParser/Font.php b/src/Smalot/PdfParser/Font.php index 111a23ba..9e4db9fe 100644 --- a/src/Smalot/PdfParser/Font.php +++ b/src/Smalot/PdfParser/Font.php @@ -45,12 +45,12 @@ class Font extends PDFObject /** * @var array */ - protected $table = null; + protected $table; /** * @var array */ - protected $tableSizes = null; + protected $tableSizes; /** * Caches results from uchr. @@ -459,7 +459,7 @@ public function decodeText(array $commands): string * * @param bool $unicode This parameter is deprecated and might be removed in a future release */ - public function decodeContent(string $text, ?bool &$unicode = null): string + public function decodeContent(string $text, bool &$unicode = null): string { if ($this->has('ToUnicode')) { return $this->decodeContentByToUnicodeCMapOrDescendantFonts($text); diff --git a/src/Smalot/PdfParser/Header.php b/src/Smalot/PdfParser/Header.php index 66a70102..562897c3 100644 --- a/src/Smalot/PdfParser/Header.php +++ b/src/Smalot/PdfParser/Header.php @@ -45,18 +45,18 @@ class Header /** * @var Document */ - protected $document = null; + protected $document; /** * @var Element[] */ - protected $elements = null; + protected $elements; /** * @param Element[] $elements list of elements * @param Document $document document */ - public function __construct(array $elements = [], ?Document $document = null) + public function __construct(array $elements = [], Document $document = null) { $this->elements = $elements; $this->document = $document; diff --git a/src/Smalot/PdfParser/PDFObject.php b/src/Smalot/PdfParser/PDFObject.php index d038702d..c879176b 100644 --- a/src/Smalot/PdfParser/PDFObject.php +++ b/src/Smalot/PdfParser/PDFObject.php @@ -56,17 +56,17 @@ class PDFObject /** * @var Document */ - protected $document = null; + protected $document; /** * @var Header */ - protected $header = null; + protected $header; /** * @var string */ - protected $content = null; + protected $content; /** * @var Config @@ -75,9 +75,9 @@ class PDFObject public function __construct( Document $document, - ?Header $header = null, - ?string $content = null, - ?Config $config = null + Header $header = null, + string $content = null, + Config $config = null ) { $this->document = $document; $this->header = $header ?? new Header(); @@ -249,7 +249,7 @@ private function getDefaultFont(Page $page = null): Font /** * @throws \Exception */ - public function getText(?Page $page = null): string + public function getText(Page $page = null): string { $result = ''; $sections = $this->getSectionsText($this->content); @@ -283,8 +283,8 @@ public function getText(?Page $page = null): string $args = preg_split('/\s/s', $command[self::COMMAND]); $y = array_pop($args); $x = array_pop($args); - if (((float) $x <= 0) || - (false !== $current_position_td['y'] && (float) $y < (float) $current_position_td['y']) + if (((float) $x <= 0) + || (false !== $current_position_td['y'] && (float) $y < (float) $current_position_td['y']) ) { // vertical offset $text .= "\n"; @@ -456,7 +456,7 @@ public function getText(?Page $page = null): string /** * @throws \Exception */ - public function getTextArray(?Page $page = null): array + public function getTextArray(Page $page = null): array { $text = []; $sections = $this->getSectionsText($this->content); @@ -772,7 +772,7 @@ public static function factory( Document $document, Header $header, ?string $content, - ?Config $config = null + Config $config = null ): self { switch ($header->get('Type')->getContent()) { case 'XObject': diff --git a/src/Smalot/PdfParser/Page.php b/src/Smalot/PdfParser/Page.php index 55121a9b..fbc19872 100644 --- a/src/Smalot/PdfParser/Page.php +++ b/src/Smalot/PdfParser/Page.php @@ -42,17 +42,17 @@ class Page extends PDFObject /** * @var Font[] */ - protected $fonts = null; + protected $fonts; /** * @var PDFObject[] */ - protected $xobjects = null; + protected $xobjects; /** * @var array */ - protected $dataTm = null; + protected $dataTm; /** * @return Font[] @@ -236,9 +236,9 @@ public function getText(self $page = null): string */ public function isFpdf(): bool { - if (\array_key_exists('Producer', $this->document->getDetails()) && - \is_string($this->document->getDetails()['Producer']) && - 0 === strncmp($this->document->getDetails()['Producer'], 'FPDF', 4)) { + if (\array_key_exists('Producer', $this->document->getDetails()) + && \is_string($this->document->getDetails()['Producer']) + && 0 === strncmp($this->document->getDetails()['Producer'], 'FPDF', 4)) { return true; } @@ -926,23 +926,23 @@ public function getTextXY(float $x = null, float $y = null, float $xError = 0, f $yTm = (float) $tm[5]; $text = $item[1]; if (null === $y) { - if (($xTm >= ($x - $xError)) && - ($xTm <= ($x + $xError))) { + if (($xTm >= ($x - $xError)) + && ($xTm <= ($x + $xError))) { $extractedData[] = [$tm, $text]; continue; } } if (null === $x) { - if (($yTm >= ($y - $yError)) && - ($yTm <= ($y + $yError))) { + if (($yTm >= ($y - $yError)) + && ($yTm <= ($y + $yError))) { $extractedData[] = [$tm, $text]; continue; } } - if (($xTm >= ($x - $xError)) && - ($xTm <= ($x + $xError)) && - ($yTm >= ($y - $yError)) && - ($yTm <= ($y + $yError))) { + if (($xTm >= ($x - $xError)) + && ($xTm <= ($x + $xError)) + && ($yTm >= ($y - $yError)) + && ($yTm <= ($y + $yError))) { $extractedData[] = [$tm, $text]; continue; } diff --git a/src/Smalot/PdfParser/Parser.php b/src/Smalot/PdfParser/Parser.php index 2109c81f..69ae9b70 100644 --- a/src/Smalot/PdfParser/Parser.php +++ b/src/Smalot/PdfParser/Parser.php @@ -60,7 +60,7 @@ class Parser protected $rawDataParser; - public function __construct($cfg = [], ?Config $config = null) + public function __construct($cfg = [], Config $config = null) { $this->config = $config ?: new Config(); $this->rawDataParser = new RawDataParser($cfg, $this->config); diff --git a/src/Smalot/PdfParser/RawData/RawDataParser.php b/src/Smalot/PdfParser/RawData/RawDataParser.php index b957a543..1a4583c0 100644 --- a/src/Smalot/PdfParser/RawData/RawDataParser.php +++ b/src/Smalot/PdfParser/RawData/RawDataParser.php @@ -402,7 +402,7 @@ protected function decodeXrefStream(string $pdfData, int $startxref, array $xref } $prev_row = $ddata[$k]; } // end for each row - // complete decoding + // complete decoding } else { // number of bytes in a row $rowlen = array_sum($wb); @@ -609,7 +609,7 @@ protected function getObjectVal(string $pdfData, $xref, array $obj): array * * @return array containing object type, raw value and offset to next object */ - protected function getRawObject(string $pdfData, int $offset = 0, ?array $headerDic = null): array + protected function getRawObject(string $pdfData, int $offset = 0, array $headerDic = null): array { $objtype = ''; // object type to be returned $objval = ''; // object value to be returned diff --git a/tests/PHPUnit/Integration/DocumentTest.php b/tests/PHPUnit/Integration/DocumentTest.php index 8ab48e30..6a7f8c79 100644 --- a/tests/PHPUnit/Integration/DocumentTest.php +++ b/tests/PHPUnit/Integration/DocumentTest.php @@ -50,7 +50,7 @@ protected function getDocumentInstance(): Document return new Document(); } - protected function getPDFObjectInstance(Document $document, ?Header $header = null): PDFObject + protected function getPDFObjectInstance(Document $document, Header $header = null): PDFObject { return new PDFObject($document, $header); } diff --git a/tests/PHPUnit/TestCase.php b/tests/PHPUnit/TestCase.php index 08d4739a..5e584c62 100644 --- a/tests/PHPUnit/TestCase.php +++ b/tests/PHPUnit/TestCase.php @@ -67,7 +67,7 @@ protected function getElementInstance($value): Element return new Element($value); } - protected function getParserInstance(?Config $config = null): Parser + protected function getParserInstance(Config $config = null): Parser { return new Parser([], $config); } diff --git a/tests/Performance/runPerformanceTests.php b/tests/Performance/runPerformanceTests.php index 7f34b60a..fa983105 100644 --- a/tests/Performance/runPerformanceTests.php +++ b/tests/Performance/runPerformanceTests.php @@ -19,6 +19,13 @@ $time = $endTime - $startTime; if ($test->getMaxEstimatedTime() <= $time) { - throw new PerformanceFailException(sprintf('Performance failed on test "%s". Time taken was %.2f seconds, expected less than %d seconds.', get_class($test), $time, $test->getMaxEstimatedTime())); + $msg = sprintf( + 'Performance failed on test "%s". Time taken was %.2f seconds, expected less than %d seconds.', + get_class($test), + $time, + $test->getMaxEstimatedTime() + ); + + throw new PerformanceFailException($msg); } }