Skip to content

Commit cb85f66

Browse files
committed
Use type hinting and strict types.
1 parent fb831ff commit cb85f66

File tree

6 files changed

+65
-61
lines changed

6 files changed

+65
-61
lines changed

build.xml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project name="Phing Extensions" default="build" basedir=".">
3-
<taskdef name="ReadSemanticVersion" classname="vendor.setbased.phing-extensions.src.Task.ReadSemanticVersionTask"/>
4-
<property name="VERSION" value="0.0.0"/> <!-- Keep our IDE happy -->
5-
63
<target name="build"/>
74

8-
<target name="version">
9-
<ReadSemanticVersion file=".version" versionProperty="VERSION"/>
10-
<echo message="${VERSION}"/>
11-
<gitcommit repository="." message="Release: ${VERSION}" allFiles="true"/>
12-
<gitpush repository="."/>
13-
<gittag repository="." name="${VERSION}"/>
14-
<gitpush repository="." refspec="${VERSION}" quiet="false"/>
5+
<!-- Runs composer update and executes various other updates -->
6+
<target name="update">
7+
<exec command="composer update" checkreturn="true" passthru="true"/>
158
</target>
169

1710
<!-- Runs all unit tests -->
1811
<target name="unit">
1912
<exec command="bin/phpunit" passthru="true" checkreturn="true"/>
2013
</target>
21-
</project>
14+
</project>

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
{
22
"name": "setbased/helper-program-execution",
33
"description": "Program Execution Helper",
4-
"keyword": [
4+
"keywords": [
55
"Program",
66
"Execution"
77
],
88
"license": "MIT",
99
"require": {
10-
"php": ">=5.4"
10+
"php": ">=7.1"
1111
},
1212
"require-dev": {
13-
"phing/phing": "2.*",
14-
"phpunit/phpunit": "4.* || 5.*",
15-
"setbased/phing-extensions": "2.*"
13+
"phing/phing": "^2.0.0",
14+
"phpunit/phpunit": "^6.0.0",
15+
"setbased/phing-extensions": "^2.0.0"
1616
},
1717
"autoload": {
1818
"psr-4": {
19-
"SetBased\\": "src"
19+
"SetBased\\": "src/"
2020
}
2121
},
2222
"autoload-dev": {
2323
"psr-4": {
24-
"SetBased\\Test\\": "test"
24+
"SetBased\\Test\\": "test/"
2525
}
2626
},
2727
"config": {
28-
"bin-dir": "bin"
28+
"bin-dir": "bin/"
2929
}
3030
}

src/Exception/ProgramExecutionException.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
2-
//----------------------------------------------------------------------------------------------------------------------
2+
declare(strict_types=1);
3+
34
namespace SetBased\Exception;
45

5-
//----------------------------------------------------------------------------------------------------------------------
66
/**
77
* Exceptions for failed program executions.
88
*/
@@ -41,7 +41,7 @@ class ProgramExecutionException extends \RuntimeException
4141
* @since 1.0.0
4242
* @api
4343
*/
44-
public function __construct($command, $status, $output)
44+
public function __construct(string $command, int $status, ?array $output)
4545
{
4646
parent::__construct(self::message($command, $status, $output));
4747

@@ -59,7 +59,7 @@ public function __construct($command, $status, $output)
5959
* @since 1.0.0
6060
* @api
6161
*/
62-
public function getCommand()
62+
public function getCommand(): string
6363
{
6464
return $this->command;
6565
}
@@ -73,7 +73,7 @@ public function getCommand()
7373
* @since 1.0.0
7474
* @api
7575
*/
76-
public function getOutput()
76+
public function getOutput(): array
7777
{
7878
return $this->output;
7979
}
@@ -87,7 +87,7 @@ public function getOutput()
8787
* @since 1.0.0
8888
* @api
8989
*/
90-
public function getStatus()
90+
public function getStatus(): int
9191
{
9292
return $this->status;
9393
}
@@ -102,7 +102,7 @@ public function getStatus()
102102
*
103103
* @return string
104104
*/
105-
private function message($command, $status, $output)
105+
private function message(string $command, int $status, ?array $output): string
106106
{
107107
$message = sprintf('Command below exited with status %s:', $status);
108108
$message .= PHP_EOL;

src/Helper/ProgramExecution.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
2-
//----------------------------------------------------------------------------------------------------------------------
2+
declare(strict_types=1);
3+
34
namespace SetBased\Helper;
45

56
use 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
//--------------------------------------------------------------------------------------------------------------------

test/Exception/ProgramExecutionExceptionTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<?php
2-
//----------------------------------------------------------------------------------------------------------------------
2+
declare(strict_types=1);
3+
34
namespace SetBased\Test\Exception;
45

6+
use PHPUnit\Framework\TestCase;
57
use SetBased\Exception\ProgramExecutionException;
68

7-
//----------------------------------------------------------------------------------------------------------------------
8-
class ProgramExecutionExceptionTest extends \PHPUnit_Framework_TestCase
9+
/**
10+
* Test cases for ProgramExecutionException.
11+
*/
12+
class ProgramExecutionExceptionTest extends TestCase
913
{
1014
//--------------------------------------------------------------------------------------------------------------------
1115
/**
1216
* Test getters.
1317
*/
14-
public function test1()
18+
public function test1(): void
1519
{
1620
$e = new ProgramExecutionException('command', 123, ['hello', 'world']);
1721

test/Helper/ProgramExecutionTest.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<?php
2-
//----------------------------------------------------------------------------------------------------------------------
2+
declare(strict_types=1);
3+
34
namespace SetBased\Helper\Test;
45

6+
use PHPUnit\Framework\TestCase;
57
use SetBased\Helper\ProgramExecution;
68

7-
//----------------------------------------------------------------------------------------------------------------------
8-
class ProgramExecutionTest extends \PHPUnit_Framework_TestCase
9+
/**
10+
* Test cases for ProgramExecution.
11+
*/
12+
class ProgramExecutionTest extends TestCase
913
{
1014
//--------------------------------------------------------------------------------------------------------------------
1115
/**
1216
* Test with failed command and any allowed exit statuses.
1317
*/
14-
public function testExec1AnyExitStatus1()
18+
public function testExec1AnyExitStatus1(): void
1519
{
1620
$ret = ProgramExecution::exec1(['false'], null);
1721

@@ -24,7 +28,7 @@ public function testExec1AnyExitStatus1()
2428
*
2529
* @expectedException \SetBased\Exception\ProgramExecutionException
2630
*/
27-
public function testExec1AnyExitStatus2()
31+
public function testExec1AnyExitStatus2(): void
2832
{
2933
$ret = ProgramExecution::exec1(['false'], []);
3034

@@ -35,7 +39,7 @@ public function testExec1AnyExitStatus2()
3539
/**
3640
* Test with array as command.
3741
*/
38-
public function testExec1BasicUsage()
42+
public function testExec1BasicUsage(): void
3943
{
4044
list($output, $status) = ProgramExecution::exec1(['echo', 'hello, world!']);
4145

@@ -49,7 +53,7 @@ public function testExec1BasicUsage()
4953
*
5054
* @expectedException \SetBased\Exception\ProgramExecutionException
5155
*/
52-
public function testExec1ExitStatusFail()
56+
public function testExec1ExitStatusFail(): void
5357
{
5458
ProgramExecution::exec1(['cmp', __FILE__, 'foogazy'], [0, 1]);
5559
}
@@ -58,7 +62,7 @@ public function testExec1ExitStatusFail()
5862
/**
5963
* Test with ignore STDERR.
6064
*/
61-
public function testExec1IgnoreStdErr()
65+
public function testExec1IgnoreStdErr(): void
6266
{
6367
$tmp = ProgramExecution::exec1(['ls', 'foogazy'], null, false);
6468
$this->assertNotEmpty($tmp[0]);
@@ -71,7 +75,7 @@ public function testExec1IgnoreStdErr()
7175
/**
7276
* Test with multiple allowed exit statuses.
7377
*/
74-
public function testExec1MultipleExitStatuses()
78+
public function testExec1MultipleExitStatuses(): void
7579
{
7680
$ret = ProgramExecution::exec1(['cmp', __FILE__, __DIR__.'/../bootstrap.php'], [0, 1]);
7781

@@ -82,7 +86,7 @@ public function testExec1MultipleExitStatuses()
8286
/**
8387
* Test with tricky command.
8488
*/
85-
public function testExec1TrickyCommand()
89+
public function testExec1TrickyCommand(): void
8690
{
8791
$strings = ['; rm -rf .. &', '"', "'"];
8892

@@ -99,7 +103,7 @@ public function testExec1TrickyCommand()
99103
/**
100104
* Test with failed command and any exit status
101105
*/
102-
public function testExec2AnyExitStatusFail()
106+
public function testExec2AnyExitStatusFail(): void
103107
{
104108
$ret = ProgramExecution::exec2(['cmp', __FILE__, 'foogazy'], null, 'error.txt', null);
105109

@@ -113,7 +117,7 @@ public function testExec2AnyExitStatusFail()
113117
/**
114118
* Test with array as command.
115119
*/
116-
public function testExec2BasicUsage()
120+
public function testExec2BasicUsage(): void
117121
{
118122
$ret = ProgramExecution::exec2(['echo', 'hello, world!'], 'hello.txt');
119123

@@ -129,7 +133,7 @@ public function testExec2BasicUsage()
129133
*
130134
* @expectedException \SetBased\Exception\ProgramExecutionException
131135
*/
132-
public function testExec2ExitStatusFail()
136+
public function testExec2ExitStatusFail(): void
133137
{
134138
ProgramExecution::exec2(['cmp', __FILE__, 'foogazy'], null, '/dev/null');
135139
}
@@ -138,7 +142,7 @@ public function testExec2ExitStatusFail()
138142
/**
139143
* Test with tricky command.
140144
*/
141-
public function testExec2TrickyCommand()
145+
public function testExec2TrickyCommand(): void
142146
{
143147
$strings = ['; rm -rf .. &', '"', "'"];
144148

0 commit comments

Comments
 (0)