• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sineflow / clamav / 22581152157

02 Mar 2026 02:47PM UTC coverage: 94.574% (+18.5%) from 76.106%
22581152157

Pull #14

github

pmishev
Code style fix
Pull Request #14: 2.0.0

62 of 68 new or added lines in 11 files covered. (91.18%)

122 of 129 relevant lines covered (94.57%)

78.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/Socket/Socket.php
1
<?php
2

3
namespace Sineflow\ClamAV\Socket;
4

5
use Sineflow\ClamAV\Exception\SocketException;
6

7
class Socket
8
{
9
    public const UNIX = \AF_UNIX;
10
    public const NETWORK = \AF_INET;
11

12
    private const MAX_READ_BYTES = 8192;
13

14
    private ?\Socket $socket = null;
15

16
    public function __construct(
240✔
17
        private readonly int $socketType,
18
        private readonly array $connectionArguments,
19
        private readonly ?int $timeoutSeconds = null,
20
    ) {
21
    }
240✔
22

23
    public function sendCommand(string $dataIn, int $flagsSend = 0, int $flagsReceive = MSG_WAITALL): string
170✔
24
    {
25
        $this->connect();
170✔
26

27
        if (false === socket_send($this->socket, $dataIn, strlen($dataIn), $flagsSend)) {
170✔
28
            throw new SocketException('Writing to socket failed', socket_last_error($this->socket));
×
29
        }
30
        $dataOut = '';
170✔
31

32
        do {
33
            $bytes = socket_recv($this->socket, $chunk, self::MAX_READ_BYTES, $flagsReceive);
170✔
34
            if (false === $bytes) {
170✔
35
                $socketError = socket_last_error($this->socket);
10✔
36
                socket_close($this->socket);
10✔
37
                $this->socket = null;
10✔
38
                throw new SocketException('Reading from socket failed', $socketError);
10✔
39
            }
40
            $dataOut .= $chunk;
160✔
41
        } while ($bytes);
160✔
42
        socket_close($this->socket);
160✔
43
        $this->socket = null;
160✔
44

45
        return $dataOut;
160✔
46
    }
47

48
    /**
49
     * @throws SocketException
50
     */
51
    private function connect(): void
170✔
52
    {
53
        if ($this->socket instanceof \Socket) {
170✔
NEW
54
            return;
×
55
        }
56

57
        $socket = @ socket_create($this->socketType, SOCK_STREAM, 0);
170✔
58
        if ($socket === false) {
170✔
NEW
59
            throw new SocketException('Creating socket failed', socket_last_error());
×
60
        }
61
        $this->socket = $socket;
170✔
62

63
        $success = @ socket_connect($this->socket, ...$this->connectionArguments);
170✔
64
        if ($success === false) {
170✔
NEW
65
            $errorCode = socket_last_error($this->socket);
×
NEW
66
            socket_close($this->socket);
×
NEW
67
            $this->socket = null;
×
NEW
68
            throw new SocketException('Connecting to socket failed', $errorCode);
×
69
        }
70

71
        if ($this->timeoutSeconds !== null) {
170✔
72
            $timeout = ['sec' => $this->timeoutSeconds, 'usec' => 0];
10✔
73
            socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, $timeout);
10✔
74
            socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, $timeout);
10✔
75
        }
76
    }
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc