Ignore:
Timestamp:
Nov 20, 2020, 12:08:12 AM (3 years ago)
Author:
chronos
Message:
  • Added: Static types added to almost all classes, methods and function. Supported by PHP 7.4.
  • Fixed: Various found code issues.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/NetworkConfigRouterOS/RouterboardAPI.php

    r874 r887  
    2525  }
    2626
    27   function EncodeLength($Length)
    28   {
    29     if ($Length < 0x80) {
     27  function EncodeLength(int $Length): int
     28  {
     29    if ($Length < 0x80)
     30    {
    3031      $Length = chr($Length);
    31     } else if ($Length < 0x4000) {
     32    } else if ($Length < 0x4000)
     33    {
    3234      $Length |= 0x8000;
    3335      $Length = chr(($Length >> 8) & 0xFF).chr($Length & 0xFF);
    34     } else if ($Length < 0x200000) {
     36    } else if ($Length < 0x200000)
     37    {
    3538      $Length |= 0xC00000;
    3639      $Length = chr(($Length >> 16) & 0xFF).chr(($Length >> 8) & 0xFF).chr($Length & 0xFF);
    37     } else if ($Length < 0x10000000) {
     40    } else if ($Length < 0x10000000)
     41    {
    3842      $Length |= 0xE0000000;
    3943      $Length = chr(($Length >> 24) & 0xFF).chr(($Length >> 16) & 0xFF).chr(($Length >> 8) & 0xFF).chr($Length & 0xFF);
     
    4347  }
    4448
    45   function ConnectOnce($IP, $Login, $Password)
     49  function ConnectOnce(string $IP, string $Login, string $Password): void
    4650  {
    4751    if ($this->Connected) $this->Disconnect();
     
    6367  }
    6468
    65   function Connect($IP, $Login, $Password)
     69  function Connect(string $IP, string $Login, string $Password): bool
    6670  {
    6771    for ($Attempt = 1; $Attempt <= $this->Attempts; $Attempt++)
     
    7478  }
    7579
    76   function Disconnect()
     80  function Disconnect(): void
    7781  {
    7882    if ($this->Connected)
     
    8387  }
    8488
    85   function ParseResponse($Response)
    86   {
    87     if (is_array($Response)) {
     89  function ParseResponse(array $Response): array
     90  {
     91    if (is_array($Response))
     92    {
    8893      $Parsed      = array();
    8994      $Current     = null;
    9095      $SingleValue = null;
    9196      $count       = 0;
    92       foreach ($Response as $x) {
     97      foreach ($Response as $x)
     98      {
    9399        if (in_array($x, array(
    94100            '!fatal',
    95101            '!re',
    96102            '!trap'
    97         ))) {
    98           if ($x == '!re') {
     103        )))
     104        {
     105          if ($x == '!re')
     106          {
    99107            $Current =& $Parsed[];
    100108          } else
    101109            $Current =& $Parsed[$x][];
    102         } else if ($x != '!done') {
    103           if (preg_match_all('/[^=]+/i', $x, $Matches)) {
     110        } else if ($x != '!done')
     111        {
     112          if (preg_match_all('/[^=]+/i', $x, $Matches))
     113          {
    104114            if ($Matches[0][0] == 'ret') {
    105115              $SingleValue = $Matches[0][1];
     
    109119        }
    110120      }
    111       if (empty($Parsed) && !is_null($SingleValue)) {
     121      if (empty($Parsed) && !is_null($SingleValue))
     122      {
    112123        $Parsed = $SingleValue;
    113124      }
     
    117128  }
    118129
    119   function ArrayChangeKeyName(&$array)
    120   {
    121     if (is_array($array)) {
    122       foreach ($array as $k => $v) {
     130  function ArrayChangeKeyName(array &$array): array
     131  {
     132    if (is_array($array))
     133    {
     134      foreach ($array as $k => $v)
     135      {
    123136        $tmp = str_replace("-", "_", $k);
    124137        $tmp = str_replace("/", "_", $tmp);
    125         if ($tmp) {
     138        if ($tmp)
     139        {
    126140          $array_new[$tmp] = $v;
    127         } else {
     141        } else
     142        {
    128143          $array_new[$k] = $v;
    129144        }
    130145      }
    131146      return $array_new;
    132     } else {
     147    } else
     148    {
    133149      return $array;
    134150    }
    135151  }
    136152
    137   function Read($Parse = true)
     153  function Read(bool $Parse = true): array
    138154  {
    139155    $Line = '';
    140156    $Response = array();
    141     while (true) {
     157    while (true)
     158    {
    142159      // Read the first byte of input which gives us some or all of the length
    143160      // of the remaining reply.
     
    149166      // If the fourth bit is set, we need to remove anything left in the first byte
    150167      // and then read in yet another byte.
    151       if ($Byte & 0x80) {
    152         if (($Byte & 0xc0) == 0x80) {
     168      if ($Byte & 0x80)
     169      {
     170        if (($Byte & 0xc0) == 0x80)
     171        {
    153172          $Length = (($Byte & 63) << 8) + ord(fread($this->Socket, 1));
    154         } else {
    155           if (($Byte & 0xe0) == 0xc0) {
     173        } else
     174        {
     175          if (($Byte & 0xe0) == 0xc0)
     176          {
    156177            $Length = (($Byte & 31) << 8) + ord(fread($this->Socket, 1));
    157178            $Length = ($Length << 8) + ord(fread($this->Socket, 1));
    158           } else {
    159             if (($Byte & 0xf0) == 0xe0) {
     179          } else
     180          {
     181            if (($Byte & 0xf0) == 0xe0)
     182            {
    160183              $Length = (($Byte & 15) << 8) + ord(fread($this->Socket, 1));
    161184              $Length = ($Length << 8) + ord(fread($this->Socket, 1));
    162185              $Length = ($Length << 8) + ord(fread($this->Socket, 1));
    163             } else {
     186            } else
     187            {
    164188              $Length = ord(fread($this->Socket, 1));
    165189              $Length = ($Length << 8) + ord(fread($this->Socket, 1));
     
    169193          }
    170194        }
    171       } else {
     195      } else
     196      {
    172197        $Length = $Byte;
    173198      }
    174199      // If we have got more characters to read, read them in.
    175       if ($Length > 0) {
     200      if ($Length > 0)
     201      {
    176202        $Line = '';
    177203        $RetLen = 0;
    178         while ($RetLen < $Length) {
     204        while ($RetLen < $Length)
     205        {
    179206          $ToRead = $Length - $RetLen;
    180207          $Line .= fread($this->Socket, $ToRead);
     
    196223  }
    197224
    198   function Write($Command, $Param2 = true)
     225  function Write(string $Command, bool $Param2 = true): bool
    199226  {
    200227    if ($Command)
    201228    {
    202229      $Data = explode("\n", $Command);
    203       foreach ($Data as $Com) {
     230      foreach ($Data as $Com)
     231      {
    204232        $Com = trim($Com);
    205233        fwrite($this->Socket, $this->EncodeLength(strlen($Com)).$Com);
    206234      }
    207       if (gettype($Param2) == 'integer') {
     235      if (gettype($Param2) == 'integer')
     236      {
    208237        fwrite($this->Socket, $this->EncodeLength(strlen('.tag='.$Param2)).'.tag='.$Param2.chr(0));
    209238      } else if (gettype($Param2) == 'boolean')
     
    214243  }
    215244
    216   function Comm($Com, $Arr = array())
     245  function Comm(string $Com, array $Arr = array()): array
    217246  {
    218247    $Count = count($Arr);
    219248    $this->write($Com, !$Arr);
    220249    $i = 0;
    221     foreach ($Arr as $k => $v) {
    222       switch ($k[0]) {
     250    foreach ($Arr as $k => $v)
     251    {
     252      switch ($k[0])
     253      {
    223254        case "?":
    224255          $el = "$k=$v";
Note: See TracChangeset for help on using the changeset viewer.