Fixed psalm errors. v0.0.2
authorOe.Salim Duran <salim.duran@graph-it.com>
Wed, 11 May 2022 09:30:29 +0000 (11:30 +0200)
committerOe.Salim Duran <salim.duran@graph-it.com>
Wed, 11 May 2022 09:30:29 +0000 (11:30 +0200)
composer.lock [new file with mode: 0644]
psalm.xml [new file with mode: 0644]
src/Daemon.php
src/MainThread.php
src/Service.php
src/Thread.php

diff --git a/composer.lock b/composer.lock
new file mode 100644 (file)
index 0000000..650cd75
--- /dev/null
@@ -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 (file)
index 0000000..4e9226b
--- /dev/null
+++ b/psalm.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<psalm
+    errorLevel="2"
+    resolveFromConfigFile="true"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns="https://getpsalm.org/schema/config"
+    xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
+>
+    <projectFiles>
+        <directory name="src" />
+        <ignoreFiles>
+            <directory name="vendor" />
+        </ignoreFiles>
+    </projectFiles>
+</psalm>
index 3bfd7a98b240cc63dfb2199e6b9fc1467987a01e..420ac13f597ed11d5f5b2790e394189ccc02f944 100644 (file)
@@ -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
+}
index e60e80300336b9ae252d2b54b94e4e47d18566ed..1338a4b051adaaffd54c440108af8824425a6aa0 100644 (file)
@@ -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 { }
index 5f849cdc8ad4145bff1073973f776e4b680dff7e..ba24f10171af995e961aee811488081b51515f3c 100644 (file)
@@ -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()) {
index eed9e4f0b7e54a5ebae5d45eda7baf111d8d750d..4bc505b435e54c88ff7f5dac3e3a6359aa0b2f5c 100644 (file)
@@ -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() { }
 }