Passed
Push — master ( 8e0ee7...e38747 )
by Alexpts
01:34
created

BaseTransport::getWriter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\Transport;
5
6
abstract class BaseTransport implements TransportInterface
7
{
8
    /** @var resource|null|false */
9
    protected $target;
10
    /** @var string */
11
    protected $schema = '';
12
    /** @var WriterInterface */
13
    protected $writer;
14
15 16
    public function __construct(WriterInterface $writer = null)
16
    {
17 16
        $this->writer = $writer ?? new Writer;
18 16
    }
19
20 4
    public function getWriter(): WriterInterface
21
    {
22 4
        return $this->writer;
23
    }
24
25
    /**
26
     * @param string $buffer
27
     * @param int|null $length - записать число байт в сокет
28
     *
29
     * @return false|int число записанных байт или false
30
     */
31 16
    public function write(string $buffer, int $length = null): int
32
    {
33 16
        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\WriterInterface::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...
34
    }
35
36 12
    public function isConnected(): bool
37
    {
38 12
        return is_resource($this->target) && !feof($this->target);
39
    }
40
41 16
    public function close(): void
42
    {
43 16
        $this->target && fclose($this->target);
44 16
        $this->target = null;
45 16
    }
46
}
47