Skip to content

Commit bc594e0

Browse files
committed
fix: apply fixes reported by psalm
Signed-off-by: Vitor Mattos <[email protected]>
1 parent 3189686 commit bc594e0

File tree

7 files changed

+203
-162
lines changed

7 files changed

+203
-162
lines changed

src/Command.php

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace mikehaertl\pdftk;
44

55
use mikehaertl\shellcommand\Command as BaseCommand;
6+
use mikehaertl\tmp\File;
7+
use Override;
68

79
/**
810
* Command
@@ -16,38 +18,35 @@
1618
class Command extends BaseCommand
1719
{
1820
/**
19-
* @var string the pdftk binary
21+
* The pdftk binary
2022
*/
2123
protected $_command = 'pdftk';
2224

2325
/**
24-
* @var array list of input files to process as array('name' => $filename,
26+
* List of input files to process as array('name' => $filename,
2527
* 'password' => $pw) indexed by handle
2628
*/
27-
protected $_files = array();
29+
protected array $_files = array();
2830

2931
/**
30-
* @var array list of command options, either strings or array with
31-
* arguments to addArg()
32+
* List of command options, either strings or array with arguments to addArg()
3233
*/
33-
protected $_options = array();
34+
protected array $_options = array();
3435

3536
/**
36-
* @var string the operation to perform
37+
* The operation to perform
3738
*/
38-
protected $_operation;
39+
protected ?string $_operation = null;
3940

4041
/**
41-
* @var string|array operation arguments, e.g. a list of page ranges or a
42-
* filename or tmp file instance
42+
* Operation arguments, e.g. a list of page ranges or a filename or tmp file instance
4343
*/
44-
protected $_operationArgument = array();
44+
protected string|array|File $_operationArgument = array();
4545

4646
/**
47-
* @var bool whether to force escaping of the operation argument e.g. for
48-
* filenames
47+
* Whether to force escaping of the operation argument e.g. for filenames
4948
*/
50-
protected $_escapeOperationArgument = false;
49+
protected bool $_escapeOperationArgument = false;
5150

5251
/**
5352
* @param string $name the PDF file to add for processing
@@ -57,7 +56,7 @@ class Command extends BaseCommand
5756
* @return Command the command instance for method chaining
5857
* @throws \Exception
5958
*/
60-
public function addFile($name, $handle, $password = null)
59+
public function addFile($name, $handle, $password = null): self
6160
{
6261
$this->checkExecutionStatus();
6362
$file = array(
@@ -76,7 +75,7 @@ public function addFile($name, $handle, $password = null)
7675
* use Command default setting.
7776
* @return Command the command instance for method chaining
7877
*/
79-
public function addOption($option, $argument = null, $escape = null)
78+
public function addOption($option, $argument = null, ?bool $escape = null): self
8079
{
8180
$this->_options[] = $argument === null ? $option : array($option, $argument, $escape);
8281
return $this;
@@ -86,7 +85,7 @@ public function addOption($option, $argument = null, $escape = null)
8685
* @param string $operation the operation to perform
8786
* @return Command the command instance for method chaining
8887
*/
89-
public function setOperation($operation)
88+
public function setOperation($operation): self
9089
{
9190
$this->checkExecutionStatus();
9291
$this->_operation = $operation;
@@ -102,11 +101,11 @@ public function getOperation()
102101
}
103102

104103
/**
105-
* @param string $value the operation argument
104+
* @param string|array $value the operation argument
106105
* @param bool $escape whether to escape the operation argument
107106
* @return Command the command instance for method chaining
108107
*/
109-
public function setOperationArgument($value, $escape = false)
108+
public function setOperationArgument(string|array|File $value, bool $escape = false): self
110109
{
111110
$this->checkExecutionStatus();
112111
$this->_operationArgument = $value;
@@ -118,7 +117,7 @@ public function setOperationArgument($value, $escape = false)
118117
* @return string|array|null the current operation argument as string or
119118
* array or null if none set
120119
*/
121-
public function getOperationArgument()
120+
public function getOperationArgument(): string|array|null
122121
{
123122
// Typecast to string in case we have a File instance as argument
124123
return is_array($this->_operationArgument) ? $this->_operationArgument : (string) $this->_operationArgument;
@@ -127,7 +126,7 @@ public function getOperationArgument()
127126
/**
128127
* @return int the number of files added to the command
129128
*/
130-
public function getFileCount()
129+
public function getFileCount(): int
131130
{
132131
return count($this->_files);
133132
}
@@ -144,17 +143,20 @@ public function getFileCount()
144143
* only a single file was added.
145144
* @param string|null $qualifier the page number qualifier, either 'even'
146145
* or 'odd' or null for none
147-
* @param string $rotation the rotation to apply to the pages.
146+
* @param string|null $rotation the rotation to apply to the pages.
148147
* @return Command the command instance for method chaining
149148
*/
150-
public function addPageRange($start, $end = null, $handle = null, $qualifier = null, $rotation = null)
151-
{
149+
public function addPageRange(
150+
int|string|array $start,
151+
int|string|null $end = null,
152+
string|null $handle = null,
153+
string|null $qualifier = null,
154+
string|null $rotation = null,
155+
) {
152156
$this->checkExecutionStatus();
153157
if (is_array($start)) {
154158
if ($handle !== null) {
155-
$start = array_map(function ($p) use ($handle) {
156-
return $handle . $p;
157-
}, $start);
159+
$start = array_map(fn ($p) => $handle . $p, $start);
158160
}
159161
$range = implode(' ', $start);
160162
} else {
@@ -164,6 +166,13 @@ public function addPageRange($start, $end = null, $handle = null, $qualifier = n
164166
}
165167
$range .= $qualifier . $rotation;
166168
}
169+
if (!is_array($this->_operationArgument)) {
170+
if (!empty($this->_operationArgument)) {
171+
$this->_operationArgument = array($this->_operationArgument);
172+
} else {
173+
$this->_operationArgument = array();
174+
}
175+
}
167176
$this->_operationArgument[] = $range;
168177
return $this;
169178
}
@@ -173,6 +182,7 @@ public function addPageRange($start, $end = null, $handle = null, $qualifier = n
173182
* null if none
174183
* @return bool whether the command was executed successfully
175184
*/
185+
#[Override]
176186
public function execute($filename = null)
177187
{
178188
$this->checkExecutionStatus();
@@ -185,7 +195,7 @@ public function execute($filename = null)
185195
/**
186196
* Process input PDF files and create respective command arguments
187197
*/
188-
protected function processInputFiles()
198+
protected function processInputFiles(): void
189199
{
190200
$passwords = array();
191201
foreach ($this->_files as $handle => $file) {
@@ -204,10 +214,11 @@ protected function processInputFiles()
204214

205215
/**
206216
* Process options and create respective command arguments
217+
*
207218
* @param string|null $filename if provided an 'output' option will be
208219
* added
209220
*/
210-
protected function processOptions($filename = null)
221+
protected function processOptions($filename = null): void
211222
{
212223
// output must be first option after operation
213224
if ($filename !== null) {
@@ -225,23 +236,21 @@ protected function processOptions($filename = null)
225236
/**
226237
* Process opearation and create respective command arguments
227238
*/
228-
protected function processOperation()
239+
protected function processOperation(): void
229240
{
230241
if ($this->_operation !== null) {
231242
$value = $this->_operationArgument ? $this->_operationArgument : null;
232-
if ($value instanceof TmpFile) {
233-
$value = (string) $value;
234-
}
235243
$this->addArg($this->_operation, $value, $this->_escapeOperationArgument);
236244
}
237245
}
238246

239247
/**
240248
* Ensure that the command was not exectued yet. Throws exception
241249
* otherwise.
250+
*
242251
* @throws \Exception
243252
*/
244-
protected function checkExecutionStatus()
253+
protected function checkExecutionStatus(): void
245254
{
246255
if ($this->getExecuted()) {
247256
throw new \Exception('Operation was already executed');

src/DataFields.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
* @author Ray Holland <[email protected]>
1212
* @author Michael Härtl <[email protected]>
1313
* @license http://www.opensource.org/licenses/MIT
14+
* @extends ArrayObject<string, mixed>
1415
*/
1516
class DataFields extends ArrayObject
1617
{
17-
private $_string;
18-
private $_array;
18+
private string $_string;
19+
private array $_array;
1920

2021
/**
2122
* DataFields constructor.
@@ -24,26 +25,23 @@ class DataFields extends ArrayObject
2425
* @param int $flags
2526
* @param string $iterator_class
2627
*/
27-
public function __construct($input = null, $flags = 0, $iterator_class = "ArrayIterator")
28-
{
28+
public function __construct(
29+
?string $input = null,
30+
int $flags = 0,
31+
string $iterator_class = "ArrayIterator",
32+
) {
2933
$this->_string = $input ?: '';
3034
$this->_array = self::parse($this->_string);
3135

32-
return parent::__construct($this->_array, $flags, $iterator_class);
36+
parent::__construct($this->_array, $flags, $iterator_class);
3337
}
3438

35-
/**
36-
* @return string
37-
*/
38-
public function __toString()
39+
public function __toString(): string
3940
{
4041
return $this->_string;
4142
}
4243

43-
/**
44-
* @return array
45-
*/
46-
public function __toArray()
44+
public function __toArray(): array
4745
{
4846
return $this->_array;
4947
}
@@ -78,7 +76,7 @@ public function __toArray()
7876
* @param $input the string to parse
7977
* @return array the parsed result
8078
*/
81-
public static function parse($input)
79+
public static function parse(string $input): array
8280
{
8381
if (strncmp('---', $input, 3) === 0) {
8482
// Split blocks only if '---' is followed by 'FieldType'
@@ -105,7 +103,7 @@ public static function parse($input)
105103
* @param string $block the block to parse
106104
* @return array the parsed block values indexed by respective names
107105
*/
108-
public static function parseBlock($block)
106+
public static function parseBlock(string $block): array
109107
{
110108
$data = array();
111109
$lines = preg_split("/(\r\n|\n|\r)/", trim($block));
@@ -156,7 +154,7 @@ public static function parseBlock($block)
156154
* 'FieldValueDefault' can span multiple lines
157155
* @return bool whether the value continues in line n + 1
158156
*/
159-
protected static function lineContinues($lines, $n, $key)
157+
protected static function lineContinues(array $lines, int $n, string $key): bool
160158
{
161159
return
162160
in_array($key, array('FieldValue', 'FieldValueDefault')) &&

src/FdfFile.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ class FdfFile extends File
4141
* created. Autodetected if not provided.
4242
* @param string|null $encoding of the data. Default is 'UTF-8'.
4343
*/
44-
public function __construct($data, $suffix = null, $prefix = null, $directory = null, $encoding = 'UTF-8')
45-
{
44+
public function __construct(
45+
array $data,
46+
?string $suffix = null,
47+
?string $prefix = null,
48+
?string $directory = null,
49+
?string $encoding = 'UTF-8',
50+
) {
4651
if ($directory === null) {
4752
$directory = self::getTempDir();
4853
}

src/InfoFields.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
*
1111
* @author Burak USGURLU <[email protected]>
1212
* @license http://www.opensource.org/licenses/MIT
13+
* @extends ArrayObject<string, mixed>
1314
*/
1415
class InfoFields extends ArrayObject
1516
{
16-
private $_string;
17+
private string $_string;
1718

18-
private $_array;
19+
private array $_array;
1920

2021
/**
2122
* InfoFields constructor.
@@ -24,26 +25,23 @@ class InfoFields extends ArrayObject
2425
* @param int $flags
2526
* @param string $iterator_class
2627
*/
27-
public function __construct($input = null, $flags = 0, $iterator_class = "ArrayIterator")
28-
{
28+
public function __construct(
29+
?string $input = null,
30+
int $flags = 0,
31+
string $iterator_class = "ArrayIterator",
32+
) {
2933
$this->_string = $input ?: '';
3034
$this->_array = $this->parseData($this->_string);
3135

32-
return parent::__construct($this->_array, $flags, $iterator_class);
36+
parent::__construct($this->_array, $flags, $iterator_class);
3337
}
3438

35-
/**
36-
* @return string
37-
*/
38-
public function __toString()
39+
public function __toString(): string
3940
{
4041
return $this->_string;
4142
}
4243

43-
/**
44-
* @return array
45-
*/
46-
public function __toArray()
44+
public function __toArray(): array
4745
{
4846
return $this->_array;
4947
}
@@ -70,11 +68,8 @@ public function __toArray()
7068
* BookmarkTitle: Second bookmark
7169
* BookmarkLevel: 1
7270
* BookmarkPageNumber: 2
73-
*
74-
* @param $dataString
75-
* @return array
7671
*/
77-
private function parseData($dataString)
72+
private function parseData(string $dataString): array
7873
{
7974
$output = array();
8075
foreach (explode(PHP_EOL, $dataString) as $line) {

0 commit comments

Comments
 (0)