Skip to content

Commit 7e2b552

Browse files
authored
Merge pull request #163 from mikehaertl/fix-coding-style
Fix coding style for PSR-2 plus extra rules
2 parents 7892813 + 9ebff76 commit 7e2b552

14 files changed

+162
-166
lines changed

src/Command.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function addFile($name, $handle, $password = null)
7777
*/
7878
public function addOption($option, $argument = null, $escape = null)
7979
{
80-
$this->_options[] = $argument===null ? $option : array($option, $argument, $escape);
80+
$this->_options[] = $argument === null ? $option : array($option, $argument, $escape);
8181
return $this;
8282
}
8383

@@ -150,16 +150,18 @@ public function addPageRange($start, $end = null, $handle = null, $qualifier = n
150150
{
151151
$this->checkExecutionStatus();
152152
if (is_array($start)) {
153-
if ($handle!==null) {
154-
$start = array_map(function ($p) use ($handle) { return $handle.$p; }, $start);
153+
if ($handle !== null) {
154+
$start = array_map(function ($p) use ($handle) {
155+
return $handle . $p;
156+
}, $start);
155157
}
156158
$range = implode(' ', $start);
157159
} else {
158-
$range = $handle.$start;
160+
$range = $handle . $start;
159161
if ($end) {
160-
$range .= '-'.$end;
162+
$range .= '-' . $end;
161163
}
162-
$range .= $qualifier.$rotation;
164+
$range .= $qualifier . $rotation;
163165
}
164166
$this->_operationArgument[] = $range;
165167
return $this;
@@ -186,15 +188,15 @@ protected function processInputFiles()
186188
{
187189
$passwords = array();
188190
foreach ($this->_files as $handle => $file) {
189-
$this->addArg($handle.'=',$file['name']);
190-
if ($file['password']!==null) {
191+
$this->addArg($handle . '=', $file['name']);
192+
if ($file['password'] !== null) {
191193
$passwords[$handle] = $file['password'];
192194
}
193195
}
194-
if ($passwords!==array()) {
196+
if ($passwords !== array()) {
195197
$this->addArg('input_pw');
196198
foreach ($passwords as $handle => $password) {
197-
$this->addArg($handle.'=', $password);
199+
$this->addArg($handle . '=', $password);
198200
}
199201
}
200202
}
@@ -207,7 +209,7 @@ protected function processInputFiles()
207209
protected function processOptions($filename = null)
208210
{
209211
// output must be first option after operation
210-
if ($filename!==null) {
212+
if ($filename !== null) {
211213
$this->addArg('output', $filename, true);
212214
}
213215
foreach ($this->_options as $option) {
@@ -224,7 +226,7 @@ protected function processOptions($filename = null)
224226
*/
225227
protected function processOperation()
226228
{
227-
if ($this->_operation!==null) {
229+
if ($this->_operation !== null) {
228230
$value = $this->_operationArgument ? $this->_operationArgument : null;
229231
if ($value instanceof TmpFile) {
230232
$value = (string) $value;

src/DataFields.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DataFields extends ArrayObject
2727
public function __construct($input = null, $flags = 0, $iterator_class = "ArrayIterator")
2828
{
2929
$this->_string = $input ?: '';
30-
$this->_array = self::parse($this->_string);
30+
$this->_array = self::parse($this->_string);
3131

3232
return parent::__construct($this->_array, $flags, $iterator_class);
3333
}
@@ -84,7 +84,7 @@ public static function parse($input)
8484
// Split blocks only if '---' is followed by 'FieldType'
8585
$blocks = preg_split(
8686
'/^---(\r\n|\n|\r)(?=FieldType:)/m',
87-
substr($input,3 )
87+
substr($input, 3)
8888
);
8989
return array_map('\mikehaertl\pdftk\DataFields::parseBlock', $blocks);
9090
} else {
@@ -110,7 +110,7 @@ public static function parseBlock($block)
110110
$data = array();
111111
$lines = preg_split("/(\r\n|\n|\r)/", trim($block));
112112
$continueKey = null;
113-
foreach($lines as $n => $line) {
113+
foreach ($lines as $n => $line) {
114114
if ($continueKey !== null) {
115115
$data[$continueKey] .= "\n" . $line;
116116
if (!self::lineContinues($lines, $n, $continueKey)) {
@@ -122,7 +122,7 @@ public static function parseBlock($block)
122122
// Convert multiple keys like 'FieldStateOption' or 'FieldValue'
123123
// from Choice fields to array
124124
if (isset($data[$key])) {
125-
$data[$key] = (array) $data[$key];
125+
$data[$key] = (array) $data[$key];
126126
$data[$key][] = $value;
127127
} else {
128128
$data[$key] = $value;

src/FdfFile.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ class FdfFile extends File
4242
*/
4343
public function __construct($data, $suffix = null, $prefix = null, $directory = null, $encoding = 'UTF-8')
4444
{
45-
if ($directory===null) {
45+
if ($directory === null) {
4646
$directory = self::getTempDir();
4747
}
4848
$suffix = '.fdf';
4949
$prefix = 'php_pdftk_fdf_';
5050

51-
$this->_fileName = tempnam($directory,$prefix);
52-
$newName = $this->_fileName.$suffix;
51+
$this->_fileName = tempnam($directory, $prefix);
52+
$newName = $this->_fileName . $suffix;
5353
rename($this->_fileName, $newName);
5454
$this->_fileName = $newName;
5555

@@ -58,20 +58,20 @@ public function __construct($data, $suffix = null, $prefix = null, $directory =
5858
}
5959

6060
$fields = '';
61-
foreach ($data as $key=>$value) {
61+
foreach ($data as $key => $value) {
6262
// Create UTF-16BE string encode as ASCII hex
6363
// See http://blog.tremily.us/posts/PDF_forms/
64-
$utf16Value = mb_convert_encoding($value,'UTF-16BE', $encoding);
64+
$utf16Value = mb_convert_encoding($value, 'UTF-16BE', $encoding);
6565

6666
/* Also create UTF-16BE encoded key, this allows field names containing
6767
* german umlauts and most likely many other "special" characters.
6868
* See issue #17 (https://github.com/mikehaertl/php-pdftk/issues/17)
6969
*/
70-
$utf16Key = mb_convert_encoding($key,'UTF-16BE', $encoding);
70+
$utf16Key = mb_convert_encoding($key, 'UTF-16BE', $encoding);
7171

7272
// Escape parenthesis
73-
$utf16Value = strtr($utf16Value, array('(' => '\\(', ')'=>'\\)'));
74-
$fields .= "<</T(".chr(0xFE).chr(0xFF).$utf16Key.")/V(".chr(0xFE).chr(0xFF).$utf16Value.")>>\n";
73+
$utf16Value = strtr($utf16Value, array('(' => '\\(', ')' => '\\)'));
74+
$fields .= "<</T(" . chr(0xFE) . chr(0xFF) . $utf16Key . ")/V(" . chr(0xFE) . chr(0xFF) . $utf16Value . ")>>\n";
7575
}
7676

7777
// Use fwrite, since file_put_contents() messes around with character encoding

src/InfoFields.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace mikehaertl\pdftk;
43

54
use ArrayObject;
@@ -27,7 +26,7 @@ class InfoFields extends ArrayObject
2726
public function __construct($input = null, $flags = 0, $iterator_class = "ArrayIterator")
2827
{
2928
$this->_string = $input ?: '';
30-
$this->_array = $this->parseData($this->_string);
29+
$this->_array = $this->parseData($this->_string);
3130

3231
return parent::__construct($this->_array, $flags, $iterator_class);
3332
}
@@ -70,66 +69,61 @@ public function __toArray()
7069
private function parseData($dataString)
7170
{
7271
$expectType = null;
73-
$output = array('Info'=>array(),'Bookmark'=>array(),'PageMedia'=>array());
74-
$field = array();
72+
$output = array('Info' => array(),'Bookmark' => array(),'PageMedia' => array());
73+
$field = array();
7574
$buffer = array();
7675
foreach (explode(PHP_EOL, $dataString) as $line) {
7776
$trimmedLine = trim($line);
78-
if($trimmedLine === 'InfoBegin') {
77+
if ($trimmedLine === 'InfoBegin') {
7978
$expectType = 'Info';
8079
continue;
8180
}
82-
if($trimmedLine === 'BookmarkBegin') {
81+
if ($trimmedLine === 'BookmarkBegin') {
8382
$expectType = 'Bookmark';
8483
continue;
8584
}
86-
if($trimmedLine === 'PageMediaBegin') {
85+
if ($trimmedLine === 'PageMediaBegin') {
8786
$expectType = 'PageMedia';
8887
continue;
8988
}
90-
89+
9190
preg_match('/([^:]*): ?(.*)/', $trimmedLine, $match);
9291
$key = $match[1];
9392
$value = $match[2];
94-
95-
if($expectType == 'Info'){
96-
if($key == 'InfoKey') {
93+
94+
if ($expectType === 'Info') {
95+
if ($key === 'InfoKey') {
9796
$buffer['Key'] = $value;
98-
}
99-
elseif($key == 'InfoValue') {
97+
} elseif ($key === 'InfoValue') {
10098
$buffer['Value'] = $value;
10199
}
102-
if(isset($buffer['Value']) && isset($buffer['Key'])) {
100+
if (isset($buffer['Value'], $buffer['Key'])) {
103101
$output['Info'][$buffer['Key']] = $buffer['Value'];
104102
$buffer = array();
105103
$expectType = null;
106104
}
107105
continue;
108106
}
109-
if(!is_null($expectType)){
110-
if(strpos($key, $expectType) === 0) {
107+
if ($expectType !== null) {
108+
if (strpos($key, $expectType) === 0) {
111109
$buffer[str_replace($expectType, '', $key)] = $value;
112-
}
113-
else{
110+
} else {
114111
throw new \Exception("Unexpected input");
115112
}
116-
if($expectType == 'Bookmark' && isset($buffer['Level']) && isset($buffer['Title']) && isset($buffer['PageNumber'])) {
113+
if ($expectType === 'Bookmark' && isset($buffer['Level'], $buffer['Title'], $buffer['PageNumber'])) {
117114
$output[$expectType][] = $buffer;
118115
$buffer = array();
119116
$expectType = null;
120-
}
121-
elseif($expectType == 'PageMedia' && isset($buffer['Number']) && isset($buffer['Rotation']) && isset($buffer['Rect']) && isset($buffer['Dimensions'])) {
117+
} elseif ($expectType === 'PageMedia' && isset($buffer['Number'], $buffer['Rotation'], $buffer['Rect'], $buffer['Dimensions'])) {
122118
$output[$expectType][] = $buffer;
123119
$buffer = array();
124120
$expectType = null;
125121
}
126122
continue;
127-
}
128-
else{
123+
} else {
129124
$output[$key] = $value;
130125
}
131126
}
132127
return $output;
133128
}
134-
135129
}

src/InfoFile.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class InfoFile extends File
2626
*/
2727
public function __construct($data, $suffix = null, $prefix = null, $directory = null, $encoding = 'UTF-8')
2828
{
29-
if ($directory===null) {
29+
if ($directory === null) {
3030
$directory = self::getTempDir();
3131
}
3232
$suffix = '.txt';
3333
$prefix = 'php_pdftk_info_';
3434

35-
$this->_fileName = tempnam($directory,$prefix);
36-
$newName = $this->_fileName.$suffix;
35+
$this->_fileName = tempnam($directory, $prefix);
36+
$newName = $this->_fileName . $suffix;
3737
rename($this->_fileName, $newName);
3838
$this->_fileName = $newName;
3939

@@ -42,11 +42,11 @@ public function __construct($data, $suffix = null, $prefix = null, $directory =
4242
}
4343

4444
$fields = '';
45-
foreach ($data as $key=>$value) {
45+
foreach ($data as $key => $value) {
4646
// Always convert to UTF-8
47-
if ($encoding!=='UTF-8' && function_exists('mb_convert_encoding')) {
48-
$value = mb_convert_encoding($value,'UTF-8', $encoding);
49-
$key = mb_convert_encoding($key,'UTF-8', $encoding);
47+
if ($encoding !== 'UTF-8' && function_exists('mb_convert_encoding')) {
48+
$value = mb_convert_encoding($value, 'UTF-8', $encoding);
49+
$key = mb_convert_encoding($key, 'UTF-8', $encoding);
5050
$value = defined('ENT_XML1') ? htmlspecialchars($key, ENT_XML1, 'UTF-8') : htmlspecialchars($key);
5151
$key = defined('ENT_XML1') ? htmlspecialchars($value, ENT_XML1, 'UTF-8') : htmlspecialchars($value);
5252
}

0 commit comments

Comments
 (0)