11<?php
2- //----------------------------------------------------------------------------------------------------------------------
2+ declare (strict_types=1 );
3+
34namespace SetBased \Helper ;
45
56use SetBased \Exception \ProgramExecutionException ;
67
7- //----------------------------------------------------------------------------------------------------------------------
88/**
99 * A helper class for program execution.
1010 */
@@ -21,7 +21,7 @@ class ProgramExecution
2121 * @since 1.0.0
2222 * @api
2323 */
24- public static function escape ($ args )
24+ public static function escape (array $ args ): string
2525 {
2626 $ command = '' ;
2727 foreach ($ args as $ arg )
@@ -39,7 +39,7 @@ public static function escape($args)
3939 *
4040 * @param string[] $command The command that will be executed. This method will compose the command executed
4141 * by exec with proper escaping.
42- * @param int[]|null $returnVars The allowed return statuses. If the return status of the command is not in
42+ * @param int[]|null $statuses The allowed return statuses. If the return status of the command is not in
4343 * this array an exception will be thrown. Null will allow all return statuses and an
4444 * empty array will throw an exception always.
4545 * @param bool $ignoreStdErr The standard error is normally redirected to standard output. If true standard
@@ -51,7 +51,7 @@ public static function escape($args)
5151 * @since 1.0.0
5252 * @api
5353 */
54- public static function exec1 ($ command , $ returnVars = [0 ], $ ignoreStdErr = false )
54+ public static function exec1 (array $ command , ? array $ statuses = [0 ], bool $ ignoreStdErr = false ): array
5555 {
5656 $ command = self ::escape ($ command );
5757
@@ -64,14 +64,14 @@ public static function exec1($command, $returnVars = [0], $ignoreStdErr = false)
6464 $ command .= ' 2>&1 ' ;
6565 }
6666
67- exec ($ command , $ output , $ return_var );
67+ exec ($ command , $ output , $ status );
6868
69- if (is_array ($ returnVars ) && !in_array ($ return_var , $ returnVars ))
69+ if (is_array ($ statuses ) && !in_array ($ status , $ statuses ))
7070 {
71- throw new ProgramExecutionException ($ command , $ return_var , $ output );
71+ throw new ProgramExecutionException ($ command , $ status , $ output );
7272 }
7373
74- return [$ output , $ return_var ];
74+ return [$ output , $ status ];
7575 }
7676
7777 //--------------------------------------------------------------------------------------------------------------------
@@ -80,11 +80,11 @@ public static function exec1($command, $returnVars = [0], $ignoreStdErr = false)
8080 *
8181 * @param string[] $command The command that will be executed. This method will compose the command
8282 * executed by exec with proper escaping.
83- * @param null| string $stdout The filename to redirect the standard output. If null the standard output is
83+ * @param string|null $stdout The filename to redirect the standard output. If null the standard output is
8484 * redirected to /dev/null.
85- * @param null| string $stderr The filename to redirect the standard error. If null the standard error is
85+ * @param string|null $stderr The filename to redirect the standard error. If null the standard error is
8686 * redirected to the standard output. Use '/dev/null' to ignore standard error.
87- * @param null| int[] $returnVars The allowed return statuses. If the return status of the command is not in
87+ * @param int[]|null $statuses The allowed return statuses. If the return status of the command is not in
8888 * this array an exception will be thrown. Null will allow all return statuses and an
8989 * empty array will throw an exception always.
9090 *
@@ -93,7 +93,10 @@ public static function exec1($command, $returnVars = [0], $ignoreStdErr = false)
9393 * @since 1.0.0
9494 * @api
9595 */
96- public static function exec2 ($ command , $ stdout = null , $ stderr = null , $ returnVars = [0 ])
96+ public static function exec2 (array $ command ,
97+ ?string $ stdout = null ,
98+ ?string $ stderr = null ,
99+ ?array $ statuses = [0 ]): int
97100 {
98101 $ command = self ::escape ($ command );
99102
@@ -117,14 +120,14 @@ public static function exec2($command, $stdout = null, $stderr = null, $returnVa
117120 $ command .= escapeshellarg ($ stderr );
118121 }
119122
120- exec ($ command , $ output , $ return_var );
123+ exec ($ command , $ output , $ status );
121124
122- if (is_array ($ returnVars ) && !in_array ($ return_var , $ returnVars ))
125+ if (is_array ($ statuses ) && !in_array ($ status , $ statuses ))
123126 {
124- throw new ProgramExecutionException ($ command , $ return_var , $ output );
127+ throw new ProgramExecutionException ($ command , $ status , $ output );
125128 }
126129
127- return ( int ) $ return_var ;
130+ return $ status ;
128131 }
129132
130133 //--------------------------------------------------------------------------------------------------------------------
0 commit comments