CDataByteReader   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 30
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createSliceAsString() 0 7 2
A offsetGet() 0 3 1
A offsetExists() 0 6 2
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Lib\ByteStream;
15
16
use FFI\CArray;
17
use FFI\CData;
18
19
use function chr;
20
use function count;
21
use function is_null;
22
23
final class CDataByteReader implements ByteReaderInterface
24
{
25
    use ByteReaderDisableWriteAccessTrait;
26
27
    /** @param CArray<int> $source */
28
    public function __construct(
29
        private CData $source
30
    ) {
31
    }
32
33
    public function offsetExists($offset): bool
34
    {
35
        if (count($this->source) <= $offset) {
0 ignored issues
show
Bug introduced by
$this->source of type FFI\CData is incompatible with the type Countable|array expected by parameter $value of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        if (count(/** @scrutinizer ignore-type */ $this->source) <= $offset) {
Loading history...
36
            return false;
37
        }
38
        return !is_null($this->source[$offset]);
39
    }
40
41
    public function offsetGet($offset): int
42
    {
43
        return $this->source[$offset];
44
    }
45
46
    public function createSliceAsString(int $offset, int $size): string
47
    {
48
        $result = '';
49
        for ($i = $offset, $last_offset = $offset + $size; $i < $last_offset; $i++) {
50
            $result .= chr($this->source[$i]);
51
        }
52
        return $result;
53
    }
54
}
55