Passed
Pull Request — master (#7)
by Shinji
12:52
created

CDataByteReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
final class CDataByteReader implements ByteReaderInterface
20
{
21
    use ByteReaderDisableWriteAccessTrait;
22
23
    /** @var CArray */
24
    private CData $source;
25
26
    /**
27
     * CDataByteReader constructor.
28
     * @param CArray $source
29
     */
30
    public function __construct(CData $source)
31
    {
32
        $this->source = $source;
0 ignored issues
show
Documentation Bug introduced by
$source is of type FFI\CData, but the property $source was declared to be of type FFI\CArray. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
33
    }
34
35
    public function offsetExists($offset): bool
36
    {
37
        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 $var 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

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