Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code quality #16

Merged
merged 2 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "9.*"
"phpunit/phpunit": "9.*",
"phpstan/phpstan": ">=0.12.42"
},

"autoload": {
Expand Down
80 changes: 68 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

class Pool {
/** @var Process[] Associative array of name=>Process */
protected $processList;
protected array $processList;

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

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

Expand Down
29 changes: 17 additions & 12 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ class Process {
const PIPE_OUT = 1;
const PIPE_ERROR = 2;

/** @var string[] */
protected $command;
/** @var string */
protected $cwd;
/** @var array Indexed array of streams: 0=>input, 1=>output, 2=>error. */
protected $pipes;
/** @var array<int, string> */
protected array $command;
protected string $cwd;
/** @var array<int, resource> Indexed array of streams: 0=>input, 1=>output, 2=>error. */
protected array $pipes;
/** @var resource The process as returned from proc_open. */
protected $process = null;
protected $status;
/** @var bool */
protected $isBlocking = false;
/** @var array<string, mixed> */
protected array $status;
protected bool $isBlocking = false;

public function __construct(string...$command) {
$this->command = $command;
$this->cwd = getcwd();
$this->pipes = [];
}

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

// Parameter #1 of proc_open is an array
// @see https://www.php.net/manual/en/function.proc-open.php

$this->process = proc_open(
/** @phpstan-param string[] */
$this->command,
$descriptor,
$this->pipes
Expand Down Expand Up @@ -89,6 +93,7 @@ public function hasNotEnded():bool {
return $this->status["exitcode"] === 0;
}

/** @return array<int, string> */
public function getCommand():array {
return $this->command;
}
Expand Down Expand Up @@ -153,8 +158,8 @@ public function setBlocking(bool $blocking = true):void {
* @see https://php.net/manual/function.proc-get-status.php
**/
protected function refreshStatus():void {
if($this->status["running"] ?? null
|| empty($this->status)) {
$running = $this->status["running"] ?? null;
if($running || empty($this->status)) {
if(is_resource($this->process)) {
$this->status = proc_get_status($this->process);
}
Expand Down