From e572adb9848723fd3dafba93fcf918e00e728edb Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Thu, 18 Jun 2020 22:49:18 +0300 Subject: [PATCH] Finetune of code --- src/APIConnector.php | 5 +++-- src/APILengthCoDec.php | 16 +++++++++------- src/Client.php | 34 +++++++++++++++++++--------------- src/Config.php | 5 +++-- src/Query.php | 21 ++++++++++++--------- src/ResponseIterator.php | 12 ++++++------ src/SocketTrait.php | 19 ++++++++++--------- 7 files changed, 62 insertions(+), 50 deletions(-) diff --git a/src/APIConnector.php b/src/APIConnector.php index f85c013..9da509d 100644 --- a/src/APIConnector.php +++ b/src/APIConnector.php @@ -48,8 +48,9 @@ class APIConnector /** * Write word to stream * - * @param string $word - * @return int return number of written bytes + * @param string $word + * + * @return int return number of written bytes */ public function writeWord(string $word): int { diff --git a/src/APILengthCoDec.php b/src/APILengthCoDec.php index 2f275bd..ad83c9c 100644 --- a/src/APILengthCoDec.php +++ b/src/APILengthCoDec.php @@ -2,6 +2,7 @@ namespace RouterOS; +use DomainException; use RouterOS\Interfaces\StreamInterface; use RouterOS\Helpers\BinaryStringHelper; @@ -18,7 +19,8 @@ class APILengthCoDec /** * Encode string to length of string * - * @param int|float $length + * @param int|float $length + * * @return string */ public static function encodeLength($length): string @@ -54,7 +56,7 @@ class APILengthCoDec // - length > 0x7FFFFFFFFF : not supported if ($length < 0) { - throw new \DomainException("Length of word could not to be negative ($length)"); + throw new DomainException("Length of word could not to be negative ($length)"); } if ($length <= 0x7F) { @@ -81,18 +83,18 @@ class APILengthCoDec // Decode length of data when reading : // The 5 firsts bits of the first byte specify how the length is encoded. // The position of the first 0 value bit, starting from the most significant postion. - // - 0xxxxxxx => The 7 remainings bits of the first byte is the length : + // - 0xxxxxxx => The 7 remaining bits of the first byte is the length : // => min value of length is 0x00 // => max value of length is 0x7F (127 bytes) - // - 10xxxxxx => The 6 remainings bits of the first byte plus the next byte represent the lenght + // - 10xxxxxx => The 6 remaining bits of the first byte plus the next byte represent the lenght // NOTE : the next byte MUST be at least 0x80 !! // => min value of length is 0x80 // => max value of length is 0x3FFF (16,383 bytes, near 16 KB) - // - 110xxxxx => The 5 remainings bits of th first byte and the two next bytes represent the length + // - 110xxxxx => The 5 remaining bits of th first byte and the two next bytes represent the length // => max value of length is 0x1FFFFF (2,097,151 bytes, near 2 MB) - // - 1110xxxx => The 4 remainings bits of the first byte and the three next bytes represent the length + // - 1110xxxx => The 4 remaining bits of the first byte and the three next bytes represent the length // => max value of length is 0xFFFFFFF (268,435,455 bytes, near 270 MB) - // - 11110xxx => The 3 remainings bits of the first byte and the four next bytes represent the length + // - 11110xxx => The 3 remaining bits of the first byte and the four next bytes represent the length // => max value of length is 0x7FFFFFFF (2,147,483,647 byes, 2GB) // - 11111xxx => This byte is not a length-encoded word but a control byte. // => Extracted from Mikrotik API doc : diff --git a/src/Client.php b/src/Client.php index be1469c..a4ae8c3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,6 +6,10 @@ use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\ConfigException; use RouterOS\Exceptions\QueryException; use RouterOS\Helpers\ArrayHelper; +use function chr; +use function count; +use function is_array; +use function is_string; /** * Class Client for RouterOS management @@ -44,7 +48,7 @@ class Client implements Interfaces\ClientInterface public function __construct($config) { // If array then need create object - if (\is_array($config)) { + if (is_array($config)) { $config = new Config($config); } @@ -87,9 +91,9 @@ class Client implements Interfaces\ClientInterface */ public function write($query): Client { - if (\is_string($query)) { + if (is_string($query)) { $query = new Query($query); - } elseif (\is_array($query)) { + } elseif (is_array($query)) { $endpoint = array_shift($query); $query = new Query($endpoint, $query); } @@ -134,15 +138,15 @@ class Client implements Interfaces\ClientInterface $operator = null; $value = null; - switch (\count($item)) { + switch (count($item)) { case 1: - list($key) = $item; + [$key] = $item; break; case 2: - list($key, $operator) = $item; + [$key, $operator] = $item; break; case 3: - list($key, $operator, $value) = $item; + [$key, $operator, $value] = $item; break; default: throw new ClientException('From 1 to 3 parameters of "where" condition is allowed'); @@ -155,15 +159,15 @@ class Client implements Interfaces\ClientInterface $operator = null; $value = null; - switch (\count($where)) { + switch (count($where)) { case 1: - list($key) = $where; + [$key] = $where; break; case 2: - list($key, $operator) = $where; + [$key, $operator] = $where; break; case 3: - list($key, $operator, $value) = $where; + [$key, $operator, $value] = $where; break; default: throw new ClientException('From 1 to 3 parameters of "where" condition is allowed'); @@ -342,7 +346,7 @@ class Client implements Interfaces\ClientInterface { $result = []; $i = -1; - $lines = \count($response); + $lines = count($response); foreach ($response as $key => $value) { switch ($value) { case '!re': @@ -383,7 +387,7 @@ class Client implements Interfaces\ClientInterface */ private function pregResponse(string $value, &$matches) { - preg_match_all('/^[=|\.](.*)=(.*)/', $value, $matches); + preg_match_all('/^[=|.](.*)=(.*)/', $value, $matches); } /** @@ -406,7 +410,7 @@ class Client implements Interfaces\ClientInterface // Now need use this hash for authorization $query = new Query('/login', [ '=name=' . $this->config('user'), - '=response=00' . md5(\chr(0) . $this->config('pass') . pack('H*', $response['after']['ret'])) + '=response=00' . md5(chr(0) . $this->config('pass') . pack('H*', $response['after']['ret'])) ]); } else { // Just login with our credentials @@ -452,7 +456,7 @@ class Client implements Interfaces\ClientInterface */ private function isLegacy(array &$response): bool { - return \count($response) > 1 && $response[0] === '!done' && !$this->config('legacy'); + return count($response) > 1 && $response[0] === '!done' && !$this->config('legacy'); } /** diff --git a/src/Config.php b/src/Config.php index c4ff807..fbabf75 100644 --- a/src/Config.php +++ b/src/Config.php @@ -6,6 +6,7 @@ use RouterOS\Exceptions\ConfigException; use RouterOS\Helpers\ArrayHelper; use RouterOS\Helpers\TypeHelper; use RouterOS\Interfaces\ConfigInterface; +use function gettype; /** * Class Config with array of parameters @@ -60,8 +61,8 @@ class Config implements ConfigInterface } // Check what type has this value - if (TypeHelper::checkIfTypeMismatch(\gettype($value), self::ALLOWED[$name])) { - throw new ConfigException("Parameter '$name' has wrong type '" . \gettype($value) . "' but should be '" . self::ALLOWED[$name] . "'"); + if (TypeHelper::checkIfTypeMismatch(gettype($value), self::ALLOWED[$name])) { + throw new ConfigException("Parameter '$name' has wrong type '" . gettype($value) . "' but should be '" . self::ALLOWED[$name] . "'"); } // Save value to array diff --git a/src/Query.php b/src/Query.php index 9458d91..cfc868b 100644 --- a/src/Query.php +++ b/src/Query.php @@ -5,6 +5,9 @@ namespace RouterOS; use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\QueryException; use RouterOS\Interfaces\QueryInterface; +use function in_array; +use function is_array; +use function is_string; /** * Class Query for building queries @@ -62,10 +65,10 @@ class Query implements QueryInterface */ public function __construct($endpoint, array $attributes = []) { - if (\is_string($endpoint)) { + if (is_string($endpoint)) { $this->setEndpoint($endpoint); $this->setAttributes($attributes); - } elseif (\is_array($endpoint)) { + } elseif (is_array($endpoint)) { $query = array_shift($endpoint); $this->setEndpoint($query); $this->setAttributes($endpoint); @@ -85,7 +88,7 @@ class Query implements QueryInterface * @throws \RouterOS\Exceptions\QueryException * @since 1.0.0 */ - public function where(string $key, $operator = null, $value = null): self + public function where(string $key, $operator = null, $value = null): Query { return $this->world('?' . $key, $operator, $value); } @@ -100,7 +103,7 @@ class Query implements QueryInterface * @throws \RouterOS\Exceptions\QueryException * @since 1.1 */ - public function equal(string $key, $value = null): self + public function equal(string $key, $value = null): Query { return $this->world('=' . $key, null, $value); } @@ -115,7 +118,7 @@ class Query implements QueryInterface * @return \RouterOS\Query * @throws \RouterOS\Exceptions\QueryException */ - private function world(string $key, $operator = null, $value = null): self + private function world(string $key, $operator = null, $value = null): Query { if (null !== $operator && null === $value) { @@ -128,7 +131,7 @@ class Query implements QueryInterface if (null !== $operator && null !== $value) { // If operator is available in list - if (\in_array($operator, self::AVAILABLE_OPERATORS, true)) { + if (in_array($operator, self::AVAILABLE_OPERATORS, true)) { $key = $operator . $key; } else { throw new QueryException('Operator "' . $operator . '" in not in allowed list [' . implode(',', self::AVAILABLE_OPERATORS) . ']'); @@ -151,7 +154,7 @@ class Query implements QueryInterface * @return \RouterOS\Query * @since 1.0.0 */ - public function operations(string $operations): self + public function operations(string $operations): Query { $this->_operations = '?#' . $operations; return $this; @@ -165,7 +168,7 @@ class Query implements QueryInterface * @return \RouterOS\Query * @since 1.0.0 */ - public function tag(string $name): self + public function tag(string $name): Query { $this->_tag = '.tag=' . $name; return $this; @@ -213,7 +216,7 @@ class Query implements QueryInterface * * @return string|null */ - public function getEndpoint() + public function getEndpoint(): ?string { return $this->_endpoint; } diff --git a/src/ResponseIterator.php b/src/ResponseIterator.php index 1498a60..4b742f5 100644 --- a/src/ResponseIterator.php +++ b/src/ResponseIterator.php @@ -95,7 +95,7 @@ class ResponseIterator implements Iterator, ArrayAccess, Countable, Serializable /** * Move forward to next element */ - public function next() + public function next(): void { ++$this->current; } @@ -103,7 +103,7 @@ class ResponseIterator implements Iterator, ArrayAccess, Countable, Serializable /** * Previous value */ - public function prev() + public function prev(): void { --$this->current; } @@ -165,7 +165,7 @@ class ResponseIterator implements Iterator, ArrayAccess, Countable, Serializable /** * Rewind the Iterator to the first element */ - public function rewind() + public function rewind(): void { $this->current = 0; } @@ -176,7 +176,7 @@ class ResponseIterator implements Iterator, ArrayAccess, Countable, Serializable * @param mixed $offset * @param mixed $value */ - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { if (null === $offset) { $this->parsed[] = $value; @@ -202,7 +202,7 @@ class ResponseIterator implements Iterator, ArrayAccess, Countable, Serializable * * @param mixed $offset */ - public function offsetUnset($offset) + public function offsetUnset($offset): void { unset($this->parsed[$offset], $this->raw[$offset]); } @@ -245,7 +245,7 @@ class ResponseIterator implements Iterator, ArrayAccess, Countable, Serializable * * @param string $serialized */ - public function unserialize($serialized) + public function unserialize($serialized): void { $this->raw = unserialize($serialized, null); } diff --git a/src/SocketTrait.php b/src/SocketTrait.php index cfa2461..d0a748a 100644 --- a/src/SocketTrait.php +++ b/src/SocketTrait.php @@ -30,11 +30,11 @@ trait SocketTrait /** * Initiate socket session * - * @return void - * @throws \RouterOS\Exceptions\ClientException - * @throws \RouterOS\Exceptions\ConfigException + * @return void + * @throws \RouterOS\Exceptions\ClientException + * @throws \RouterOS\Exceptions\ConfigException */ - private function openSocket() + private function openSocket(): void { // Default: Context for ssl $context = stream_context_create([ @@ -62,10 +62,10 @@ trait SocketTrait if (false === $socket) { throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str); } - + //Timeout read stream_set_timeout($socket, $this->config('timeout')); - + // Save socket to static variable $this->setSocket($socket); } @@ -83,10 +83,11 @@ trait SocketTrait /** * Save socket resource to static variable * - * @param resource $socket + * @param resource $socket + * * @return void */ - private function setSocket($socket) + private function setSocket($socket): void { $this->_socket = $socket; } @@ -94,7 +95,7 @@ trait SocketTrait /** * Return socket resource if is exist * - * @return resource + * @return resource */ public function getSocket() {