From 8e9a1e28803d3dcf1009edfc2dbd12f245c044b8 Mon Sep 17 00:00:00 2001 From: "Oe.Salim Duran" Date: Wed, 11 May 2022 11:30:29 +0200 Subject: [PATCH] Fixed psalm errors. --- composer.lock | 20 ++++++++++++++++++++ psalm.xml | 15 +++++++++++++++ src/Daemon.php | 5 +++-- src/MainThread.php | 10 +--------- src/Service.php | 13 ++++++++++--- src/Thread.php | 45 +++++++++++++++++++++++++++++++++++++-------- 6 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 composer.lock create mode 100644 psalm.xml diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..650cd75 --- /dev/null +++ b/composer.lock @@ -0,0 +1,20 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "371512141531f38a907ed1a5fbe3055b", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.6.0" + }, + "platform-dev": [], + "plugin-api-version": "2.2.0" +} diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..4e9226b --- /dev/null +++ b/psalm.xml @@ -0,0 +1,15 @@ + + + + + + + + + diff --git a/src/Daemon.php b/src/Daemon.php index 3bfd7a9..420ac13 100644 --- a/src/Daemon.php +++ b/src/Daemon.php @@ -4,6 +4,7 @@ namespace Graphit\Concurrent; abstract class Daemon extends Thread { + /** */ public function start() { if ($this->isRunning()) { @@ -37,7 +38,7 @@ abstract class Daemon extends Thread $childpid = 256*256-1; } if ($childpid != 0) { // Push Child-PID from Starter-Thread - $data = chr(floor($childpid / 256)).chr($childpid % 256); + $data = chr((int)(floor($childpid / 256))).chr($childpid % 256); $fifo = fopen($fifo_file, 'w'); fwrite($fifo, $data); @@ -56,4 +57,4 @@ abstract class Daemon extends Thread exit(); } } -} \ No newline at end of file +} diff --git a/src/MainThread.php b/src/MainThread.php index e60e803..1338a4b 100644 --- a/src/MainThread.php +++ b/src/MainThread.php @@ -2,12 +2,4 @@ namespace Graphit\Concurrent; -class MainThread extends Thread -{ - public function getChildren() - { - return parent::getChildren(); - } - - abstract function run(); -} \ No newline at end of file +class MainThread extends Thread { } diff --git a/src/Service.php b/src/Service.php index 5f849cd..ba24f10 100644 --- a/src/Service.php +++ b/src/Service.php @@ -7,19 +7,23 @@ abstract class Service extends Daemon /** @var string */ protected $pidfile; + /** + * @param string $pidfile + */ public function __construct($pidfile) { $this->pidfile = $pidfile; if (file_exists($this->pidfile)) { - $this->pid = trim(file_get_contents($this->pidfile)); + $this->pid = (int)(trim(file_get_contents($this->pidfile))); } } + /** */ public function isRunning() { if ( !$this->pid){ if (file_exists($this->pidfile)) { - $this->pid = trim(file_get_contents($this->pidfile)); + $this->pid = (int)(trim(file_get_contents($this->pidfile))); } } $running = parent::isRunning(); @@ -31,6 +35,7 @@ abstract class Service extends Daemon return $running; } + /** */ public function stop() { while ($this->isRunning()) { @@ -39,15 +44,17 @@ abstract class Service extends Daemon } } + /** */ public function setup() { parent::setup(); if ($this->isRunning()) { - @file_put_contents($this->pidfile, $this->pid); + @file_put_contents($this->pidfile, (string)$this->pid); } } + /** */ public function teardown() { if ($this->isRunning()) { diff --git a/src/Thread.php b/src/Thread.php index eed9e4f..4bc505b 100644 --- a/src/Thread.php +++ b/src/Thread.php @@ -4,16 +4,19 @@ namespace Graphit\Concurrent; abstract class Thread { - /** @var integer */ + /** @var int|null */ protected $pid; /** @var boolean */ protected $stopped = false; + /** @var bool */ + private $daemon; + /** @var \Graphit\Concurrent\Thread[] */ protected $children = array(); - /** @var \Graphit\Concurrent\Thread */ + /** @var Thread */ protected static $current; /** @@ -23,7 +26,7 @@ abstract class Thread */ public static function getCurrent() { - if ( !isset(Thread::$current)) { + if (!Thread::$current instanceof Thread) { Thread::makeCurrent(new MainThread); } if (Thread::$current->pid !== posix_getpid()) { @@ -32,6 +35,9 @@ abstract class Thread return Thread::$current; } + /** + * @return void + */ protected static function makeCurrent(Thread $current) { Thread::$current = $current; @@ -41,11 +47,17 @@ abstract class Thread pcntl_signal(SIGCHLD, array(Thread::$current, 'chldHandler')); } + /** + * @return void + */ protected function termHandler() { $this->stopped = true; } + /** + * @return void + */ protected function chldHandler() { while (($childpid = pcntl_waitpid(-1, $status, WNOHANG)) > 0) { @@ -62,7 +74,7 @@ abstract class Thread * Gibt die Prozess-Id des Threads zurück. Gibt nur den richtigen Wert zurück, * solange $this->isRunning() === true. * - * @return integer + * @return int|null */ public function getPid() { @@ -88,14 +100,22 @@ abstract class Thread return $this->children; } + /** + * @return bool + */ public function isDaemon() { - return $this->_daemon; + return $this->daemon; } + /** + * @param bool $daemon + * + * @return void + */ public function setDaemon($daemon) { - $this->_daemon = true == $daemon; + $this->daemon = !!$daemon; } /** @@ -149,7 +169,7 @@ abstract class Thread /** * Started den Thread * - * @return boolean + * @return bool|null */ public function start() { @@ -178,10 +198,19 @@ abstract class Thread } } + /** + * @return void + */ public function setup() { } - abstract public function run(); + /** + * @return void + */ + public function run() { } + /** + * @return void + */ public function teardown() { } } -- 2.34.1