Optimized code.

This commit is contained in:
李铭昕 2019-11-06 11:22:10 +08:00
parent 17359ce647
commit 911d2f0341
5 changed files with 13 additions and 23 deletions

View File

@ -38,6 +38,6 @@ before_script:
- composer config -g process-timeout 900 && composer update
script:
- composer analyse src/di src/json-rpc src/tracer src/metric
- composer analyse src/di src/json-rpc src/tracer src/metric src/nats
- composer test -- --exclude-group NonCoroutine
- vendor/bin/phpunit --group NonCoroutine

View File

@ -204,7 +204,7 @@ class Connection
if ($this->isConnected() === true) {
if (is_numeric($seconds) === true) {
try {
$timeout = number_format($seconds, 3);
$timeout = (float) number_format($seconds, 3);
$seconds = floor($timeout);
$microseconds = (($timeout - $seconds) * 1000);
return stream_set_timeout($this->streamSocket, $seconds, $microseconds);
@ -252,7 +252,7 @@ class Connection
*
* @param float $timeout number of seconds until the connect() system call should timeout
*
* @throws \Exception exception raised if connection fails
* @throws \Throwable exception raised if connection fails
*/
public function connect($timeout = null)
{
@ -390,7 +390,7 @@ class Connection
*
* @param int $quantity number of messages to wait for
*
* @return Connection $connection Connection object
* @return null|Connection $connection Connection object
*/
public function wait($quantity = 0)
{
@ -478,7 +478,7 @@ class Connection
throw Exception::forStreamSocketClientError($errstr, $errno);
}
$timeout = number_format($timeout, 3);
$timeout = (float) number_format($timeout, 3);
$seconds = floor($timeout);
$microseconds = (($timeout - $seconds) * 1000);
stream_set_timeout($fp, $seconds, $microseconds);
@ -537,7 +537,7 @@ class Connection
*
* @return string
*/
private function receive($len = 0)
private function receive(int $len = 0)
{
if ($len > 0) {
$chunkSize = $this->chunkSize;
@ -595,7 +595,7 @@ class Connection
$subject = $parts[1];
}
$payload = $this->receive($length);
$payload = $this->receive((int) $length);
$msg = new Message($subject, $payload, $sid, $this);
if (isset($this->subscriptions[$sid]) === false) {

View File

@ -57,7 +57,7 @@ class NatsDriver extends AbstractDriver
$client = $connection->getConnection();
$client->publish($subject, $payload, $inbox);
} finally {
$connection->release();
$connection && $connection->release();
}
}
@ -70,7 +70,7 @@ class NatsDriver extends AbstractDriver
$client = $connection->getConnection();
$client->request($subject, $payload, $callback);
} finally {
$connection->release();
$connection && $connection->release();
}
}
@ -92,7 +92,7 @@ class NatsDriver extends AbstractDriver
}
return $message;
} finally {
$connection->release();
$connection && $connection->release();
}
}
@ -110,7 +110,7 @@ class NatsDriver extends AbstractDriver
}
$client->wait();
} finally {
$connection->release();
$connection && $connection->release();
}
}
}

View File

@ -22,7 +22,7 @@ class EncodedConnection extends Connection
/**
* Encoder for this connection.
*
* @var null|\Nats\Encoders\Encoder
* @var null|Encoder
*/
private $encoder;
@ -30,7 +30,7 @@ class EncodedConnection extends Connection
* EncodedConnection constructor.
*
* @param ConnectionOptions $options connection options object
* @param null|\Nats\Encoders\Encoder $encoder encoder to use with the payload
* @param null|Encoder $encoder encoder to use with the payload
*/
public function __construct(ConnectionOptions $options = null, Encoder $encoder = null)
{

View File

@ -21,8 +21,6 @@ class Exception extends \Exception
* Creates an Exception for a failed connection.
*
* @param string $response the failed error response
*
* @return \Nats\Exception
*/
public static function forFailedConnection($response)
{
@ -33,8 +31,6 @@ class Exception extends \Exception
* Creates an Exception for a failed PING response.
*
* @param string $response the failed PING response
*
* @return \Nats\Exception
*/
public static function forFailedPing($response)
{
@ -45,8 +41,6 @@ class Exception extends \Exception
* Creates an Exception for an invalid Subscription Identifier (sid).
*
* @param string $subscription the Subscription Identifier (sid)
*
* @return \Nats\Exception
*/
public static function forSubscriptionNotFound($subscription)
{
@ -57,8 +51,6 @@ class Exception extends \Exception
* Creates an Exception for an invalid Subscription Identifier (sid) callback.
*
* @param string $subscription the Subscription Identifier (sid)
*
* @return \Nats\Exception
*/
public static function forSubscriptionCallbackInvalid($subscription)
{
@ -70,8 +62,6 @@ class Exception extends \Exception
*
* @param string $message the system level error message
* @param int $code the system level error code
*
* @return \Nats\Exception
*/
public static function forStreamSocketClientError($message, $code)
{