Skip to content

Commit

Permalink
Merge pull request #292 from smalot/feature/add-cs-fixer
Browse files Browse the repository at this point in the history
Add FriendsOfPHP/PHP-CS-Fixer to "require-dev" to enforce coding styles
  • Loading branch information
k00ni authored May 27, 2020
2 parents f7fac8e + 91be9eb commit 6bc9dcb
Show file tree
Hide file tree
Showing 54 changed files with 1,201 additions and 1,354 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/composer
debug*
composer.lock
/.php_cs.cache
22 changes: 22 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'no_empty_phpdoc' => true,
'no_unused_imports' => true,
'no_superfluous_phpdoc_tags' => true,
'ordered_imports' => true,
'phpdoc_summary' => false,
'protected_to_private' => false,
])
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->files()
->in(__DIR__ . '/samples')
->in(__DIR__ . '/src')
->name('*.php')
);
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"tecnickcom/tcpdf": "^6.2.22"
},
"require-dev": {
"atoum/atoum": "^3.1"
"atoum/atoum": "^3.1",
"friendsofphp/php-cs-fixer": "^2.16.3"
},
"autoload": {
"psr-0": {
Expand Down
45 changes: 17 additions & 28 deletions src/Smalot/PdfParser/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
* @author Sébastien MALOT <[email protected]>
* @date 2017-01-03
*
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
Expand All @@ -25,13 +26,10 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/

namespace Smalot\PdfParser;

use Smalot\PdfParser\Element\ElementDate;

/**
* Technical references :
* - http://www.mactech.com/articles/mactech/Vol.15/15.09/PDFIntro/index.html
Expand All @@ -43,20 +41,18 @@
* - http://cpansearch.perl.org/src/JV/PostScript-Font-1.10.02/lib/PostScript/WinAnsiEncoding.pm
*
* Class Document
*
* @package Smalot\PdfParser
*/
class Document
{
/**
* @var PDFObject[]
*/
protected $objects = array();
protected $objects = [];

/**
* @var array
*/
protected $dictionary = array();
protected $dictionary = [];

/**
* @var Header
Expand All @@ -68,17 +64,11 @@ class Document
*/
protected $details = null;

/**
*
*/
public function __construct()
{
$this->trailer = new Header(array(), $this);
$this->trailer = new Header([], $this);
}

/**
*
*/
public function init()
{
$this->buildDictionary();
Expand All @@ -97,7 +87,7 @@ public function init()
protected function buildDictionary()
{
// Build dictionary.
$this->dictionary = array();
$this->dictionary = [];

foreach ($this->objects as $id => $object) {
$type = $object->getHeader()->get('Type')->getContent();
Expand All @@ -114,23 +104,23 @@ protected function buildDictionary()
protected function buildDetails()
{
// Build details array.
$details = array();
$details = [];

// Extract document info
if ($this->trailer->has('Info')) {
/** @var PDFObject $info */
$info = $this->trailer->get('Info');
// This could be an ElementMissing object, so we need to check for
// the getHeader method first.
if ($info !== null && method_exists($info, 'getHeader')) {
if (null !== $info && method_exists($info, 'getHeader')) {
$details = $info->getHeader()->getDetails();
}
}

// Retrieve the page count
try {
$pages = $this->getPages();
$details['Pages'] = count($pages);
$details['Pages'] = \count($pages);
} catch (\Exception $e) {
$details['Pages'] = 0;
}
Expand All @@ -149,9 +139,9 @@ public function getDictionary()
/**
* @param PDFObject[] $objects
*/
public function setObjects($objects = array())
public function setObjects($objects = [])
{
$this->objects = (array)$objects;
$this->objects = (array) $objects;

$this->init();
}
Expand Down Expand Up @@ -186,11 +176,11 @@ public function getObjectById($id)
*/
public function getObjectsByType($type, $subtype = null)
{
$objects = array();
$objects = [];

foreach ($this->objects as $id => $object) {
if ($object->getHeader()->get('Type') == $type &&
(is_null($subtype) || $object->getHeader()->get('Subtype') == $subtype)
(null === $subtype || $object->getHeader()->get('Subtype') == $subtype)
) {
$objects[$id] = $object;
}
Expand All @@ -209,6 +199,7 @@ public function getFonts()

/**
* @return Page[]
*
* @throws \Exception
*/
public function getPages()
Expand All @@ -221,13 +212,14 @@ public function getPages()
$object = $this->objects[$id]->get('Pages');
if (method_exists($object, 'getPages')) {
$pages = $object->getPages(true);

return $pages;
}
}

if (isset($this->dictionary['Pages'])) {
// Search for pages to list kids.
$pages = array();
$pages = [];

/** @var Pages[] $objects */
$objects = $this->getObjectsByType('Pages');
Expand Down Expand Up @@ -255,14 +247,14 @@ public function getPages()
*/
public function getText(Page $page = null)
{
$texts = array();
$texts = [];
$pages = $this->getPages();

foreach ($pages as $index => $page) {
/**
* In some cases, the $page variable may be null.
*/
if (is_null($page)) {
if (null === $page) {
continue;
}
if ($text = trim($page->getText())) {
Expand All @@ -281,9 +273,6 @@ public function getTrailer()
return $this->trailer;
}

/**
* @param Header $trailer
*/
public function setTrailer(Header $trailer)
{
$this->trailer = $trailer;
Expand Down
44 changes: 14 additions & 30 deletions src/Smalot/PdfParser/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
* @author Sébastien MALOT <[email protected]>
* @date 2017-01-03
*
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
Expand All @@ -25,7 +26,6 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/

namespace Smalot\PdfParser;
Expand All @@ -43,8 +43,6 @@

/**
* Class Element
*
* @package Smalot\PdfParser
*/
class Element
{
Expand All @@ -53,47 +51,35 @@ class Element
*/
protected $document = null;

/**
* @var mixed
*/
protected $value = null;

/**
* @param mixed $value
* @param Document $document
*/
public function __construct($value, Document $document = null)
{
$this->value = $value;
$this->value = $value;
$this->document = $document;
}

/**
*
*/
public function init()
{

}

/**
* @param mixed $value
*
* @return bool
*/
public function equals($value)
{
return ($value == $this->value);
return $value == $this->value;
}

/**
* @param mixed $value
*
* @return bool
*/
public function contains($value)
{
if (is_array($this->value)) {
if (\is_array($this->value)) {
/** @var Element $val */
foreach ($this->value as $val) {
if ($val->equals($value)) {
Expand All @@ -107,9 +93,6 @@ public function contains($value)
}
}

/**
* @return mixed
*/
public function getContent()
{
return $this->value;
Expand All @@ -120,7 +103,7 @@ public function getContent()
*/
public function __toString()
{
return (string)($this->value);
return (string) ($this->value);
}

/**
Expand All @@ -129,14 +112,15 @@ public function __toString()
* @param int $position
*
* @return array
*
* @throws \Exception
*/
public static function parse($content, Document $document = null, &$position = 0)
{
$args = func_get_args();
$args = \func_get_args();
$only_values = isset($args[3]) ? $args[3] : false;
$content = trim($content);
$values = array();
$content = trim($content);
$values = [];

do {
$old_position = $position;
Expand All @@ -145,12 +129,12 @@ public static function parse($content, Document $document = null, &$position = 0
if (!preg_match('/^\s*(?P<name>\/[A-Z0-9\._]+)(?P<value>.*)/si', substr($content, $position), $match)) {
break;
} else {
$name = ltrim($match['name'], '/');
$value = $match['value'];
$position = strpos($content, $value, $position + strlen($match['name']));
$name = ltrim($match['name'], '/');
$value = $match['value'];
$position = strpos($content, $value, $position + \strlen($match['name']));
}
} else {
$name = count($values);
$name = \count($values);
$value = substr($content, $position);
}

Expand Down Expand Up @@ -178,7 +162,7 @@ public static function parse($content, Document $document = null, &$position = 0
$position = $old_position;
break;
}
} while ($position < strlen($content));
} while ($position < \strlen($content));

return $values;
}
Expand Down
Loading

0 comments on commit 6bc9dcb

Please sign in to comment.