Skip to content

Commit 48c0b9e

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

File tree

8 files changed

+234
-145
lines changed

8 files changed

+234
-145
lines changed

.vscode/launch.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
8+
{
9+
"name": "Listen for Xdebug",
10+
"type": "php",
11+
"request": "launch",
12+
"port": 9003,
13+
"pathMappings": {
14+
"/var/www/html/apps-extra/libresign/php-pdftk": "${workspaceFolder}"
15+
}
16+
},
17+
{
18+
"name": "Launch currently open script",
19+
"type": "php",
20+
"request": "launch",
21+
"program": "${file}",
22+
"cwd": "${fileDirname}",
23+
"port": 0,
24+
"runtimeArgs": [
25+
"-dxdebug.start_with_request=yes"
26+
],
27+
"env": {
28+
"XDEBUG_MODE": "debug,develop",
29+
"XDEBUG_CONFIG": "client_port=${port}"
30+
}
31+
},
32+
{
33+
"name": "Launch Built-in web server",
34+
"type": "php",
35+
"request": "launch",
36+
"runtimeArgs": [
37+
"-dxdebug.mode=debug",
38+
"-dxdebug.start_with_request=yes",
39+
"-S",
40+
"localhost:0"
41+
],
42+
"program": "",
43+
"cwd": "${workspaceRoot}",
44+
"port": 9003,
45+
"serverReadyAction": {
46+
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
47+
"uriFormat": "http://localhost:%s",
48+
"action": "openExternally"
49+
}
50+
}
51+
]
52+
}

src/Command.php

Lines changed: 30 additions & 25 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
@@ -24,30 +26,30 @@ class Command extends BaseCommand
2426
* @var array 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
/**
3032
* @var array list of command options, either strings or array with
3133
* arguments to addArg()
3234
*/
33-
protected $_options = array();
35+
protected array $_options = array();
3436

3537
/**
3638
* @var string the operation to perform
3739
*/
38-
protected $_operation;
40+
protected ?string $_operation = null;
3941

4042
/**
4143
* @var string|array operation arguments, e.g. a list of page ranges or a
4244
* filename or tmp file instance
4345
*/
44-
protected $_operationArgument = array();
46+
protected string|array|File $_operationArgument = array();
4547

4648
/**
4749
* @var bool whether to force escaping of the operation argument e.g. for
4850
* filenames
4951
*/
50-
protected $_escapeOperationArgument = false;
52+
protected bool $_escapeOperationArgument = false;
5153

5254
/**
5355
* @param string $name the PDF file to add for processing
@@ -57,7 +59,7 @@ class Command extends BaseCommand
5759
* @return Command the command instance for method chaining
5860
* @throws \Exception
5961
*/
60-
public function addFile($name, $handle, $password = null)
62+
public function addFile($name, $handle, $password = null): self
6163
{
6264
$this->checkExecutionStatus();
6365
$file = array(
@@ -76,7 +78,7 @@ public function addFile($name, $handle, $password = null)
7678
* use Command default setting.
7779
* @return Command the command instance for method chaining
7880
*/
79-
public function addOption($option, $argument = null, $escape = null)
81+
public function addOption($option, $argument = null, ?bool $escape = null): self
8082
{
8183
$this->_options[] = $argument === null ? $option : array($option, $argument, $escape);
8284
return $this;
@@ -86,7 +88,7 @@ public function addOption($option, $argument = null, $escape = null)
8688
* @param string $operation the operation to perform
8789
* @return Command the command instance for method chaining
8890
*/
89-
public function setOperation($operation)
91+
public function setOperation($operation): self
9092
{
9193
$this->checkExecutionStatus();
9294
$this->_operation = $operation;
@@ -102,11 +104,11 @@ public function getOperation()
102104
}
103105

104106
/**
105-
* @param string $value the operation argument
107+
* @param string|array $value the operation argument
106108
* @param bool $escape whether to escape the operation argument
107109
* @return Command the command instance for method chaining
108110
*/
109-
public function setOperationArgument($value, $escape = false)
111+
public function setOperationArgument(string|array|File $value, bool $escape = false): self
110112
{
111113
$this->checkExecutionStatus();
112114
$this->_operationArgument = $value;
@@ -118,7 +120,7 @@ public function setOperationArgument($value, $escape = false)
118120
* @return string|array|null the current operation argument as string or
119121
* array or null if none set
120122
*/
121-
public function getOperationArgument()
123+
public function getOperationArgument(): string|array|null
122124
{
123125
// Typecast to string in case we have a File instance as argument
124126
return is_array($this->_operationArgument) ? $this->_operationArgument : (string) $this->_operationArgument;
@@ -127,7 +129,7 @@ public function getOperationArgument()
127129
/**
128130
* @return int the number of files added to the command
129131
*/
130-
public function getFileCount()
132+
public function getFileCount(): int
131133
{
132134
return count($this->_files);
133135
}
@@ -144,17 +146,20 @@ public function getFileCount()
144146
* only a single file was added.
145147
* @param string|null $qualifier the page number qualifier, either 'even'
146148
* or 'odd' or null for none
147-
* @param string $rotation the rotation to apply to the pages.
149+
* @param string|null $rotation the rotation to apply to the pages.
148150
* @return Command the command instance for method chaining
149151
*/
150-
public function addPageRange($start, $end = null, $handle = null, $qualifier = null, $rotation = null)
151-
{
152+
public function addPageRange(
153+
int|string|array $start,
154+
int|string|null $end = null,
155+
string|null $handle = null,
156+
string|null $qualifier = null,
157+
string|null $rotation = null,
158+
) {
152159
$this->checkExecutionStatus();
153160
if (is_array($start)) {
154161
if ($handle !== null) {
155-
$start = array_map(function ($p) use ($handle) {
156-
return $handle . $p;
157-
}, $start);
162+
$start = array_map(fn($p) => $handle . $p, $start);
158163
}
159164
$range = implode(' ', $start);
160165
} else {
@@ -173,6 +178,7 @@ public function addPageRange($start, $end = null, $handle = null, $qualifier = n
173178
* null if none
174179
* @return bool whether the command was executed successfully
175180
*/
181+
#[Override]
176182
public function execute($filename = null)
177183
{
178184
$this->checkExecutionStatus();
@@ -185,7 +191,7 @@ public function execute($filename = null)
185191
/**
186192
* Process input PDF files and create respective command arguments
187193
*/
188-
protected function processInputFiles()
194+
protected function processInputFiles(): void
189195
{
190196
$passwords = array();
191197
foreach ($this->_files as $handle => $file) {
@@ -204,10 +210,11 @@ protected function processInputFiles()
204210

205211
/**
206212
* Process options and create respective command arguments
213+
*
207214
* @param string|null $filename if provided an 'output' option will be
208215
* added
209216
*/
210-
protected function processOptions($filename = null)
217+
protected function processOptions($filename = null): void
211218
{
212219
// output must be first option after operation
213220
if ($filename !== null) {
@@ -225,23 +232,21 @@ protected function processOptions($filename = null)
225232
/**
226233
* Process opearation and create respective command arguments
227234
*/
228-
protected function processOperation()
235+
protected function processOperation(): void
229236
{
230237
if ($this->_operation !== null) {
231238
$value = $this->_operationArgument ? $this->_operationArgument : null;
232-
if ($value instanceof TmpFile) {
233-
$value = (string) $value;
234-
}
235239
$this->addArg($this->_operation, $value, $this->_escapeOperationArgument);
236240
}
237241
}
238242

239243
/**
240244
* Ensure that the command was not exectued yet. Throws exception
241245
* otherwise.
246+
*
242247
* @throws \Exception
243248
*/
244-
protected function checkExecutionStatus()
249+
protected function checkExecutionStatus(): void
245250
{
246251
if ($this->getExecuted()) {
247252
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)