Skip to content

Commit

Permalink
fixed latest coding style issues; refined a few PHP doc entries to ma…
Browse files Browse the repository at this point in the history
…tch type
  • Loading branch information
k00ni committed Feb 15, 2024
1 parent 4db3b81 commit c83c126
Show file tree
Hide file tree
Showing 22 changed files with 47 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/Smalot/PdfParser/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/Smalot/PdfParser/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/Element/ElementArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<array>.*)/is', $content, $match)) {
preg_match_all('/(.*?)(\[|\])/s', trim($content), $matches);
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Element/ElementBoolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -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*(?P<value>true|false)/is', $content, $match)) {
$value = $match['value'];
Expand Down
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/Element/ElementDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
class ElementDate extends ElementString
{
/**
* @var array
* @var array<int,string>
*/
protected static $formats = [
4 => 'Y',
Expand Down Expand Up @@ -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<name>.*?)\)/s', $content, $match)) {
$name = $match['name'];
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Element/ElementHexa.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<name>[A-F0-9]+)\>/is', $content, $match)) {
$name = $match['name'];
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Element/ElementName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Element/ElementNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Element/ElementNumeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<value>\-?[0-9\.]+)/s', $content, $match)) {
$value = $match['value'];
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Element/ElementString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<name>.*)/s', $content, $match)) {
$name = $match['name'];
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Element/ElementStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<struct>.*)/is', $content)) {
preg_match_all('/(.*?)(<<|>>)/s', trim($content), $matches);
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Element/ElementXRef.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<id>[0-9]+\s+[0-9]+\s+R)/s', $content, $match)) {
$id = $match['id'];
Expand Down
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
class Header
{
/**
* @var Document
* @var Document|null
*/
protected $document;

Expand All @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class PDFObject
public static $recursionStack = [];

/**
* @var Document
* @var Document|null
*/
protected $document;

Expand All @@ -69,7 +69,7 @@ class PDFObject
protected $content;

/**
* @var Config
* @var Config|null
*/
protected $config;

Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -433,7 +433,7 @@ private function getDefaultFont(Page $page = null): Font
*
* @param array<int,array<string,string|bool>> $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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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':
Expand Down
12 changes: 6 additions & 6 deletions src/Smalot/PdfParser/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class RawDataParser

/**
* Configuration array.
*
* @var array<string,bool>
*/
protected $cfg = [
// if `true` ignore filter decoding errors
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/XObject/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/XObject/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
class Image extends PDFObject
{
public function getText(Page $page = null): string
public function getText(?Page $page = null): string
{
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPUnit/Integration/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPUnit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit c83c126

Please sign in to comment.