Skip to content

Commit

Permalink
Deploy latest PHP-CS-Fixer rules (adapted some to match our setup wit…
Browse files Browse the repository at this point in the history
…h PHP 7+8) (#602)

* Run cs-fixer

Observed changes:
* in properties declarations, removed `= null`
  (no_null_property_initialization)
* in methods definitions, replaced
   `?Type $variable = null`
  by
   `Type $variable = null`
  (nullable_type_declaration_for_default_null_value)
* replaced `false !== strpos(…)` by `str_contains(…)
  (modernize_strpos)
* whitespaces changes
  (operator_linebreak, no_spaces_inside_parenthesis)
* replaced `0 !== strpos(…)` by `!str_starts_with(…)`
  (modernize_strpos)

* fixed coding style issue in Header.php

* attempt to fix failing performance tests

* php-cs-fixer: set rule get_class_to_class_keyword to false

* dynamic classes use get_class again (removed ::class)

* php-cs-fixer: set rule modernize_strpos to false (str_contains not in PHP 7.x)

* fixed another cs break

* reverted change in performance.yml

---------

Co-authored-by: Konrad Abicht <hi@inspirito.de>
  • Loading branch information
Seb35 and k00ni authored Jun 5, 2023
1 parent fafe2f2 commit fff98fd
Show file tree
Hide file tree
Showing 22 changed files with 66 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
])
;

Expand Down
10 changes: 5 additions & 5 deletions src/Smalot/PdfParser/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class Document
/**
* @var Header
*/
protected $trailer = null;
protected $trailer;

/**
* @var array
*/
protected $details = null;
protected $details;

public function __construct()
{
Expand Down Expand Up @@ -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 [];
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/Smalot/PdfParser/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Element/ElementDate.php
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Smalot/PdfParser/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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':
Expand Down
28 changes: 14 additions & 14 deletions src/Smalot/PdfParser/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
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
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
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 @@ -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);
}
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
Loading

0 comments on commit fff98fd

Please sign in to comment.