Skip to content

Commit 6bc9dcb

Browse files
authored
Merge pull request #292 from smalot/feature/add-cs-fixer
Add FriendsOfPHP/PHP-CS-Fixer to "require-dev" to enforce coding styles
2 parents f7fac8e + 91be9eb commit 6bc9dcb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1201
-1354
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
/composer
99
debug*
1010
composer.lock
11+
/.php_cs.cache

.php_cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
return PhpCsFixer\Config::create()
4+
->setRules([
5+
'@Symfony' => true,
6+
'@Symfony:risky' => true,
7+
'array_syntax' => ['syntax' => 'short'],
8+
'no_empty_phpdoc' => true,
9+
'no_unused_imports' => true,
10+
'no_superfluous_phpdoc_tags' => true,
11+
'ordered_imports' => true,
12+
'phpdoc_summary' => false,
13+
'protected_to_private' => false,
14+
])
15+
->setRiskyAllowed(true)
16+
->setFinder(
17+
PhpCsFixer\Finder::create()
18+
->files()
19+
->in(__DIR__ . '/samples')
20+
->in(__DIR__ . '/src')
21+
->name('*.php')
22+
);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"tecnickcom/tcpdf": "^6.2.22"
2222
},
2323
"require-dev": {
24-
"atoum/atoum": "^3.1"
24+
"atoum/atoum": "^3.1",
25+
"friendsofphp/php-cs-fixer": "^2.16.3"
2526
},
2627
"autoload": {
2728
"psr-0": {

src/Smalot/PdfParser/Document.php

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
* @author Sébastien MALOT <[email protected]>
88
* @date 2017-01-03
9+
*
910
* @license LGPLv3
1011
* @url <https://github.com/smalot/pdfparser>
1112
*
@@ -25,13 +26,10 @@
2526
* You should have received a copy of the GNU Lesser General Public License
2627
* along with this program.
2728
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
28-
*
2929
*/
3030

3131
namespace Smalot\PdfParser;
3232

33-
use Smalot\PdfParser\Element\ElementDate;
34-
3533
/**
3634
* Technical references :
3735
* - http://www.mactech.com/articles/mactech/Vol.15/15.09/PDFIntro/index.html
@@ -43,20 +41,18 @@
4341
* - http://cpansearch.perl.org/src/JV/PostScript-Font-1.10.02/lib/PostScript/WinAnsiEncoding.pm
4442
*
4543
* Class Document
46-
*
47-
* @package Smalot\PdfParser
4844
*/
4945
class Document
5046
{
5147
/**
5248
* @var PDFObject[]
5349
*/
54-
protected $objects = array();
50+
protected $objects = [];
5551

5652
/**
5753
* @var array
5854
*/
59-
protected $dictionary = array();
55+
protected $dictionary = [];
6056

6157
/**
6258
* @var Header
@@ -68,17 +64,11 @@ class Document
6864
*/
6965
protected $details = null;
7066

71-
/**
72-
*
73-
*/
7467
public function __construct()
7568
{
76-
$this->trailer = new Header(array(), $this);
69+
$this->trailer = new Header([], $this);
7770
}
7871

79-
/**
80-
*
81-
*/
8272
public function init()
8373
{
8474
$this->buildDictionary();
@@ -97,7 +87,7 @@ public function init()
9787
protected function buildDictionary()
9888
{
9989
// Build dictionary.
100-
$this->dictionary = array();
90+
$this->dictionary = [];
10191

10292
foreach ($this->objects as $id => $object) {
10393
$type = $object->getHeader()->get('Type')->getContent();
@@ -114,23 +104,23 @@ protected function buildDictionary()
114104
protected function buildDetails()
115105
{
116106
// Build details array.
117-
$details = array();
107+
$details = [];
118108

119109
// Extract document info
120110
if ($this->trailer->has('Info')) {
121111
/** @var PDFObject $info */
122112
$info = $this->trailer->get('Info');
123113
// This could be an ElementMissing object, so we need to check for
124114
// the getHeader method first.
125-
if ($info !== null && method_exists($info, 'getHeader')) {
115+
if (null !== $info && method_exists($info, 'getHeader')) {
126116
$details = $info->getHeader()->getDetails();
127117
}
128118
}
129119

130120
// Retrieve the page count
131121
try {
132122
$pages = $this->getPages();
133-
$details['Pages'] = count($pages);
123+
$details['Pages'] = \count($pages);
134124
} catch (\Exception $e) {
135125
$details['Pages'] = 0;
136126
}
@@ -149,9 +139,9 @@ public function getDictionary()
149139
/**
150140
* @param PDFObject[] $objects
151141
*/
152-
public function setObjects($objects = array())
142+
public function setObjects($objects = [])
153143
{
154-
$this->objects = (array)$objects;
144+
$this->objects = (array) $objects;
155145

156146
$this->init();
157147
}
@@ -186,11 +176,11 @@ public function getObjectById($id)
186176
*/
187177
public function getObjectsByType($type, $subtype = null)
188178
{
189-
$objects = array();
179+
$objects = [];
190180

191181
foreach ($this->objects as $id => $object) {
192182
if ($object->getHeader()->get('Type') == $type &&
193-
(is_null($subtype) || $object->getHeader()->get('Subtype') == $subtype)
183+
(null === $subtype || $object->getHeader()->get('Subtype') == $subtype)
194184
) {
195185
$objects[$id] = $object;
196186
}
@@ -209,6 +199,7 @@ public function getFonts()
209199

210200
/**
211201
* @return Page[]
202+
*
212203
* @throws \Exception
213204
*/
214205
public function getPages()
@@ -221,13 +212,14 @@ public function getPages()
221212
$object = $this->objects[$id]->get('Pages');
222213
if (method_exists($object, 'getPages')) {
223214
$pages = $object->getPages(true);
215+
224216
return $pages;
225217
}
226218
}
227219

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

232224
/** @var Pages[] $objects */
233225
$objects = $this->getObjectsByType('Pages');
@@ -255,14 +247,14 @@ public function getPages()
255247
*/
256248
public function getText(Page $page = null)
257249
{
258-
$texts = array();
250+
$texts = [];
259251
$pages = $this->getPages();
260252

261253
foreach ($pages as $index => $page) {
262254
/**
263255
* In some cases, the $page variable may be null.
264256
*/
265-
if (is_null($page)) {
257+
if (null === $page) {
266258
continue;
267259
}
268260
if ($text = trim($page->getText())) {
@@ -281,9 +273,6 @@ public function getTrailer()
281273
return $this->trailer;
282274
}
283275

284-
/**
285-
* @param Header $trailer
286-
*/
287276
public function setTrailer(Header $trailer)
288277
{
289278
$this->trailer = $trailer;

src/Smalot/PdfParser/Element.php

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
* @author Sébastien MALOT <[email protected]>
88
* @date 2017-01-03
9+
*
910
* @license LGPLv3
1011
* @url <https://github.com/smalot/pdfparser>
1112
*
@@ -25,7 +26,6 @@
2526
* You should have received a copy of the GNU Lesser General Public License
2627
* along with this program.
2728
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
28-
*
2929
*/
3030

3131
namespace Smalot\PdfParser;
@@ -43,8 +43,6 @@
4343

4444
/**
4545
* Class Element
46-
*
47-
* @package Smalot\PdfParser
4846
*/
4947
class Element
5048
{
@@ -53,47 +51,35 @@ class Element
5351
*/
5452
protected $document = null;
5553

56-
/**
57-
* @var mixed
58-
*/
5954
protected $value = null;
6055

6156
/**
62-
* @param mixed $value
6357
* @param Document $document
6458
*/
6559
public function __construct($value, Document $document = null)
6660
{
67-
$this->value = $value;
61+
$this->value = $value;
6862
$this->document = $document;
6963
}
7064

71-
/**
72-
*
73-
*/
7465
public function init()
7566
{
76-
7767
}
7868

7969
/**
80-
* @param mixed $value
81-
*
8270
* @return bool
8371
*/
8472
public function equals($value)
8573
{
86-
return ($value == $this->value);
74+
return $value == $this->value;
8775
}
8876

8977
/**
90-
* @param mixed $value
91-
*
9278
* @return bool
9379
*/
9480
public function contains($value)
9581
{
96-
if (is_array($this->value)) {
82+
if (\is_array($this->value)) {
9783
/** @var Element $val */
9884
foreach ($this->value as $val) {
9985
if ($val->equals($value)) {
@@ -107,9 +93,6 @@ public function contains($value)
10793
}
10894
}
10995

110-
/**
111-
* @return mixed
112-
*/
11396
public function getContent()
11497
{
11598
return $this->value;
@@ -120,7 +103,7 @@ public function getContent()
120103
*/
121104
public function __toString()
122105
{
123-
return (string)($this->value);
106+
return (string) ($this->value);
124107
}
125108

126109
/**
@@ -129,14 +112,15 @@ public function __toString()
129112
* @param int $position
130113
*
131114
* @return array
115+
*
132116
* @throws \Exception
133117
*/
134118
public static function parse($content, Document $document = null, &$position = 0)
135119
{
136-
$args = func_get_args();
120+
$args = \func_get_args();
137121
$only_values = isset($args[3]) ? $args[3] : false;
138-
$content = trim($content);
139-
$values = array();
122+
$content = trim($content);
123+
$values = [];
140124

141125
do {
142126
$old_position = $position;
@@ -145,12 +129,12 @@ public static function parse($content, Document $document = null, &$position = 0
145129
if (!preg_match('/^\s*(?P<name>\/[A-Z0-9\._]+)(?P<value>.*)/si', substr($content, $position), $match)) {
146130
break;
147131
} else {
148-
$name = ltrim($match['name'], '/');
149-
$value = $match['value'];
150-
$position = strpos($content, $value, $position + strlen($match['name']));
132+
$name = ltrim($match['name'], '/');
133+
$value = $match['value'];
134+
$position = strpos($content, $value, $position + \strlen($match['name']));
151135
}
152136
} else {
153-
$name = count($values);
137+
$name = \count($values);
154138
$value = substr($content, $position);
155139
}
156140

@@ -178,7 +162,7 @@ public static function parse($content, Document $document = null, &$position = 0
178162
$position = $old_position;
179163
break;
180164
}
181-
} while ($position < strlen($content));
165+
} while ($position < \strlen($content));
182166

183167
return $values;
184168
}

0 commit comments

Comments
 (0)