Skip to content

Commit 6490df9

Browse files
author
Greg Bowler
authored
Improve code quality (#16)
* Improve code quality * Initialise array
1 parent e243c16 commit 6490df9

4 files changed

Lines changed: 89 additions & 27 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"php": ">=7.4"
77
},
88
"require-dev": {
9-
"phpunit/phpunit": "9.*"
9+
"phpunit/phpunit": "9.*",
10+
"phpstan/phpstan": ">=0.12.42"
1011
},
1112

1213
"autoload": {

composer.lock

Lines changed: 68 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Pool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
class Pool {
55
/** @var Process[] Associative array of name=>Process */
6-
protected $processList;
6+
protected array $processList;
77

88
public function __construct() {
99
$this->processList = [];
1010
}
1111

12-
public function add(string $name, Process $process) {
12+
public function add(string $name, Process $process):void {
1313
$this->processList[$name] = $process;
1414
}
1515

src/Process.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ class Process {
66
const PIPE_OUT = 1;
77
const PIPE_ERROR = 2;
88

9-
/** @var string[] */
10-
protected $command;
11-
/** @var string */
12-
protected $cwd;
13-
/** @var array Indexed array of streams: 0=>input, 1=>output, 2=>error. */
14-
protected $pipes;
9+
/** @var array<int, string> */
10+
protected array $command;
11+
protected string $cwd;
12+
/** @var array<int, resource> Indexed array of streams: 0=>input, 1=>output, 2=>error. */
13+
protected array $pipes;
1514
/** @var resource The process as returned from proc_open. */
1615
protected $process = null;
17-
protected $status;
18-
/** @var bool */
19-
protected $isBlocking = false;
16+
/** @var array<string, mixed> */
17+
protected array $status;
18+
protected bool $isBlocking = false;
2019

2120
public function __construct(string...$command) {
2221
$this->command = $command;
2322
$this->cwd = getcwd();
23+
$this->pipes = [];
2424
}
2525

2626
public function __destruct() {
@@ -35,7 +35,7 @@ public function setExecCwd(string $cwd):void {
3535
* Runs the command in a concurrent thread.
3636
* Sets the input, output and errors streams.
3737
*/
38-
public function exec() {
38+
public function exec():void {
3939
$descriptor = [
4040
self::PIPE_IN => ["pipe", "r"],
4141
self::PIPE_OUT => ["pipe", "w"],
@@ -45,7 +45,11 @@ public function exec() {
4545
$oldCwd = getcwd();
4646
chdir($this->cwd);
4747

48+
// Parameter #1 of proc_open is an array
49+
// @see https://www.php.net/manual/en/function.proc-open.php
50+
4851
$this->process = proc_open(
52+
/** @phpstan-param string[] */
4953
$this->command,
5054
$descriptor,
5155
$this->pipes
@@ -89,6 +93,7 @@ public function hasNotEnded():bool {
8993
return $this->status["exitcode"] === 0;
9094
}
9195

96+
/** @return array<int, string> */
9297
public function getCommand():array {
9398
return $this->command;
9499
}
@@ -153,8 +158,8 @@ public function setBlocking(bool $blocking = true):void {
153158
* @see https://php.net/manual/function.proc-get-status.php
154159
**/
155160
protected function refreshStatus():void {
156-
if($this->status["running"] ?? null
157-
|| empty($this->status)) {
161+
$running = $this->status["running"] ?? null;
162+
if($running || empty($this->status)) {
158163
if(is_resource($this->process)) {
159164
$this->status = proc_get_status($this->process);
160165
}

0 commit comments

Comments
 (0)