Passed
Push — master ( dde472...6978f3 )
by Alexpts
01:57
created

BaseTransport::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\Transport;
5
6
abstract class BaseTransport implements TransportInterface
7
{
8
9
    /**
10
     * @var resource|null|false
11
     */
12
    protected $target;
13
14
    /** @var string - udp:// tcp:// */
15
    protected $schema = '';
16
17
    protected $errorNumber = 0;
18
    protected $errorMessage = '';
19
20
    /** @var Writer */
21
    protected $writer;
22
23 3
    public function __construct(Writer $writer = null)
24
    {
25 3
        $this->writer = $writer ?? new Writer;
26 3
    }
27
28
    /**
29
     * @param string $buffer
30
     * @param int|null $length - записать число байт в сокет
31
     *
32
     * @return false|int число записанных байт или false
33
     */
34 3
    public function write(string $buffer, int $length = null): int
35
    {
36 3
        return $this->writer->write($this->target, $buffer, $length);
0 ignored issues
show
Bug introduced by
It seems like $this->target can also be of type false or null; however, PTS\Transport\Writer::write() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
37
    }
38
39 3
    public function isConnected(): bool
40
    {
41 3
        return is_resource($this->target) && !feof($this->target);
42
    }
43
44 3
    public function close(): void
45
    {
46 3
        $this->target && fclose($this->target);
47 3
        $this->target = null;
48 3
    }
49
}
50