diff --git a/composer.json b/composer.json
index 1b53508..5af5f45 100644
--- a/composer.json
+++ b/composer.json
@@ -1,20 +1,16 @@
 {
-    "name": "hyperthese/php-serial",
+    "name": "zzepish/php-serial",
     "description": "Multi-platform serial port access convenience class",
-    "license": "GNU GPLv2",
+    "license": "GPL-3.0-only",
     "authors": [
         {
             "name": "Rémy Sanchez",
             "email": "remy.sanchez@hyperthese.net"
         }
     ],
-    "minimum-stability": "stable",
     "autoload": {
-        "classmap": ["src/"]
-    },
-    "extra": {
-        "branch-alias": {
-            "dev-develop": "1.0.x-dev"
+        "psr-4": {
+            "PhpSerial\\": "src/"
         }
     }
 }
diff --git a/examples/VS421CPNTA.php b/examples/VS421CPNTA.php
deleted file mode 100644
index 58d8d6b..0000000
--- a/examples/VS421CPNTA.php
+++ /dev/null
@@ -1,102 +0,0 @@
-<?php
-
-/*
-
-This example illustrates some basic communication with a device via an HTML form.  In this example,
-the device being communicated with is a StarTech.com video switcher, model VS421CPNTA.
-
-http://www.startech.com/item/VS421CPNTA-4-Port-Component-Video-Switch-with-RS232.aspx
-
-This device uses RS232 commands to switch between 1 of 4 inputs by sending an 'I' followed by a
-single digit 1-4, and will echo back this same string in response.
-
-Port settings are 9600-8-N-1, set specifically in the code below.  Failure to specifically set these
-may result in incorrect defaults being used.
-
-A timer is used in the function that reads the serial input, .5 seconds is the allotted time for the
-unit to echo back a response, other devices may need more time for their replies depending on port
-speed and the amount of data being sent back, this is just an example and is sufficient for this
-device.  No end of line character is sent, but we could instead have used a loop to read until a
-number was received, again, this code is just an example.
-
-We then echo back the result received, and present the form again for additional input changes.
-
-*/
-
-function microtime_float()
-{
-    list($usec, $sec) = explode(" ", microtime());
-
-    return ((float) $usec + (float) $sec);
-}
-
-$the_input  = $_POST['the_input'];
-
-if ($the_input == '') {
-    echo "<div id='newrequestbox'>";
-    echo "<form id='FormName' name='FormName' action='example_VS421CPNTA.php' method='post'>
-            <table width=500>
-                <tr>
-                    <td>Switch to input? :</td>
-                    <td><input type=text name=the_input  maxlength=30 size=30></td>
-                    <td><input type=submit value='Switch'></td>
-                </tr>
-            </table>
-        </form>";
-    echo "</div>";
-} else {
-
-include 'PhpSerial.php';
-
-// Let's start the class
-$serial = new PhpSerial;
-
-// First we must specify the device. This works on both linux and windows (if
-// your linux serial device is /dev/ttyS0 for COM1, etc)
-// $serial->deviceSet("COM1");
-$serial->deviceSet("/dev/cu.usbserial-FTDY7ID6");
-
-// We can change the baud rate, parity, length, stop bits, flow control
-$serial->confBaudRate(2400);
-$serial->confParity("none");
-$serial->confCharacterLength(8);
-$serial->confStopBits(1);
-$serial->confFlowControl("none");
-
-// Then we need to open it
-$serial->deviceOpen();
-
-// To write into
-$serial->sendMessage("I".$the_input);
-
-// Or to read from
-$read = '';
-$theResult = '';
-$start = microtime_float();
-
-while ( ($read == '') && (microtime_float() <= $start + 0.5) ) {
-    $read = $serial->readPort();
-    if ($read != '') {
-        $theResult .= $read;
-        $read = '';
-    }
-}
-
-// If you want to change the configuration, the device must be closed
-$serial->deviceClose();
-
-// etc...
-
-echo "Read data: ".$theResult."<br>";
-
-echo "<form id='FormName' name='FormName' action='example_VS421CPNTA.php' method='post'>
-        <table width=500>
-
-            <tr>
-                <td>Switch to input? :</td>
-                <td><input type=text name=the_input  maxlength=30 size=30></td>
-                <td><input type=submit value='Switch'></td>
-            </tr>
-        </table>
-    </form>";
-}
diff --git a/examples/dummy.php b/examples/dummy.php
index 486031f..ef1e696 100644
--- a/examples/dummy.php
+++ b/examples/dummy.php
@@ -1,19 +1,24 @@
 <?php
-include 'PhpSerial.php';
+require_once __DIR__ . '/../vendor/autoload.php';
+
+use \PhpSerial\PhpSerial;
+use \PhpSerial\Interfaces\BaudInterface;
+use \PhpSerial\Interfaces\ParityInterface;
+use \PhpSerial\Interfaces\FlowControlInterface;
 
 // Let's start the class
-$serial = new PhpSerial;
+$serial = new PhpSerial();
 
 // First we must specify the device. This works on both linux and windows (if
 // your linux serial device is /dev/ttyS0 for COM1, etc)
 $serial->deviceSet("COM1");
 
 // We can change the baud rate, parity, length, stop bits, flow control
-$serial->confBaudRate(2400);
-$serial->confParity("none");
+$serial->confBaudRate(BaudInterface::RATE_2400);
+$serial->confParity(ParityInterface::NONE);
 $serial->confCharacterLength(8);
 $serial->confStopBits(1);
-$serial->confFlowControl("none");
+$serial->confFlowControl(FlowControlInterface::NONE);
 
 // Then we need to open it
 $serial->deviceOpen();
@@ -28,7 +33,7 @@
 $serial->deviceClose();
 
 // We can change the baud rate
-$serial->confBaudRate(2400);
+$serial->confBaudRate(BaudInterface::RATE_2400);
 
 // etc...
 //
diff --git a/examples/sms.php b/examples/sms.php
index a877de6..925e331 100644
--- a/examples/sms.php
+++ b/examples/sms.php
@@ -1,8 +1,11 @@
 <?php
-include 'PhpSerial.php';
+require_once __DIR__ . '/../vendor/autoload.php';
+
+use \PhpSerial\PhpSerial;
+use \PhpSerial\Interfaces\BaudInterface;
 
 // Let's start the class
-$serial = new PhpSerial;
+$serial = new PhpSerial();
 
 // First we must specify the device. This works on both linux and windows (if
 // your linux serial device is /dev/ttyS0 for COM1, etc)
@@ -15,10 +18,10 @@
 $serial->deviceOpen('w+');
 
 // We may need to return if nothing happens for 10 seconds
-stream_set_timeout($serial->_dHandle, 10);
+stream_set_timeout($serial->dHandle, 10);
 
 // We can change the baud rate
-$serial->confBaudRate(9600);
+$serial->confBaudRate(BaudInterface::RATE_9600);
 
 // SMS inbox query - mode command and list command
 $serial->sendMessage("AT",1);
diff --git a/src/Interfaces/BaudInterface.php b/src/Interfaces/BaudInterface.php
new file mode 100644
index 0000000..c7e8b1e
--- /dev/null
+++ b/src/Interfaces/BaudInterface.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace PhpSerial\Interfaces;
+
+interface BaudInterface
+{
+    public const RATE_110    = 110;
+    public const RATE_150    = 150;
+    public const RATE_300    = 300;
+    public const RATE_600    = 600;
+    public const RATE_1200   = 1200;
+    public const RATE_2400   = 2400;
+    public const RATE_4800   = 4800;
+    public const RATE_9600   = 9600;
+    public const RATE_19200  = 19200;
+    public const RATE_38400  = 38400;
+    public const RATE_57600  = 57600;
+    public const RATE_115200 = 115200;
+}
diff --git a/src/Interfaces/FlowControlInterface.php b/src/Interfaces/FlowControlInterface.php
new file mode 100644
index 0000000..f5c5f32
--- /dev/null
+++ b/src/Interfaces/FlowControlInterface.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace PhpSerial\Interfaces;
+
+class FlowControlInterface
+{
+    public const NONE     = 'none';
+    public const XON_XOFF = 'xon/xoff';
+    public const RST_CTS  = 'rts/cts';
+}
diff --git a/src/Interfaces/ParityInterface.php b/src/Interfaces/ParityInterface.php
new file mode 100644
index 0000000..996f6c6
--- /dev/null
+++ b/src/Interfaces/ParityInterface.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace PhpSerial\Interfaces;
+
+interface ParityInterface
+{
+    public const ODD  = 'odd';
+    public const EVEN = 'even';
+    public const NONE = 'none';
+}
diff --git a/src/PhpSerial.php b/src/PhpSerial.php
index 1d253d5..5d67aad 100644
--- a/src/PhpSerial.php
+++ b/src/PhpSerial.php
@@ -1,7 +1,13 @@
 <?php
-define ("SERIAL_DEVICE_NOTSET", 0);
-define ("SERIAL_DEVICE_SET", 1);
-define ("SERIAL_DEVICE_OPENED", 2);
+
+namespace PhpSerial;
+
+use PhpSerial\Interfaces\BaudInterface;
+use PhpSerial\Interfaces\ParityInterface;
+
+define("SERIAL_DEVICE_NOTSET", 0);
+define("SERIAL_DEVICE_SET", 1);
+define("SERIAL_DEVICE_OPENED", 2);
 
 /**
  * Serial port control class
@@ -18,37 +24,27 @@
  */
 class PhpSerial
 {
-    public $_device = null;
-    public $_winDevice = null;
-    public $_dHandle = null;
-    public $_dState = SERIAL_DEVICE_NOTSET;
-    public $_buffer = "";
-    public $_os = "";
+    public ?string $device    = null;
+    public ?string $winDevice = null;
+    public         $dHandle   = null;
+    public int     $dState    = SERIAL_DEVICE_NOTSET;
+    public string  $buffer    = '';
+    public string  $os        = '';
 
     /**
      * This var says if buffer should be flushed by sendMessage (true) or
      * manually (false)
-     *
-     * @var bool
      */
-    public $autoFlush = true;
+    public bool $autoFlush = true;
 
-    /**
-     * Constructor. Perform some checks about the OS and setserial
-     *
-     * @return PhpSerial
-     */
-    public function PhpSerial()
+    public function __construct()
     {
         setlocale(LC_ALL, "en_US");
-
         $sysName = php_uname();
-
         if (substr($sysName, 0, 5) === "Linux") {
-            $this->_os = "linux";
-
-            if ($this->_exec("stty") === 0) {
-                register_shutdown_function(array($this, "deviceClose"));
+            $this->os = "linux";
+            if ($this->_exec("stty --version") === 0) {
+                register_shutdown_function([$this, "deviceClose"]);
             } else {
                 trigger_error(
                     "No stty availible, unable to run.",
@@ -56,14 +52,16 @@ public function PhpSerial()
                 );
             }
         } elseif (substr($sysName, 0, 6) === "Darwin") {
-            $this->_os = "osx";
-            register_shutdown_function(array($this, "deviceClose"));
+            $this->os = "osx";
+            register_shutdown_function([$this, "deviceClose"]);
         } elseif (substr($sysName, 0, 7) === "Windows") {
-            $this->_os = "windows";
-            register_shutdown_function(array($this, "deviceClose"));
+            $this->os = "windows";
+            register_shutdown_function([$this, "deviceClose"]);
         } else {
-            trigger_error("Host OS is neither osx, linux nor windows, unable " .
-                          "to run.", E_USER_ERROR);
+            trigger_error(
+                "Host OS is neither osx, linux nor windows, unable " . "to run.",
+                E_USER_ERROR
+            );
             exit();
         }
     }
@@ -71,58 +69,52 @@ public function PhpSerial()
     //
     // OPEN/CLOSE DEVICE SECTION -- {START}
     //
-
     /**
      * Device set function : used to set the device name/address.
      * -> linux : use the device address, like /dev/ttyS0
      * -> osx : use the device address, like /dev/tty.serial
      * -> windows : use the COMxx device name, like COM1 (can also be used
      *     with linux)
-     *
-     * @param  string $device the name of the device to be used
-     * @return bool
      */
-    public function deviceSet($device)
+    public function deviceSet(string $device): bool
     {
-        if ($this->_dState !== SERIAL_DEVICE_OPENED) {
-            if ($this->_os === "linux") {
+        if ($this->dState !== SERIAL_DEVICE_OPENED) {
+            if ($this->os === "linux") {
                 if (preg_match("@^COM(\\d+):?$@i", $device, $matches)) {
                     $device = "/dev/ttyS" . ($matches[1] - 1);
                 }
-
                 if ($this->_exec("stty -F " . $device) === 0) {
-                    $this->_device = $device;
-                    $this->_dState = SERIAL_DEVICE_SET;
+                    $this->device = $device;
+                    $this->dState = SERIAL_DEVICE_SET;
 
                     return true;
                 }
-            } elseif ($this->_os === "osx") {
+            } elseif ($this->os === "osx") {
                 if ($this->_exec("stty -f " . $device) === 0) {
-                    $this->_device = $device;
-                    $this->_dState = SERIAL_DEVICE_SET;
+                    $this->device = $device;
+                    $this->dState = SERIAL_DEVICE_SET;
 
                     return true;
                 }
-            } elseif ($this->_os === "windows") {
-                if (preg_match("@^COM(\\d+):?$@i", $device, $matches)
-                        and $this->_exec(
-                            exec("mode " . $device . " xon=on BAUD=9600")
-                        ) === 0
-                ) {
-                    $this->_winDevice = "COM" . $matches[1];
-                    $this->_device = "\\.com" . $matches[1];
-                    $this->_dState = SERIAL_DEVICE_SET;
+            } elseif ($this->os === "windows") {
+                if (preg_match("@^COM(\\d+):?$@i", $device, $matches) and $this->_exec(
+                        exec("mode " . $device . " xon=on BAUD=9600")
+                    ) === 0) {
+                    $this->winDevice = "COM" . $matches[1];
+                    $this->device    = "\\.com" . $matches[1];
+                    $this->dState    = SERIAL_DEVICE_SET;
 
                     return true;
                 }
             }
-
             trigger_error("Specified serial port is not valid", E_USER_WARNING);
 
             return false;
         } else {
-            trigger_error("You must close your device before to set an other " .
-                          "one", E_USER_WARNING);
+            trigger_error(
+                "You must close your device before to set an other " . "one",
+                E_USER_WARNING
+            );
 
             return false;
         }
@@ -130,19 +122,15 @@ public function deviceSet($device)
 
     /**
      * Opens the device for reading and/or writing.
-     *
-     * @param  string $mode Opening mode : same parameter as fopen()
-     * @return bool
      */
-    public function deviceOpen($mode = "r+b")
+    public function deviceOpen(string $mode = "r+b"): bool
     {
-        if ($this->_dState === SERIAL_DEVICE_OPENED) {
+        if ($this->dState === SERIAL_DEVICE_OPENED) {
             trigger_error("The device is already opened", E_USER_NOTICE);
 
             return true;
         }
-
-        if ($this->_dState === SERIAL_DEVICE_NOTSET) {
+        if ($this->dState === SERIAL_DEVICE_NOTSET) {
             trigger_error(
                 "The device must be set before to be open",
                 E_USER_WARNING
@@ -150,26 +138,22 @@ public function deviceOpen($mode = "r+b")
 
             return false;
         }
-
         if (!preg_match("@^[raw]\\+?b?$@", $mode)) {
             trigger_error(
-                "Invalid opening mode : ".$mode.". Use fopen() modes.",
+                "Invalid opening mode : " . $mode . ". Use fopen() modes.",
                 E_USER_WARNING
             );
 
             return false;
         }
-
-        $this->_dHandle = @fopen($this->_device, $mode);
-
-        if ($this->_dHandle !== false) {
-            stream_set_blocking($this->_dHandle, 0);
-            $this->_dState = SERIAL_DEVICE_OPENED;
+        $this->dHandle = @fopen($this->device, $mode);
+        if ($this->dHandle !== false) {
+            stream_set_blocking($this->dHandle, 0);
+            $this->dState = SERIAL_DEVICE_OPENED;
 
             return true;
         }
-
-        $this->_dHandle = null;
+        $this->dHandle = null;
         trigger_error("Unable to open the device", E_USER_WARNING);
 
         return false;
@@ -177,22 +161,18 @@ public function deviceOpen($mode = "r+b")
 
     /**
      * Closes the device
-     *
-     * @return bool
      */
-    public function deviceClose()
+    public function deviceClose(): bool
     {
-        if ($this->_dState !== SERIAL_DEVICE_OPENED) {
+        if ($this->dState !== SERIAL_DEVICE_OPENED) {
             return true;
         }
-
-        if (fclose($this->_dHandle)) {
-            $this->_dHandle = null;
-            $this->_dState = SERIAL_DEVICE_SET;
+        if (fclose($this->dHandle)) {
+            $this->dHandle = null;
+            $this->dState  = SERIAL_DEVICE_SET;
 
             return true;
         }
-
         trigger_error("Unable to close the device", E_USER_ERROR);
 
         return false;
@@ -201,63 +181,57 @@ public function deviceClose()
     //
     // OPEN/CLOSE DEVICE SECTION -- {STOP}
     //
-
     //
     // CONFIGURE SECTION -- {START}
     //
-
     /**
      * Configure the Baud Rate
      * Possible rates : 110, 150, 300, 600, 1200, 2400, 4800, 9600, 38400,
      * 57600 and 115200.
-     *
-     * @param  int  $rate the rate to set the port in
-     * @return bool
      */
-    public function confBaudRate($rate)
+    public function confBaudRate(int $rate): bool
     {
-        if ($this->_dState !== SERIAL_DEVICE_SET) {
-            trigger_error("Unable to set the baud rate : the device is " .
-                          "either not set or opened", E_USER_WARNING);
+        if ($this->dState !== SERIAL_DEVICE_SET) {
+            trigger_error(
+                "Unable to set the baud rate : the device is " . "either not set or opened",
+                E_USER_WARNING
+            );
 
             return false;
         }
-
-        $validBauds = array (
-            110    => 11,
-            150    => 15,
-            300    => 30,
-            600    => 60,
-            1200   => 12,
-            2400   => 24,
-            4800   => 48,
-            9600   => 96,
-            19200  => 19,
-            38400  => 38400,
-            57600  => 57600,
-            115200 => 115200
-        );
-
+        $validBauds = [
+            BaudInterface::RATE_110    => 11,
+            BaudInterface::RATE_150    => 15,
+            BaudInterface::RATE_300    => 30,
+            BaudInterface::RATE_600    => 60,
+            BaudInterface::RATE_1200   => 12,
+            BaudInterface::RATE_2400   => 24,
+            BaudInterface::RATE_4800   => 48,
+            BaudInterface::RATE_9600   => 96,
+            BaudInterface::RATE_19200  => 19,
+            BaudInterface::RATE_38400  => 38400,
+            BaudInterface::RATE_57600  => 57600,
+            BaudInterface::RATE_115200 => 115200
+        ];
         if (isset($validBauds[$rate])) {
-            if ($this->_os === "linux") {
+            if ($this->os === "linux") {
                 $ret = $this->_exec(
-                    "stty -F " . $this->_device . " " . (int) $rate,
+                    "stty -F " . $this->device . " " . (int)$rate,
                     $out
                 );
-            } elseif ($this->_os === "osx") {
+            } elseif ($this->os === "osx") {
                 $ret = $this->_exec(
-                    "stty -f " . $this->_device . " " . (int) $rate,
+                    "stty -f " . $this->device . " " . (int)$rate,
                     $out
                 );
-            } elseif ($this->_os === "windows") {
+            } elseif ($this->os === "windows") {
                 $ret = $this->_exec(
-                    "mode " . $this->_winDevice . " BAUD=" . $validBauds[$rate],
+                    "mode " . $this->winDevice . " BAUD=" . $validBauds[$rate],
                     $out
                 );
             } else {
                 return false;
             }
-
             if ($ret !== 0) {
                 trigger_error(
                     "Unable to set baud rate: " . $out[1],
@@ -276,13 +250,10 @@ public function confBaudRate($rate)
     /**
      * Configure parity.
      * Modes : odd, even, none
-     *
-     * @param  string $parity one of the modes
-     * @return bool
      */
-    public function confParity($parity)
+    public function confParity(string $parity): bool
     {
-        if ($this->_dState !== SERIAL_DEVICE_SET) {
+        if ($this->dState !== SERIAL_DEVICE_SET) {
             trigger_error(
                 "Unable to set parity : the device is either not set or opened",
                 E_USER_WARNING
@@ -290,40 +261,35 @@ public function confParity($parity)
 
             return false;
         }
-
-        $args = array(
-            "none" => "-parenb",
-            "odd"  => "parenb parodd",
-            "even" => "parenb -parodd",
-        );
-
+        $args = [
+            ParityInterface::NONE => "-parenb",
+            ParityInterface::ODD  => "parenb parodd",
+            ParityInterface::EVEN => "parenb -parodd",
+        ];
         if (!isset($args[$parity])) {
             trigger_error("Parity mode not supported", E_USER_WARNING);
 
             return false;
         }
-
-        if ($this->_os === "linux") {
+        if ($this->os === "linux") {
             $ret = $this->_exec(
-                "stty -F " . $this->_device . " " . $args[$parity],
+                "stty -F " . $this->device . " " . $args[$parity],
                 $out
             );
-        } elseif ($this->_os === "osx") {
+        } elseif ($this->os === "osx") {
             $ret = $this->_exec(
-                "stty -f " . $this->_device . " " . $args[$parity],
+                "stty -f " . $this->device . " " . $args[$parity],
                 $out
             );
         } else {
             $ret = $this->_exec(
-                "mode " . $this->_winDevice . " PARITY=" . $parity{0},
+                "mode " . $this->winDevice . " PARITY=" . $parity[0],
                 $out
             );
         }
-
         if ($ret === 0) {
             return true;
         }
-
         trigger_error("Unable to set parity : " . $out[1], E_USER_WARNING);
 
         return false;
@@ -332,48 +298,45 @@ public function confParity($parity)
     /**
      * Sets the length of a character.
      *
-     * @param  int  $int length of a character (5 <= length <= 8)
-     * @return bool
+     * $length length of a character (5 <= length <= 8)
      */
-    public function confCharacterLength($int)
+    public function confCharacterLength(int $length): bool
     {
-        if ($this->_dState !== SERIAL_DEVICE_SET) {
-            trigger_error("Unable to set length of a character : the device " .
-                          "is either not set or opened", E_USER_WARNING);
+        if ($this->dState !== SERIAL_DEVICE_SET) {
+            trigger_error(
+                "Unable to set length of a character : the device " . "is either not set or opened",
+                E_USER_WARNING
+            );
 
             return false;
         }
-
-        $int = (int) $int;
-        if ($int < 5) {
-            $int = 5;
-        } elseif ($int > 8) {
-            $int = 8;
+        $length = (int)$length;
+        if ($length < 5) {
+            $length = 5;
+        } elseif ($length > 8) {
+            $length = 8;
         }
-
-        if ($this->_os === "linux") {
+        if ($this->os === "linux") {
             $ret = $this->_exec(
-                "stty -F " . $this->_device . " cs" . $int,
+                "stty -F " . $this->device . " cs" . $length,
                 $out
             );
-        } elseif ($this->_os === "osx") {
+        } elseif ($this->os === "osx") {
             $ret = $this->_exec(
-                "stty -f " . $this->_device . " cs" . $int,
+                "stty -f " . $this->device . " cs" . $length,
                 $out
             );
         } else {
             $ret = $this->_exec(
-                "mode " . $this->_winDevice . " DATA=" . $int,
+                "mode " . $this->winDevice . " DATA=" . $length,
                 $out
             );
         }
-
         if ($ret === 0) {
             return true;
         }
-
         trigger_error(
-            "Unable to set character length : " .$out[1],
+            "Unable to set character length : " . $out[1],
             E_USER_WARNING
         );
 
@@ -383,25 +346,21 @@ public function confCharacterLength($int)
     /**
      * Sets the length of stop bits.
      *
-     * @param  float $length the length of a stop bit. It must be either 1,
+     *  $length the length of a stop bit. It must be either 1,
      *                       1.5 or 2. 1.5 is not supported under linux and on
      *                       some computers.
-     * @return bool
      */
-    public function confStopBits($length)
+    public function confStopBits(float $length): bool
     {
-        if ($this->_dState !== SERIAL_DEVICE_SET) {
-            trigger_error("Unable to set the length of a stop bit : the " .
-                          "device is either not set or opened", E_USER_WARNING);
+        if ($this->dState !== SERIAL_DEVICE_SET) {
+            trigger_error(
+                "Unable to set the length of a stop bit : the " . "device is either not set or opened",
+                E_USER_WARNING
+            );
 
             return false;
         }
-
-        if ($length != 1
-                and $length != 2
-                and $length != 1.5
-                and !($length == 1.5 and $this->_os === "linux")
-        ) {
+        if ($length != 1 and $length != 2 and $length != 1.5 and !($length == 1.5 and $this->os === "linux")) {
             trigger_error(
                 "Specified stop bit length is invalid",
                 E_USER_WARNING
@@ -409,30 +368,25 @@ public function confStopBits($length)
 
             return false;
         }
-
-        if ($this->_os === "linux") {
+        if ($this->os === "linux") {
             $ret = $this->_exec(
-                "stty -F " . $this->_device . " " .
-                    (($length == 1) ? "-" : "") . "cstopb",
+                "stty -F " . $this->device . " " . (($length == 1) ? "-" : "") . "cstopb",
                 $out
             );
-        } elseif ($this->_os === "osx") {
+        } elseif ($this->os === "osx") {
             $ret = $this->_exec(
-                "stty -f " . $this->_device . " " .
-                    (($length == 1) ? "-" : "") . "cstopb",
+                "stty -f " . $this->device . " " . (($length == 1) ? "-" : "") . "cstopb",
                 $out
             );
         } else {
             $ret = $this->_exec(
-                "mode " . $this->_winDevice . " STOP=" . $length,
+                "mode " . $this->winDevice . " STOP=" . $length,
                 $out
             );
         }
-
         if ($ret === 0) {
             return true;
         }
-
         trigger_error(
             "Unable to set stop bit length : " . $out[1],
             E_USER_WARNING
@@ -444,55 +398,54 @@ public function confStopBits($length)
     /**
      * Configures the flow control
      *
-     * @param  string $mode Set the flow control mode. Availible modes :
+     * @param string $mode Set the flow control mode. Availible modes :
      *                      -> "none" : no flow control
      *                      -> "rts/cts" : use RTS/CTS handshaking
      *                      -> "xon/xoff" : use XON/XOFF protocol
+     *
      * @return bool
      */
     public function confFlowControl($mode)
     {
-        if ($this->_dState !== SERIAL_DEVICE_SET) {
-            trigger_error("Unable to set flow control mode : the device is " .
-                          "either not set or opened", E_USER_WARNING);
+        if ($this->dState !== SERIAL_DEVICE_SET) {
+            trigger_error(
+                "Unable to set flow control mode : the device is " . "either not set or opened",
+                E_USER_WARNING
+            );
 
             return false;
         }
-
-        $linuxModes = array(
+        $linuxModes   = [
             "none"     => "clocal -crtscts -ixon -ixoff",
             "rts/cts"  => "-clocal crtscts -ixon -ixoff",
             "xon/xoff" => "-clocal -crtscts ixon ixoff"
-        );
-        $windowsModes = array(
+        ];
+        $windowsModes = [
             "none"     => "xon=off octs=off rts=on",
             "rts/cts"  => "xon=off octs=on rts=hs",
             "xon/xoff" => "xon=on octs=off rts=on",
-        );
-
+        ];
         if ($mode !== "none" and $mode !== "rts/cts" and $mode !== "xon/xoff") {
             trigger_error("Invalid flow control mode specified", E_USER_ERROR);
 
             return false;
         }
-
-        if ($this->_os === "linux") {
+        if ($this->os === "linux") {
             $ret = $this->_exec(
-                "stty -F " . $this->_device . " " . $linuxModes[$mode],
+                "stty -F " . $this->device . " " . $linuxModes[$mode],
                 $out
             );
-        } elseif ($this->_os === "osx") {
+        } elseif ($this->os === "osx") {
             $ret = $this->_exec(
-                "stty -f " . $this->_device . " " . $linuxModes[$mode],
+                "stty -f " . $this->device . " " . $linuxModes[$mode],
                 $out
             );
         } else {
             $ret = $this->_exec(
-                "mode " . $this->_winDevice . " " . $windowsModes[$mode],
+                "mode " . $this->winDevice . " " . $windowsModes[$mode],
                 $out
             );
         }
-
         if ($ret === 0) {
             return true;
         } else {
@@ -508,11 +461,12 @@ public function confFlowControl($mode)
     /**
      * Sets a setserial parameter (cf man setserial)
      * NO MORE USEFUL !
-     * 	-> No longer supported
-     * 	-> Only use it if you need it
+     *    -> No longer supported
+     *    -> Only use it if you need it
+     *
+     * @param string $param parameter name
+     * @param string $arg parameter value
      *
-     * @param  string $param parameter name
-     * @param  string $arg   parameter value
      * @return bool
      */
     public function setSetserialFlag($param, $arg = "")
@@ -520,16 +474,14 @@ public function setSetserialFlag($param, $arg = "")
         if (!$this->_ckOpened()) {
             return false;
         }
-
         $return = exec(
-            "setserial " . $this->_device . " " . $param . " " . $arg . " 2>&1"
+            "setserial " . $this->device . " " . $param . " " . $arg . " 2>&1"
         );
-
-        if ($return{0} === "I") {
+        if (strpos($return, 'I') === 0) {
             trigger_error("setserial: Invalid flag", E_USER_WARNING);
 
             return false;
-        } elseif ($return{0} === "/") {
+        } elseif (strpos($return, '/') === 0) {
             trigger_error("setserial: Error with device file", E_USER_WARNING);
 
             return false;
@@ -541,26 +493,22 @@ public function setSetserialFlag($param, $arg = "")
     //
     // CONFIGURE SECTION -- {STOP}
     //
-
     //
     // I/O SECTION -- {START}
     //
-
     /**
      * Sends a string to the device
      *
-     * @param string $str          string to be sent to the device
+     * @param string $str string to be sent to the device
      * @param float  $waitForReply time to wait for the reply (in seconds)
      */
     public function sendMessage($str, $waitForReply = 0.1)
     {
-        $this->_buffer .= $str;
-
+        $this->buffer .= $str;
         if ($this->autoFlush === true) {
             $this->serialflush();
         }
-
-        usleep((int) ($waitForReply * 1000000));
+        usleep((int)($waitForReply * 1000000));
     }
 
     /**
@@ -568,52 +516,52 @@ public function sendMessage($str, $waitForReply = 0.1)
      *
      * @param int $count Number of characters to be read (will stop before
      *                   if less characters are in the buffer)
+     *
      * @return string
      */
-    public function readPort($count = 0)
+    public function readPort(int $count = 0): string
     {
-        if ($this->_dState !== SERIAL_DEVICE_OPENED) {
+        if ($this->dState !== SERIAL_DEVICE_OPENED) {
             trigger_error("Device must be opened to read it", E_USER_WARNING);
 
             return false;
         }
-
-        if ($this->_os === "linux" || $this->_os === "osx") {
+        if ($this->os === "linux" || $this->os === "osx") {
             // Behavior in OSX isn't to wait for new data to recover, but just
             // grabs what's there!
             // Doesn't always work perfectly for me in OSX
-            $content = ""; $i = 0;
-
+            $content = "";
+            $i       = 0;
             if ($count !== 0) {
                 do {
                     if ($i > $count) {
-                        $content .= fread($this->_dHandle, ($count - $i));
+                        $content .= fread($this->dHandle, ($count - $i));
                     } else {
-                        $content .= fread($this->_dHandle, 128);
+                        $content .= fread($this->dHandle, 128);
                     }
                 } while (($i += 128) === strlen($content));
             } else {
                 do {
-                    $content .= fread($this->_dHandle, 128);
+                    $content .= fread($this->dHandle, 128);
                 } while (($i += 128) === strlen($content));
             }
 
             return $content;
-        } elseif ($this->_os === "windows") {
+        } elseif ($this->os === "windows") {
             // Windows port reading procedures still buggy
-            $content = ""; $i = 0;
-
+            $content = "";
+            $i       = 0;
             if ($count !== 0) {
                 do {
                     if ($i > $count) {
-                        $content .= fread($this->_dHandle, ($count - $i));
+                        $content .= fread($this->dHandle, ($count - $i));
                     } else {
-                        $content .= fread($this->_dHandle, 128);
+                        $content .= fread($this->dHandle, 128);
                     }
                 } while (($i += 128) === strlen($content));
             } else {
                 do {
-                    $content .= fread($this->_dHandle, 128);
+                    $content .= fread($this->dHandle, 128);
                 } while (($i += 128) === strlen($content));
             }
 
@@ -634,13 +582,12 @@ public function serialflush()
         if (!$this->_ckOpened()) {
             return false;
         }
-
-        if (fwrite($this->_dHandle, $this->_buffer) !== false) {
-            $this->_buffer = "";
+        if (fwrite($this->dHandle, $this->buffer) !== false) {
+            $this->buffer = "";
 
             return true;
         } else {
-            $this->_buffer = "";
+            $this->buffer = "";
             trigger_error("Error while sending message", E_USER_WARNING);
 
             return false;
@@ -650,14 +597,12 @@ public function serialflush()
     //
     // I/O SECTION -- {STOP}
     //
-
     //
     // INTERNAL TOOLKIT -- {START}
     //
-
     public function _ckOpened()
     {
-        if ($this->_dState !== SERIAL_DEVICE_OPENED) {
+        if ($this->dState !== SERIAL_DEVICE_OPENED) {
             trigger_error("Device must be opened", E_USER_WARNING);
 
             return false;
@@ -668,7 +613,7 @@ public function _ckOpened()
 
     public function _ckClosed()
     {
-        if ($this->_dState === SERIAL_DEVICE_OPENED) {
+        if ($this->dState === SERIAL_DEVICE_OPENED) {
             trigger_error("Device must be closed", E_USER_WARNING);
 
             return false;
@@ -679,26 +624,30 @@ public function _ckClosed()
 
     public function _exec($cmd, &$out = null)
     {
-        $desc = array(
-            1 => array("pipe", "w"),
-            2 => array("pipe", "w")
-        );
-
+        $desc = [
+            1 => ["pipe", "w"],
+            2 => ["pipe", "w"]
+        ];
         $proc = proc_open($cmd, $desc, $pipes);
-
-        $ret = stream_get_contents($pipes[1]);
-        $err = stream_get_contents($pipes[2]);
-
+        $ret  = stream_get_contents($pipes[1]);
+        $err  = stream_get_contents($pipes[2]);
         fclose($pipes[1]);
         fclose($pipes[2]);
-
         $retVal = proc_close($proc);
+        if (func_num_args() == 2) {
+            $out = [$ret, $err];
+        }
 
-        if (func_num_args() == 2) $out = array($ret, $err);
         return $retVal;
     }
 
     //
     // INTERNAL TOOLKIT -- {STOP}
     //
+
+    public function __destruct()
+    {
+        $this->deviceClose();
+    }
 }
+