Skip to content

Commit

Permalink
Some CS fixes and static code analysis fixes from PhpStorm.
Browse files Browse the repository at this point in the history
  • Loading branch information
boenrobot committed Jul 6, 2019
1 parent ba08547 commit 10cc489
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 36 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"pear2/net_transmitter": ">=1.0.0b1 || dev-develop@dev"
},
"require-dev": {
"ext-iconv": "*",
"phpunit/phpunit": "@stable",
"squizlabs/php_codesniffer": "@stable",
"pear2/net_transmitter": "dev-develop@dev",
Expand All @@ -31,6 +32,7 @@
"pear2/cache_shm": "Enables persistent connections.",
"pear2/console_commandline": "Enables the console",
"pear2/console_color": "Enables colors in the console",
"ext-iconv": "Used for charset conversion",
"ext-apc": "This, APCu or Wincache is required for persistent connections.",
"ext-apcu": "This, APC or Wincache is required for persistent connections.",
"ext-wincache": "This, APC or APCu is required for persistent connections. Reccomended for Windows.",
Expand Down
2 changes: 1 addition & 1 deletion src/PEAR2/Net/RouterOS/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,8 @@ protected function send(Request $request)
* If NULL, wait indefinitely.
* @param int $usTimeout Microseconds to add to the waiting time.
*
* @return Response The dispatched response.
* @throws SocketException When there's no response within the time limit.
* @return Response The dispatched response.
*/
protected function dispatchNextResponse($sTimeout = 0, $usTimeout = 0)
{
Expand Down
45 changes: 30 additions & 15 deletions src/PEAR2/Net/RouterOS/Communicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function __construct(
$context = stream_context_get_default();
$opts = stream_context_get_options($context);
if (isset($opts['ssl']['verify_peer'])) {
$verifyPeer = !!$opts['ssl']['verify_peer'];
$verifyPeer = (bool)$opts['ssl']['verify_peer'];
} elseif (isset($opts['ssl']['ciphers'])
&& 'ADH' === $opts['ssl']['ciphers']
) {
Expand Down Expand Up @@ -197,7 +197,7 @@ public function __construct(
'verify_peer' => $verifyPeer,
'verify_peer_name' => isset(
$opts['ssl']['verify_peer_name']
) ? !!$opts['ssl']['verify_peer_name'] : $verifyPeer
) ? (bool)$opts['ssl']['verify_peer_name'] : $verifyPeer
)
);
if (!$verifyPeer && !isset($opts['ssl']['ciphers'])) {
Expand Down Expand Up @@ -582,21 +582,27 @@ public static function encodeLength($length)
null,
$length
);
} elseif ($length < 0x80) {
}
if ($length < 0x80) {
return chr($length);
} elseif ($length < 0x4000) {
}
if ($length < 0x4000) {
return pack('n', $length |= 0x8000);
} elseif ($length < 0x200000) {
}
if ($length < 0x200000) {
$length |= 0xC00000;
return pack('n', $length >> 8) . chr($length & 0xFF);
} elseif ($length < 0x10000000) {
}
if ($length < 0x10000000) {
return pack('N', $length |= 0xE0000000);
} elseif ($length <= 0xFFFFFFFF) {
}
if ($length <= 0xFFFFFFFF) {
return chr(0xF0) . pack('N', $length);
} elseif ($length <= 0x7FFFFFFFF) {
$length = 'f' . base_convert($length, 10, 16);
return chr(hexdec(substr($length, 0, 2))) .
pack('N', hexdec(substr($length, 2)));
}
if ($length <= 0x7FFFFFFFF) {
$lengthHex = 'f' . base_convert($length, 10, 16);
return chr(hexdec(substr($lengthHex, 0, 2))) .
pack('N', hexdec(substr($lengthHex, 2)));
}
throw new LengthException(
'Length must not be above 0x7FFFFFFFF.',
Expand Down Expand Up @@ -709,10 +715,13 @@ public function getNextWordAsStream()
*
* @return int|double The decoded length.
* Is of type "double" only for values above "2 << 31".
*
* @throws NotSupportedException If the stream contains an unsupported
* control byte.
*/
public static function decodeLength(T\Stream $trans)
{
if ($trans->isPersistent() && $trans instanceof T\TcpClient) {
if ($trans instanceof T\TcpClient && $trans->isPersistent()) {
$old = $trans->lock($trans::DIRECTION_RECEIVE);
$length = self::_decodeLength($trans);
$trans->lock($old, true);
Expand All @@ -735,20 +744,26 @@ public static function decodeLength(T\Stream $trans)
*
* @return int|double The decoded length.
* Is of type "double" only for values above "2 << 31".
*
* @throws NotSupportedException If the stream contains an unsupported
* control byte.
*/
private static function _decodeLength(T\Stream $trans)
{
$byte = ord($trans->receive(1, 'initial length byte'));
if ($byte & 0x80) {
if (($byte & 0xC0) === 0x80) {
return (($byte & 077) << 8 ) + ord($trans->receive(1));
} elseif (($byte & 0xE0) === 0xC0) {
}
if (($byte & 0xE0) === 0xC0) {
$rem = unpack('n~', $trans->receive(2));
return (($byte & 037) << 16 ) + $rem['~'];
} elseif (($byte & 0xF0) === 0xE0) {
}
if (($byte & 0xF0) === 0xE0) {
$rem = unpack('n~/C~~', $trans->receive(3));
return (($byte & 017) << 24 ) + ($rem['~'] << 8) + $rem['~~'];
} elseif (($byte & 0xF8) === 0xF0) {
}
if (($byte & 0xF8) === 0xF0) {
$rem = unpack('N~', $trans->receive(4));
return (($byte & 007) * 0x100000000/* '<< 32' or '2^32' */)
+ (double) sprintf('%u', $rem['~']);
Expand Down
3 changes: 2 additions & 1 deletion src/PEAR2/Net/RouterOS/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ protected function removeAllAttributes()
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
* You can still call it in earlier versions to get the object as a
* plain array.
*
* @return array The info, as an associative array.
*/
Expand Down
15 changes: 8 additions & 7 deletions src/PEAR2/Net/RouterOS/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ protected static function sanitizeOperator($operator)
{
$operator = (string) $operator;
switch ($operator) {
case Query::OP_EX:
case Query::OP_NEX:
case Query::OP_EQ:
case Query::OP_LT:
case Query::OP_GT:
case self::OP_EX:
case self::OP_NEX:
case self::OP_EQ:
case self::OP_LT:
case self::OP_GT:
return $operator;
default:
throw new UnexpectedValueException(
Expand Down Expand Up @@ -277,7 +277,8 @@ public function verify(Communicator $com)
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
* You can still call it in earlier versions to get the object as a
* plain array.
*
* @return array The info, as an associative array.
*/
Expand Down Expand Up @@ -307,7 +308,7 @@ protected function addWhere($name, $value, $operator)
$this->words[] = array(
static::sanitizeOperator($operator)
. Message::sanitizeAttributeName($name),
(null === $value ? null : Message::sanitizeAttributeValue($value))
null === $value ? null : Message::sanitizeAttributeValue($value)
);
return $this;
}
Expand Down
3 changes: 2 additions & 1 deletion src/PEAR2/Net/RouterOS/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ public function removeAllArguments()
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
* You can still call it in earlier versions to get the object as a
* plain array.
*
* @return array The info, as an associative array.
*/
Expand Down
9 changes: 5 additions & 4 deletions src/PEAR2/Net/RouterOS/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ private function _receive(
} while ('' === $type);
$this->setType($type);
if (null === $streamOn) {
for ($word = $com->getNextWord(); '' !== $word;
$word = $com->getNextWord()) {
for ($word = $com->getNextWord(); '' !== $word; $word = $com->
getNextWord()) {
if (preg_match('/^=([^=]+)=(.*)$/sS', $word, $matches)) {
$this->setAttribute($matches[1], $matches[2]);
} elseif (preg_match('/^\.tag=(.*)$/sS', $word, $matches)) {
Expand Down Expand Up @@ -332,7 +332,7 @@ public function getArgument($name)
*/
public function getProperty($name)
{
return parent::getAttribute($name);
return $this->getAttribute($name);
}

/**
Expand All @@ -351,7 +351,8 @@ public function getUnrecognizedWords()
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
* You can still call it in earlier versions to get the object as a
* plain array.
*
* @return array The info, as an associative array.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/PEAR2/Net/RouterOS/ResponseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ public function toArray($flags = self::ARRAY_DEFAULT)
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
* You can still call it in earlier versions to get the object as a
* plain array.
*
* @return array The info, as an associative array.
*/
Expand Down
1 change: 1 addition & 0 deletions src/PEAR2/Net/RouterOS/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ public static function escapeValue($value)
}
$value = '{' . substr($result, 1) . '}';
break;
/** @noinspection PhpMissingBreakStatementInspection */
case 'object':
if ($value instanceof DateTime) {
$usec = $value->format('u');
Expand Down
21 changes: 15 additions & 6 deletions src/PEAR2/Net/RouterOS/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function setMenu($newMenu)
$menuRequest = new Request('/menu');
if ('/' === $newMenu) {
$this->menu = '';
} elseif ('/' === $newMenu[0]) {
} elseif (strpos($newMenu, '/') === 0) {
$this->menu = $menuRequest->setCommand($newMenu)->getCommand();
} else {
$newMenu = (string)substr(
Expand Down Expand Up @@ -436,7 +436,9 @@ public function find()
if (null === $ret) {
$this->idCache = array();
return '';
} elseif (!is_string($ret)) {
}

if (!is_string($ret)) {
$ret = stream_get_contents($ret);
}

Expand All @@ -453,6 +455,7 @@ public function find()
$idList = '';
foreach (func_get_args() as $criteria) {
if ($criteria instanceof Query) {
/** @var Response $response */
foreach ($this->client->sendSync(
new Request($this->menu . '/print .proplist=.id', $criteria)
)->getAllOfType(Response::TYPE_DATA) as $response) {
Expand All @@ -466,6 +469,7 @@ public function find()
}
} elseif (is_callable($criteria)) {
$idCache = array();
/** @var Response $response */
foreach ($this->client->sendSync(
new Request($this->menu . '/print')
)->getAllOfType(Response::TYPE_DATA) as $response) {
Expand Down Expand Up @@ -528,6 +532,7 @@ public function find()
public function get($number, $valueName = null)
{
if ($number instanceof Query) {
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$number = explode(',', $this->find($number));
$number = $number[0];
if ('' === $number) {
Expand Down Expand Up @@ -1113,10 +1118,12 @@ public function filePutContents($filename, $data, $overwrite = false)
$fileSize = stream_get_contents($fileSize);
}
if (Communicator::isSeekableStream($data)) {
return Communicator::seekableStreamLength($data) == $fileSize;
} else {
return sprintf('%u', strlen((string)$data)) === $fileSize;
return (double)$fileSize === Communicator::seekableStreamLength(
$data
);
}

return sprintf('%u', strlen((string)$data)) === $fileSize;
}

/**
Expand Down Expand Up @@ -1157,7 +1164,9 @@ public function fileGetContents($filename, $tmpScriptName = null)
$responses
);
} catch (RouterErrorException $e) {
if ($e->getCode() !== RouterErrorException::CODE_SCRIPT_RUN_ERROR) {
if ($e->getCode() !== RouterErrorException::CODE_SCRIPT_RUN_ERROR
|| null === $e->getResponses()
) {
throw $e;
}
$message = $e->getResponses()->getAllOfType(Response::TYPE_ERROR)
Expand Down

0 comments on commit 10cc489

Please sign in to comment.