Test Failed
Pull Request — master (#12)
by wujunze
03:09
created

SocketIO   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 35
c 1
b 0
f 1
dl 0
loc 91
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 3 1
A send() 0 12 4
A close() 0 3 1
A recv() 0 4 1
B createConnection() 0 23 7
A reconnect() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Seasx\SeasLogger\Kafka\Socket;
5
6
use Co;
7
use Co\Socket;
8
use Exception;
9
10
/**
11
 * Class SocketIO
12
 * @package Seasx\SeasLogger\Kafka\Socket
13
 */
14
class SocketIO
15
{
16
    /** @var bool */
17
    private $recv = false;
18
    /** @var Socket */
19
    private $connection;
20
    /** @var array */
21
    private $config = [];
22
23
    /**
24
     * @param string $data
25
     * @param float $timeout
26
     * @return int
27
     * @throws Exception
28
     */
29
    public function send(string $data, float $timeout = -1): int
30
    {
31
        $ln = strlen($data);
32
        while ($data && $ln > 0) {
33
            $result = $this->connection->sendAll($data, $timeout);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->connection->sendAll($data, $timeout) targeting Swoole\Coroutine\Socket::sendAll() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34
            if ($result === false) {
35
                $this->reconnect();
36
            }
37
            $data = substr($data, $result);
38
        }
39
        $this->recv = false;
40
        return $ln;
41
    }
42
43
    /**
44
     * @throws Exception
45
     */
46
    public function reconnect(): void
47
    {
48
        $this->createConnection();
49
    }
50
51
    /**
52
     * @param array $config
53
     * @throws Exception
54
     */
55
    public function createConnection(array $config = []): void
56
    {
57
        !empty($config) && ($this->config = $config);
58
        $client = new Socket(AF_INET, SOCK_STREAM, 0);
59
        list($host, $port) = explode(':', $this->config['uri']);
60
        $maxRetry = $this->config['retry'];
61
        $reconnectCount = 0;
62
        while (true) {
63
            $isConnect = $this->config['timeout'] ? $client->connect($host, (int)$port,
0 ignored issues
show
Bug introduced by
Are you sure the usage of $client->connect($host, ...his->config['timeout']) targeting Swoole\Coroutine\Socket::connect() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
64
                $this->config['timeout']) : $client->connect($host, (int)$port);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $client->connect($host, (int)$port) targeting Swoole\Coroutine\Socket::connect() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
            if (!$isConnect) {
66
                $reconnectCount++;
67
                if ($maxRetry > 0 && $reconnectCount >= $maxRetry) {
68
                    $error = sprintf('Service connect fail error=%s host=%s port=%s', socket_strerror($client->errCode),
69
                        $host, $port);
70
                    throw new Exception($error);
71
                }
72
                Co::sleep($this->config['sleep']);
73
            } else {
74
                break;
75
            }
76
        }
77
        $this->connection = $client;
78
    }
79
80
    /**
81
     * @param int $length
82
     * @param float $timeout
83
     * @return string
84
     */
85
    public function recv(int $length = 65535, float $timeout = -1): string
86
    {
87
        $data = $this->connection->recvAll($length, $timeout);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $this->connection->recvAll($length, $timeout) targeting Swoole\Coroutine\Socket::recvAll() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
        return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data returns the type null which is incompatible with the type-hinted return string.
Loading history...
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function check(): bool
95
    {
96
        return $this->connection->errCode === 0;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    public function close(): bool
103
    {
104
        return $this->connection->close();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->connection->close() targeting Swoole\Coroutine\Socket::close() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->connection->close() returns the type null which is incompatible with the type-hinted return boolean.
Loading history...
105
    }
106
}