33namespace mikehaertl \pdftk ;
44
55use 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 ' );
0 commit comments