Skip to content

Commit

Permalink
Merge pull request #304 from smalot/feature/add-phpstan
Browse files Browse the repository at this point in the history
Add PHPStan (+ extend php-cs-fixer to complain)
  • Loading branch information
j0k3r authored Jun 8, 2020
2 parents a90ece6 + 19fe567 commit 2f38955
Show file tree
Hide file tree
Showing 28 changed files with 164 additions and 195 deletions.
11 changes: 5 additions & 6 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->files()
->in(__DIR__ . '/samples')
->in(__DIR__ . '/src')
->in(__DIR__ . '/test')
->in(__DIR__ . '/tests')
->name('*.php')
->exclude([
'vendor',
])
->in(__DIR__)
->name('*.php')
);
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4
- nightly

jobs:
fast_finish: true
allow_failures:
- php: nightly
include:
- php: 7.2
env: COMPOSER_FLAGS="--prefer-lowest"
- php: 7.4
env: COVERAGE_FLAGS="--coverage-clover coverage/clover.xml"
env: COMPOSER_FLAGS="--prefer-lowest" LINTER_RUN=run COVERAGE_FLAGS="--coverage-clover coverage/clover.xml"

install:
- composer self-update
- composer update --prefer-dist --no-progress --no-suggest --optimize-autoloader $COMPOSER_FLAGS

script: vendor/bin/phpunit $COVERAGE_FLAGS
script:
- if [ "$LINTER_RUN" = "run" ]; then php vendor/bin/php-cs-fixer fix --verbose --dry-run ; fi;
- if [ "$LINTER_RUN" = "run" ]; then composer require phpstan/phpstan --no-progress --no-suggest ; fi;
- if [ "$LINTER_RUN" = "run" ]; then php vendor/bin/phpstan analyse src test tests --no-progress --level 3 ; fi;
- php vendor/bin/phpunit $COVERAGE_FLAGS
12 changes: 5 additions & 7 deletions src/Smalot/PdfParser/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,22 @@ public function getObjects()
/**
* @param string $id
*
* @return PDFObject
* @return PDFObject|Font|Page|Element|null
*/
public function getObjectById($id)
{
if (isset($this->objects[$id])) {
return $this->objects[$id];
} else {
return null;
}

return null;
}

/**
* @param string $type
* @param string $subtype
*
* @return PDFObject[]
* @return array
*/
public function getObjectsByType($type, $subtype = null)
{
Expand Down Expand Up @@ -211,9 +211,7 @@ public function getPages()
/** @var Pages $object */
$object = $this->objects[$id]->get('Pages');
if (method_exists($object, 'getPages')) {
$pages = $object->getPages(true);

return $pages;
return $object->getPages(true);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public function contains($value)
}

return false;
} else {
return $this->equals($value);
}

return $this->equals($value);
}

public function getContent()
Expand Down
7 changes: 3 additions & 4 deletions src/Smalot/PdfParser/Element/ElementBoolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@
class ElementBoolean extends Element
{
/**
* @param string $value
* @param Document $document
* @param string|bool $value
*/
public function __construct($value, Document $document = null)
public function __construct($value)
{
parent::__construct(('true' == strtolower($value) || true === $value), null);
}
Expand Down Expand Up @@ -76,7 +75,7 @@ public static function parse($content, Document $document = null, &$offset = 0)
$value = $match['value'];
$offset += strpos($content, $value) + \strlen($value);

return new self($value, $document);
return new self($value);
}

return false;
Expand Down
8 changes: 3 additions & 5 deletions src/Smalot/PdfParser/Element/ElementDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ class ElementDate extends ElementString

/**
* @param \DateTime $value
* @param Document $document
*/
public function __construct($value, Document $document = null)
public function __construct($value)
{
if (!($value instanceof \DateTime)) {
throw new \Exception('DateTime required.');
}

parent::__construct($value, null);
parent::__construct($value);
}

/**
Expand Down Expand Up @@ -140,9 +139,8 @@ public static function parse($content, Document $document = null, &$offset = 0)
}

$offset += strpos($content, '(D:') + \strlen($match['name']) + 4; // 1 for '(D:' and ')'
$element = new self($date, $document);

return $element;
return new self($date);
}

return false;
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
* @param Document $document
* @param int $offset
*
* @return bool|ElementHexa
* @return bool|ElementHexa|ElementDate
*/
public static function parse($content, Document $document = null, &$offset = 0)
{
Expand Down
7 changes: 1 addition & 6 deletions src/Smalot/PdfParser/Element/ElementMissing.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,14 @@

namespace Smalot\PdfParser\Element;

use Smalot\PdfParser\Document;
use Smalot\PdfParser\Element;

/**
* Class ElementMissing
*/
class ElementMissing extends Element
{
/**
* @param string $value
* @param Document $document
*/
public function __construct($value, Document $document = null)
public function __construct()
{
parent::__construct(null, null);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Smalot/PdfParser/Element/ElementName.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
class ElementName extends Element
{
/**
* @param string $value
* @param Document $document
* @param string $value
*/
public function __construct($value, Document $document = null)
public function __construct($value)
{
parent::__construct($value, null);
}
Expand All @@ -70,7 +69,7 @@ public static function parse($content, Document $document = null, &$offset = 0)
$offset += strpos($content, $name) + \strlen($name);
$name = Font::decodeEntities($name);

return new self($name, $document);
return new self($name);
}

return false;
Expand Down
8 changes: 2 additions & 6 deletions src/Smalot/PdfParser/Element/ElementNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@
*/
class ElementNull extends Element
{
/**
* @param string $value
* @param Document $document
*/
public function __construct($value, Document $document = null)
public function __construct()
{
parent::__construct(null, null);
}
Expand Down Expand Up @@ -75,7 +71,7 @@ public static function parse($content, Document $document = null, &$offset = 0)
if (preg_match('/^\s*(null)/s', $content, $match)) {
$offset += strpos($content, 'null') + \strlen('null');

return new self(null, $document);
return new self();
}

return false;
Expand Down
7 changes: 3 additions & 4 deletions src/Smalot/PdfParser/Element/ElementNumeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@
class ElementNumeric extends Element
{
/**
* @param string $value
* @param Document $document
* @param string $value
*/
public function __construct($value, Document $document = null)
public function __construct($value)
{
parent::__construct((float) $value, null);
}
Expand All @@ -60,7 +59,7 @@ public static function parse($content, Document $document = null, &$offset = 0)
$value = $match['value'];
$offset += strpos($content, $value) + \strlen($value);

return new self($value, $document);
return new self($value);
}

return false;
Expand Down
9 changes: 4 additions & 5 deletions src/Smalot/PdfParser/Element/ElementString.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
class ElementString extends Element
{
/**
* @param string $value
* @param Document $document
* @param string $value
*/
public function __construct($value, Document $document = null)
public function __construct($value)
{
parent::__construct($value, null);
}
Expand Down Expand Up @@ -80,7 +79,7 @@ public static function parse($content, Document $document = null, &$offset = 0)
}

// Extract string.
$name = substr($name, 0, $cur_start_pos);
$name = substr($name, 0, (int) $cur_start_pos);
$offset += strpos($content, '(') + $cur_start_pos + 2; // 2 for '(' and ')'
$name = str_replace(
['\\\\', '\\ ', '\\/', '\(', '\)', '\n', '\r', '\t'],
Expand All @@ -94,7 +93,7 @@ public static function parse($content, Document $document = null, &$offset = 0)
$name = Font::decodeHexadecimal($name, false);
$name = Font::decodeUnicode($name);

return new self($name, $document);
return new self($name);
}

return false;
Expand Down
7 changes: 3 additions & 4 deletions 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
* @param Document $document
* @param int $offset
*
* @return bool|ElementStruct
* @return false|Header
*/
public static function parse($content, Document $document = null, &$offset = 0)
{
Expand All @@ -64,13 +64,12 @@ public static function parse($content, Document $document = null, &$offset = 0)
$offset += strpos($content, '<<') + \strlen(rtrim($sub));

// Removes '<<' and '>>'.
$sub = trim(preg_replace('/^\s*<<(.*)>>\s*$/s', '\\1', $sub));
$sub = trim((string) preg_replace('/^\s*<<(.*)>>\s*$/s', '\\1', $sub));

$position = 0;
$elements = Element::parse($sub, $document, $position);
$header = new Header($elements, $document);

return $header;
return new Header($elements, $document);
}

return false;
Expand Down
13 changes: 6 additions & 7 deletions src/Smalot/PdfParser/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ public function init()
{
$this->mapping = [];
$this->differences = [];
$this->encoding = null;
$this->encoding = [];

if ($this->has('BaseEncoding')) {
// Load reference table charset.
$baseEncoding = preg_replace('/[^A-Z0-9]/is', '', $this->get('BaseEncoding')->getContent());
$className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding;

if (class_exists($className)) {
$class = new $className();
$this->encoding = $class->getTranslations();
} else {
if (!class_exists($className)) {
throw new \Exception('Missing encoding data for: "'.$baseEncoding.'".');
}

$class = new $className();
$this->encoding = $class->getTranslations();

// Build table including differences.
$differences = $this->get('Differences')->getContent();
$code = 0;
Expand All @@ -86,10 +86,9 @@ public function init()
}

// ElementName
$this->differences[$code] = $difference;
if (\is_object($difference)) {
$this->differences[$code] = $difference->getContent();
} else {
$this->differences[$code] = $difference;
}

// For the next char.
Expand Down
13 changes: 6 additions & 7 deletions src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,17 @@ public function getDetails($deep = true)
* @param string $char
* @param bool $use_default
*
* @return string
* @return string|bool
*/
public function translateChar($char, $use_default = true)
{
$dec = hexdec(bin2hex($char));

if (\array_key_exists($dec, $this->table)) {
$char = $this->table[$dec];
} else {
$char = ($use_default ? self::MISSING : $char);
return $this->table[$dec];
}

return $char;
return $use_default ? self::MISSING : $char;
}

/**
Expand Down Expand Up @@ -292,7 +290,7 @@ public static function decodeOctal($text)
}

/**
* @param $text
* @param string $text
*
* @return string
*/
Expand Down Expand Up @@ -348,6 +346,7 @@ protected function getFontSpaceLimit()
*/
public function decodeText($commands)
{
$text = '';
$word_position = 0;
$words = [];
$unicode = false;
Expand Down Expand Up @@ -456,7 +455,7 @@ public function decodeContent($text, &$unicode)
if ($encoding instanceof Encoding) {
if ($unicode) {
$chars = preg_split(
'//s'.($unicode ? 'u' : ''),
'//su',
$text,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
Expand Down
Loading

0 comments on commit 2f38955

Please sign in to comment.