Passed
Push — master ( ef59e7...9fe26e )
by Alexpts
01:49
created

Writer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 29
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A write() 0 18 3
1
<?php
2
3
namespace PTS\Transport;
4
5
class Writer
6
{
7
8
    /**
9
     * @param resource $target
10
     * @param string $buffer
11
     * @param int|null $length - записать число байт в сокет
12
     *
13
     * @return false|int число записанных байт или false
14
     */
15 3
    public function write($target, string $buffer, int $length = null): int
16
    {
17 3
        $length = $length ?? mb_strlen($buffer, '8bit');
18
19 3
        $written = 0;
20 3
        while ($written < $length) {
21 2
            $chunk = mb_substr($buffer, $written, $length, '8bit');
22 2
            $byteCount = fwrite($target, $chunk, $length);
23
24 2
            if (!$byteCount) {
25
                break;
26
            }
27
28 2
            $written += $byteCount;
29
        }
30
31 3
        return $written;
32
    }
33
}
34