diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8141620..c02d785 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -52,5 +52,4 @@ jobs: run: docker run --entrypoint php -p 9501:9501 -d swoole:latest examples/http_server.php - name: Run Test Cases run: | - composer analyse composer test diff --git a/src/Channel.php b/src/Channel.php index 6c8dbf6..54272e6 100644 --- a/src/Channel.php +++ b/src/Channel.php @@ -14,61 +14,129 @@ use Hyperf\Engine\Contract\ChannelInterface; use Hyperf\Engine\Exception\RuntimeException; -class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface -{ - /** - * @var bool - */ - protected $closed = false; - - public function getCapacity() +if (PHP_VERSION_ID > 80000 && SWOOLE_VERSION_ID >= 40900) { + class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface { - return $this->capacity; - } + protected bool $closed = false; - public function getLength() - { - return $this->length(); - } + public function push(mixed $data, float $timeout = -1): bool + { + return parent::push($data, $timeout); + } - public function isAvailable() - { - return ! $this->isClosing(); - } + public function pop(float $timeout = -1): mixed + { + return parent::pop($timeout); + } - public function close() - { - $this->closed = true; - parent::close(); - } + public function getCapacity(): int + { + return $this->capacity; + } - public function hasProducers() - { - throw new RuntimeException('Not supported.'); - } + public function getLength(): int + { + return $this->length(); + } - public function hasConsumers() - { - throw new RuntimeException('Not supported.'); - } + public function isAvailable(): bool + { + return ! $this->isClosing(); + } - public function isReadable() - { - throw new RuntimeException('Not supported.'); - } + public function close(): bool + { + $this->closed = true; + return parent::close(); + } - public function isWritable() - { - throw new RuntimeException('Not supported.'); - } + public function hasProducers(): bool + { + throw new RuntimeException('Not supported.'); + } - public function isClosing() - { - return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED; - } + public function hasConsumers(): bool + { + throw new RuntimeException('Not supported.'); + } + + public function isReadable(): bool + { + throw new RuntimeException('Not supported.'); + } + + public function isWritable(): bool + { + throw new RuntimeException('Not supported.'); + } + + public function isClosing(): bool + { + return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED; + } - public function isTimeout() + public function isTimeout(): bool + { + return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT; + } + } +} else { + class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface { - return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT; + /** + * @var bool + */ + protected $closed = false; + + public function getCapacity(): int + { + return $this->capacity; + } + + public function getLength(): int + { + return $this->length(); + } + + public function isAvailable(): bool + { + return ! $this->isClosing(); + } + + public function close(): bool + { + $this->closed = true; + return parent::close(); + } + + public function hasProducers(): bool + { + throw new RuntimeException('Not supported.'); + } + + public function hasConsumers(): bool + { + throw new RuntimeException('Not supported.'); + } + + public function isReadable(): bool + { + throw new RuntimeException('Not supported.'); + } + + public function isWritable(): bool + { + throw new RuntimeException('Not supported.'); + } + + public function isClosing(): bool + { + return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED; + } + + public function isTimeout(): bool + { + return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT; + } } } diff --git a/src/Contract/ChannelInterface.php b/src/Contract/ChannelInterface.php index a9997cf..d8e6d18 100644 --- a/src/Contract/ChannelInterface.php +++ b/src/Contract/ChannelInterface.php @@ -11,80 +11,124 @@ */ namespace Hyperf\Engine\Contract; -interface ChannelInterface -{ - /** - * @param mixed $data [required] - * @param float|int $timeout [optional] = -1 - * @return bool - */ - public function push($data, $timeout = -1); - - /** - * @param float $timeout seconds [optional] = -1 - * @return mixed when pop failed, return false - */ - public function pop($timeout = -1); - - /** - * Swow: When the channel is closed, all the data in it will be destroyed. - * Swoole: When the channel is closed, the data in it can still be popped out, but push behavior will no longer succeed. - * @return mixed - */ - public function close(); - - /** - * @return int - */ - public function getCapacity(); - - /** - * @return int - */ - public function getLength(); - - /** - * @return bool - */ - public function isAvailable(); - - /** - * @return bool - */ - public function hasProducers(); - - /** - * @return bool - */ - public function hasConsumers(); - - /** - * @return bool - */ - public function isEmpty(); - - /** - * @return bool - */ - public function isFull(); - - /** - * @return bool - */ - public function isReadable(); - - /** - * @return bool - */ - public function isWritable(); - - /** - * @return bool - */ - public function isClosing(); - - /** - * @return bool - */ - public function isTimeout(); +if (PHP_VERSION_ID > 80000 && SWOOLE_VERSION_ID >= 40900) { + interface ChannelInterface + { + /** + * @param float|int $timeout [optional] = -1 + */ + public function push(mixed $data, float $timeout = -1): bool; + + /** + * @param float $timeout seconds [optional] = -1 + * @return mixed when pop failed, return false + */ + public function pop(float $timeout = -1): mixed; + + /** + * Swow: When the channel is closed, all the data in it will be destroyed. + * Swoole: When the channel is closed, the data in it can still be popped out, but push behavior will no longer succeed. + */ + public function close(): bool; + + public function getCapacity(): int; + + public function getLength(): int; + + public function isAvailable(): bool; + + public function hasProducers(): bool; + + public function hasConsumers(): bool; + + public function isEmpty(): bool; + + public function isFull(): bool; + + public function isReadable(): bool; + + public function isWritable(): bool; + + public function isClosing(): bool; + + public function isTimeout(): bool; + } +} else { + interface ChannelInterface + { + /** + * @param mixed $data [required] + * @param float|int $timeout [optional] = -1 + * @return bool + */ + public function push($data, $timeout = -1); + + /** + * @param float $timeout seconds [optional] = -1 + * @return mixed when pop failed, return false + */ + public function pop($timeout = -1); + + /** + * Swow: When the channel is closed, all the data in it will be destroyed. + * Swoole: When the channel is closed, the data in it can still be popped out, but push behavior will no longer succeed. + * @return mixed + */ + public function close(): bool; + + /** + * @return int + */ + public function getCapacity(); + + /** + * @return int + */ + public function getLength(); + + /** + * @return bool + */ + public function isAvailable(); + + /** + * @return bool + */ + public function hasProducers(); + + /** + * @return bool + */ + public function hasConsumers(); + + /** + * @return bool + */ + public function isEmpty(); + + /** + * @return bool + */ + public function isFull(); + + /** + * @return bool + */ + public function isReadable(); + + /** + * @return bool + */ + public function isWritable(); + + /** + * @return bool + */ + public function isClosing(); + + /** + * @return bool + */ + public function isTimeout(); + } } diff --git a/src/Contract/Http/ClientInterface.php b/src/Contract/Http/ClientInterface.php index 5eb6288..eeb69cd 100644 --- a/src/Contract/Http/ClientInterface.php +++ b/src/Contract/Http/ClientInterface.php @@ -15,10 +15,7 @@ interface ClientInterface { - /** - * @return $this - */ - public function set(array $settings); + public function set(array $settings): bool; /** * @param string[][] $headers diff --git a/src/Http/Client.php b/src/Http/Client.php index f22580b..cc1a4fd 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -17,10 +17,9 @@ class Client extends HttpClient implements ClientInterface { - public function set(array $settings) + public function set(array $settings): bool { - parent::set($settings); - return $this; + return parent::set($settings); } /** diff --git a/tests/Cases/ChannelTest.php b/tests/Cases/ChannelTest.php index 4912dae..73dd877 100644 --- a/tests/Cases/ChannelTest.php +++ b/tests/Cases/ChannelTest.php @@ -158,7 +158,7 @@ public function testChannelIsClosing() $this->assertFalse($channel->isClosing()); $this->assertTrue($channel->isTimeout()); $this->assertTrue($channel->isAvailable()); - $this->assertNull($channel->close()); + $this->assertTrue($channel->close()); $this->assertTrue($channel->isClosing()); $this->assertFalse($channel->isTimeout()); $this->assertFalse($channel->isAvailable());