Skip to content

Commit

Permalink
Require at least PHP 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
binsoul committed Dec 7, 2019
1 parent e4ff7ef commit d97871c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
}
],
"require": {
"php": "~5.6|~7.0",
"binsoul/net-mqtt": "^0.4",
"php": "^7.0",
"binsoul/net-mqtt": "^0.5",
"react/promise": "^2.7",
"react/socket": "^1.3"
},
Expand Down
22 changes: 12 additions & 10 deletions src/ReactFlow.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace BinSoul\Net\Mqtt\Client\React;

use BinSoul\Net\Mqtt\Flow;
Expand All @@ -15,7 +17,7 @@ class ReactFlow implements Flow
private $decorated;
/** @var Deferred */
private $deferred;
/** @var Packet */
/** @var Packet|null */
private $packet;
/** @var bool */
private $isSilent;
Expand All @@ -28,15 +30,15 @@ class ReactFlow implements Flow
* @param Packet $packet
* @param bool $isSilent
*/
public function __construct(Flow $decorated, Deferred $deferred, Packet $packet = null, $isSilent = false)
public function __construct(Flow $decorated, Deferred $deferred, Packet $packet = null, bool $isSilent = false)
{
$this->decorated = $decorated;
$this->deferred = $deferred;
$this->packet = $packet;
$this->isSilent = $isSilent;
}

public function getCode()
public function getCode(): string
{
return $this->decorated->getCode();
}
Expand All @@ -48,7 +50,7 @@ public function start()
return $this->packet;
}

public function accept(Packet $packet)
public function accept(Packet $packet): bool
{
return $this->decorated->accept($packet);
}
Expand All @@ -60,12 +62,12 @@ public function next(Packet $packet)
return $this->packet;
}

public function isFinished()
public function isFinished(): bool
{
return $this->decorated->isFinished();
}

public function isSuccess()
public function isSuccess(): bool
{
return $this->decorated->isSuccess();
}
Expand All @@ -75,7 +77,7 @@ public function getResult()
return $this->decorated->getResult();
}

public function getErrorMessage()
public function getErrorMessage(): string
{
return $this->decorated->getErrorMessage();
}
Expand All @@ -85,15 +87,15 @@ public function getErrorMessage()
*
* @return Deferred
*/
public function getDeferred()
public function getDeferred(): Deferred
{
return $this->deferred;
}

/**
* Returns the current packet.
*
* @return Packet
* @return Packet|null
*/
public function getPacket()
{
Expand All @@ -105,7 +107,7 @@ public function getPacket()
*
* @return bool
*/
public function isSilent()
public function isSilent(): bool
{
return $this->isSilent;
}
Expand Down
43 changes: 23 additions & 20 deletions src/ReactMqttClient.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace BinSoul\Net\Mqtt\Client\React;

use BinSoul\Net\Mqtt\ClientIdentifierGenerator;
Expand Down Expand Up @@ -128,7 +130,7 @@ public function __construct(
*
* @return string
*/
public function getHost()
public function getHost(): string
{
return $this->host;
}
Expand All @@ -138,7 +140,7 @@ public function getHost()
*
* @return int
*/
public function getPort()
public function getPort(): int
{
return $this->port;
}
Expand All @@ -148,7 +150,7 @@ public function getPort()
*
* @return bool
*/
public function isConnected()
public function isConnected(): bool
{
return $this->isConnected;
}
Expand All @@ -173,7 +175,7 @@ public function getStream()
*
* @return ExtendedPromiseInterface
*/
public function connect($host, $port = 1883, Connection $connection = null, $timeout = 5)
public function connect(string $host, int $port = 1883, Connection $connection = null, int $timeout = 5): ExtendedPromiseInterface
{
if ($this->isConnected || $this->isConnecting) {
return new RejectedPromise(new \LogicException('The client is already connected.'));
Expand Down Expand Up @@ -240,7 +242,7 @@ public function connect($host, $port = 1883, Connection $connection = null, $tim
*
* @return ExtendedPromiseInterface
*/
public function disconnect($timeout = 5)
public function disconnect(int $timeout = 5): ExtendedPromiseInterface
{
if (!$this->isConnected || $this->isDisconnecting) {
return new RejectedPromise(new \LogicException('The client is not connected.'));
Expand All @@ -251,6 +253,7 @@ public function disconnect($timeout = 5)
$deferred = new Deferred();

$isResolved = false;
/** @var mixed $flowResult */
$flowResult = null;

$this->onCloseCallback = function ($connection) use ($deferred, &$isResolved, &$flowResult) {
Expand Down Expand Up @@ -296,7 +299,7 @@ function () {
*
* @return ExtendedPromiseInterface
*/
public function subscribe(Subscription $subscription)
public function subscribe(Subscription $subscription): ExtendedPromiseInterface
{
if (!$this->isConnected) {
return new RejectedPromise(new \LogicException('The client is not connected.'));
Expand All @@ -312,7 +315,7 @@ public function subscribe(Subscription $subscription)
*
* @return ExtendedPromiseInterface
*/
public function unsubscribe(Subscription $subscription)
public function unsubscribe(Subscription $subscription): ExtendedPromiseInterface
{
if (!$this->isConnected) {
return new RejectedPromise(new \LogicException('The client is not connected.'));
Expand All @@ -328,7 +331,7 @@ public function unsubscribe(Subscription $subscription)
*
* @return ExtendedPromiseInterface
*/
public function publish(Message $message)
public function publish(Message $message): ExtendedPromiseInterface
{
if (!$this->isConnected) {
return new RejectedPromise(new \LogicException('The client is not connected.'));
Expand All @@ -346,7 +349,7 @@ public function publish(Message $message)
*
* @return ExtendedPromiseInterface
*/
public function publishPeriodically($interval, Message $message, callable $generator)
public function publishPeriodically(int $interval, Message $message, callable $generator): ExtendedPromiseInterface
{
if (!$this->isConnected) {
return new RejectedPromise(new \LogicException('The client is not connected.'));
Expand All @@ -357,11 +360,11 @@ public function publishPeriodically($interval, Message $message, callable $gener
$this->timer[] = $this->loop->addPeriodicTimer(
$interval,
function () use ($message, $generator, $deferred) {
$this->publish($message->withPayload($generator($message->getTopic())))->then(
function ($value) use ($deferred) {
$this->publish($message->withPayload((string) $generator($message->getTopic())))->then(
static function ($value) use ($deferred) {
$deferred->notify($value);
},
function (\Exception $e) use ($deferred) {
static function (\Exception $e) use ($deferred) {
$deferred->reject($e);
}
);
Expand Down Expand Up @@ -404,13 +407,13 @@ private function emitError(\Exception $e)
*
* @return ExtendedPromiseInterface
*/
private function establishConnection($host, $port, $timeout)
private function establishConnection(string $host, int $port, int $timeout): ExtendedPromiseInterface
{
$deferred = new Deferred();

$timer = $this->loop->addTimer(
$timeout,
function () use ($deferred, $timeout) {
static function () use ($deferred, $timeout) {
$exception = new \RuntimeException(sprintf('Connection timed out after %d seconds.', $timeout));
$deferred->reject($exception);
}
Expand All @@ -435,7 +438,7 @@ function () use ($deferred, $timeout) {

$deferred->resolve($stream);
})
->otherwise(function (\Exception $e) use ($deferred) {
->otherwise(static function (\Exception $e) use ($deferred) {
$deferred->reject($e);
});

Expand All @@ -450,13 +453,13 @@ function () use ($deferred, $timeout) {
*
* @return ExtendedPromiseInterface
*/
private function registerClient(Connection $connection, $timeout)
private function registerClient(Connection $connection, int $timeout): ExtendedPromiseInterface
{
$deferred = new Deferred();

$responseTimer = $this->loop->addTimer(
$timeout,
function () use ($deferred, $timeout) {
static function () use ($deferred, $timeout) {
$exception = new \RuntimeException(sprintf('No response after %d seconds.', $timeout));
$deferred->reject($exception);
}
Expand All @@ -474,7 +477,7 @@ function () {
);

$deferred->resolve($result ?: $connection);
})->otherwise(function (\Exception $e) use ($deferred) {
})->otherwise(static function (\Exception $e) use ($deferred) {
$deferred->reject($e);
});

Expand All @@ -488,7 +491,7 @@ function () {
*
* @return void
*/
private function handleReceive($data)
private function handleReceive(string $data)
{
if (!$this->isConnected && !$this->isConnecting) {
return;
Expand Down Expand Up @@ -644,7 +647,7 @@ private function handleError(\Exception $e)
*
* @return ExtendedPromiseInterface
*/
private function startFlow(Flow $flow, $isSilent = false)
private function startFlow(Flow $flow, bool $isSilent = false): ExtendedPromiseInterface
{
try {
$packet = $flow->start();
Expand Down

0 comments on commit d97871c

Please sign in to comment.