From c83c126de022c56a0f704e34a3b832e7ddd72df3 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Thu, 15 Feb 2024 08:13:00 +0100 Subject: [PATCH] fixed latest coding style issues; refined a few PHP doc entries to match type --- src/Smalot/PdfParser/Document.php | 6 +++--- src/Smalot/PdfParser/Element.php | 6 +++--- src/Smalot/PdfParser/Element/ElementArray.php | 4 ++-- .../PdfParser/Element/ElementBoolean.php | 2 +- src/Smalot/PdfParser/Element/ElementDate.php | 4 ++-- src/Smalot/PdfParser/Element/ElementHexa.php | 2 +- src/Smalot/PdfParser/Element/ElementName.php | 2 +- src/Smalot/PdfParser/Element/ElementNull.php | 2 +- .../PdfParser/Element/ElementNumeric.php | 2 +- .../PdfParser/Element/ElementString.php | 2 +- .../PdfParser/Element/ElementStruct.php | 2 +- src/Smalot/PdfParser/Element/ElementXRef.php | 2 +- src/Smalot/PdfParser/Font.php | 4 ++-- src/Smalot/PdfParser/Header.php | 4 ++-- src/Smalot/PdfParser/PDFObject.php | 20 +++++++++---------- src/Smalot/PdfParser/Page.php | 12 +++++------ src/Smalot/PdfParser/Parser.php | 2 +- .../PdfParser/RawData/RawDataParser.php | 6 ++++-- src/Smalot/PdfParser/XObject/Form.php | 2 +- src/Smalot/PdfParser/XObject/Image.php | 2 +- tests/PHPUnit/Integration/DocumentTest.php | 2 +- tests/PHPUnit/TestCase.php | 2 +- 22 files changed, 47 insertions(+), 45 deletions(-) diff --git a/src/Smalot/PdfParser/Document.php b/src/Smalot/PdfParser/Document.php index 1b5dc919..016787af 100644 --- a/src/Smalot/PdfParser/Document.php +++ b/src/Smalot/PdfParser/Document.php @@ -336,12 +336,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 []; @@ -418,7 +418,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 0ce6c428..80660303 100644 --- a/src/Smalot/PdfParser/Element.php +++ b/src/Smalot/PdfParser/Element.php @@ -49,13 +49,13 @@ class Element { /** - * @var Document + * @var Document|null */ protected $document; 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 6ad22204..b54bf843 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 4831a4ab..55fb4638 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 c4d39840..f1f2df6f 100644 --- a/src/Smalot/PdfParser/Element/ElementDate.php +++ b/src/Smalot/PdfParser/Element/ElementDate.php @@ -40,7 +40,7 @@ class ElementDate extends ElementString { /** - * @var array + * @var array */ protected static $formats = [ 4 => 'Y', @@ -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 d0314618..e3265d23 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 0f8d06b9..6e8d97ac 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 8757630e..9af88434 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 80885c17..5454acc0 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 a18ba5f5..011bcf46 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 7c95559c..c37b6da4 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 50531a79..ebba71a1 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 378f8109..cfe85d79 100644 --- a/src/Smalot/PdfParser/Font.php +++ b/src/Smalot/PdfParser/Font.php @@ -279,7 +279,7 @@ public function setTable(array $table) /** * Calculate text width with data from header 'Widths'. If width of character is not found then character is added to missing array. */ - public function calculateTextWidth(string $text, array &$missing = null): ?float + public function calculateTextWidth(string $text, ?array &$missing = null): ?float { $index_map = array_flip($this->table); $details = $this->getDetails(); @@ -485,7 +485,7 @@ public function decodeText(array $commands, float $fontFactor = 4): 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 string begins with a UTF-16BE BOM, then decode it // directly as Unicode diff --git a/src/Smalot/PdfParser/Header.php b/src/Smalot/PdfParser/Header.php index 562897c3..b58773a5 100644 --- a/src/Smalot/PdfParser/Header.php +++ b/src/Smalot/PdfParser/Header.php @@ -43,7 +43,7 @@ class Header { /** - * @var Document + * @var Document|null */ protected $document; @@ -56,7 +56,7 @@ class Header * @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 6e24064d..40973da0 100644 --- a/src/Smalot/PdfParser/PDFObject.php +++ b/src/Smalot/PdfParser/PDFObject.php @@ -54,7 +54,7 @@ class PDFObject public static $recursionStack = []; /** - * @var Document + * @var Document|null */ protected $document; @@ -69,7 +69,7 @@ class PDFObject protected $content; /** - * @var Config + * @var Config|null */ protected $config; @@ -80,9 +80,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(); @@ -405,7 +405,7 @@ public function getSectionsText(?string $content): array return $sections; } - private function getDefaultFont(Page $page = null): Font + private function getDefaultFont(?Page $page = null): Font { $fonts = []; if (null !== $page) { @@ -433,7 +433,7 @@ private function getDefaultFont(Page $page = null): Font * * @param array> $command */ - private function getTJUsingFontFallback(Font $font, array $command, Page $page = null, float $fontFactor = 4): string + private function getTJUsingFontFallback(Font $font, array $command, ?Page $page = null, float $fontFactor = 4): string { $orig_text = $font->decodeText($command, $fontFactor); $text = $orig_text; @@ -571,7 +571,7 @@ public function parseDictionary(string $dictionary): array * so whitespace is inserted in a logical way for reading by * humans. */ - public function getText(Page $page = null): string + public function getText(?Page $page = null): string { $this->addPositionWhitespace = true; $result = $this->getTextArray($page); @@ -587,7 +587,7 @@ public function getText(Page $page = null): string * * @throws \Exception */ - public function getTextArray(Page $page = null): array + public function getTextArray(?Page $page = null): array { $result = []; $text = []; @@ -1054,7 +1054,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 b8002bd3..d6ffaf08 100644 --- a/src/Smalot/PdfParser/Page.php +++ b/src/Smalot/PdfParser/Page.php @@ -176,7 +176,7 @@ public function getXObject(string $id): ?PDFObject }*/ } - public function getText(self $page = null): string + public function getText(?self $page = null): string { if ($contents = $this->get('Contents')) { if ($contents instanceof ElementMissing) { @@ -312,7 +312,7 @@ public function createPageForFpdf(): self return new self($pdfObject->document, $header, $new_content, $config); } - public function getTextArray(self $page = null): array + public function getTextArray(?self $page = null): array { if ($this->isFpdf()) { $pdfObject = $this->getPDFObjectForFpdf(); @@ -418,7 +418,7 @@ public function extractRawData(): array * * @return array An array with the data and the internal representation */ - public function extractDecodedRawData(array $extractedRawData = null): array + public function extractDecodedRawData(?array $extractedRawData = null): array { if (!isset($extractedRawData) || !$extractedRawData) { $extractedRawData = $this->extractRawData(); @@ -498,7 +498,7 @@ public function extractDecodedRawData(array $extractedRawData = null): array * * @return array An array with the text command of the page */ - public function getDataCommands(array $extractedDecodedRawData = null): array + public function getDataCommands(?array $extractedDecodedRawData = null): array { if (!isset($extractedDecodedRawData) || !$extractedDecodedRawData) { $extractedDecodedRawData = $this->extractDecodedRawData(); @@ -649,7 +649,7 @@ public function getDataCommands(array $extractedDecodedRawData = null): array * @return array an array with the data of the page including the Tm information * of any text in the page */ - public function getDataTm(array $dataCommands = null): array + public function getDataTm(?array $dataCommands = null): array { if (!isset($dataCommands) || !$dataCommands) { $dataCommands = $this->getDataCommands(); @@ -894,7 +894,7 @@ public function getDataTm(array $dataCommands = null): array * "near" the x,y coordinate, an empty array is returned. If Both, x * and y coordinates are null, null is returned. */ - public function getTextXY(float $x = null, float $y = null, float $xError = 0, float $yError = 0): array + public function getTextXY(?float $x = null, ?float $y = null, float $xError = 0, float $yError = 0): array { if (!isset($this->dataTm) || !$this->dataTm) { $this->getDataTm(); diff --git a/src/Smalot/PdfParser/Parser.php b/src/Smalot/PdfParser/Parser.php index 86bfe555..b051f114 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 8f8c15dc..6b04fae5 100644 --- a/src/Smalot/PdfParser/RawData/RawDataParser.php +++ b/src/Smalot/PdfParser/RawData/RawDataParser.php @@ -53,6 +53,8 @@ class RawDataParser /** * Configuration array. + * + * @var array */ protected $cfg = [ // if `true` ignore filter decoding errors @@ -67,7 +69,7 @@ class RawDataParser /** * @param array $cfg Configuration array, default is [] */ - public function __construct($cfg = [], Config $config = null) + public function __construct($cfg = [], ?Config $config = null) { // merge given array with default values $this->cfg = array_merge($this->cfg, $cfg); @@ -609,7 +611,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/src/Smalot/PdfParser/XObject/Form.php b/src/Smalot/PdfParser/XObject/Form.php index 7caec8c9..8e60647f 100644 --- a/src/Smalot/PdfParser/XObject/Form.php +++ b/src/Smalot/PdfParser/XObject/Form.php @@ -41,7 +41,7 @@ */ class Form extends Page { - public function getText(Page $page = null): string + public function getText(?Page $page = null): string { $header = new Header([], $this->document); $contents = new PDFObject($this->document, $header, $this->content, $this->config); diff --git a/src/Smalot/PdfParser/XObject/Image.php b/src/Smalot/PdfParser/XObject/Image.php index 12655828..6dc6b0a6 100644 --- a/src/Smalot/PdfParser/XObject/Image.php +++ b/src/Smalot/PdfParser/XObject/Image.php @@ -40,7 +40,7 @@ */ class Image extends PDFObject { - public function getText(Page $page = null): string + public function getText(?Page $page = null): string { return ''; } diff --git a/tests/PHPUnit/Integration/DocumentTest.php b/tests/PHPUnit/Integration/DocumentTest.php index 26553fca..5f19b696 100644 --- a/tests/PHPUnit/Integration/DocumentTest.php +++ b/tests/PHPUnit/Integration/DocumentTest.php @@ -52,7 +52,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 5e584c62..08d4739a 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); }