From 6490df99a22818149f30e3af408002ea7f73e035 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 2 Feb 2021 17:33:16 +0000 Subject: [PATCH] Improve code quality (#16) * Improve code quality * Initialise array --- composer.json | 3 +- composer.lock | 80 +++++++++++++++++++++++++++++++++++++++++-------- src/Pool.php | 4 +-- src/Process.php | 29 ++++++++++-------- 4 files changed, 89 insertions(+), 27 deletions(-) diff --git a/composer.json b/composer.json index 663bbb2..8fe68fb 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "php": ">=7.4" }, "require-dev": { - "phpunit/phpunit": "9.*" + "phpunit/phpunit": "9.*", + "phpstan/phpstan": ">=0.12.42" }, "autoload": { diff --git a/composer.lock b/composer.lock index 4afeffb..0d5a2ca 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c738cc31376e09de8edb72c7039476dc", + "content-hash": "d8a937720c006b21dc286e5111bfcf13", "packages": [], "packages-dev": [ { @@ -490,6 +490,62 @@ ], "time": "2020-12-19T10:15:11+00:00" }, + { + "name": "phpstan/phpstan", + "version": "0.12.71", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "d508fa3b0ecc5fc91ac70c6c7ac2862f968ba2b5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/d508fa3b0ecc5fc91ac70c6c7ac2862f968ba2b5", + "reference": "d508fa3b0ecc5fc91ac70c6c7ac2862f968ba2b5", + "shasum": "" + }, + "require": { + "php": "^7.1|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.12-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://www.patreon.com/phpstan", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2021-02-01T18:24:00+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "9.2.5", @@ -790,16 +846,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.0", + "version": "9.5.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "8e16c225d57c3d6808014df6b1dd7598d0a5bbbe" + "reference": "f661659747f2f87f9e72095bb207bceb0f151cb4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/8e16c225d57c3d6808014df6b1dd7598d0a5bbbe", - "reference": "8e16c225d57c3d6808014df6b1dd7598d0a5bbbe", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f661659747f2f87f9e72095bb207bceb0f151cb4", + "reference": "f661659747f2f87f9e72095bb207bceb0f151cb4", "shasum": "" }, "require": { @@ -885,7 +941,7 @@ "type": "github" } ], - "time": "2020-12-04T05:05:53+00:00" + "time": "2021-02-02T14:45:58+00:00" }, { "name": "sebastian/cli-parser", @@ -1789,16 +1845,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.20.0", + "version": "v1.22.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "f4ba089a5b6366e453971d3aad5fe8e897b37f41" + "reference": "c6c942b1ac76c82448322025e084cadc56048b4e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/f4ba089a5b6366e453971d3aad5fe8e897b37f41", - "reference": "f4ba089a5b6366e453971d3aad5fe8e897b37f41", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/c6c942b1ac76c82448322025e084cadc56048b4e", + "reference": "c6c942b1ac76c82448322025e084cadc56048b4e", "shasum": "" }, "require": { @@ -1810,7 +1866,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.20-dev" + "dev-main": "1.22-dev" }, "thanks": { "name": "symfony/polyfill", @@ -1861,7 +1917,7 @@ "type": "tidelift" } ], - "time": "2020-10-23T14:02:19+00:00" + "time": "2021-01-07T16:49:33+00:00" }, { "name": "theseer/tokenizer", diff --git a/src/Pool.php b/src/Pool.php index db094e6..7ed9249 100644 --- a/src/Pool.php +++ b/src/Pool.php @@ -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; } diff --git a/src/Process.php b/src/Process.php index 3bcd510..8871195 100644 --- a/src/Process.php +++ b/src/Process.php @@ -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 */ + protected array $command; + protected string $cwd; + /** @var array 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 */ + protected array $status; + protected bool $isBlocking = false; public function __construct(string...$command) { $this->command = $command; $this->cwd = getcwd(); + $this->pipes = []; } public function __destruct() { @@ -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"], @@ -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 @@ -89,6 +93,7 @@ public function hasNotEnded():bool { return $this->status["exitcode"] === 0; } + /** @return array */ public function getCommand():array { return $this->command; } @@ -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); }