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

Writer::write()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 10
cp 0.9
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3.009
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