TypedCDataArray::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of the sj-i/typed-cdata 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 TypedCData;
15
16
use FFI\CData;
17
18
/**
19
 * @template T of TypedCDataInterface
20
 */
21
class TypedCDataArray implements \ArrayAccess
22
{
23
    /** @var \FFI\CDataArray */
24
    private CData $cdata;
25
    /** @var class-string<T> $typed_cdata_class */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
26
    private string $typed_cdata_class;
27
    /** @var array<int, T> */
28
    private array $typed_element_cache;
29
30
    /**
31
     * TypedCDataArray constructor.
32
     * @param \FFI\CDataArray $cdata
33
     * @param class-string<T> $typed_cdata_class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
34
     */
35
    public function __construct(CData $cdata, string $typed_cdata_class)
36
    {
37
        $this->cdata = $cdata;
0 ignored issues
show
Documentation Bug introduced by
$cdata is of type FFI\CData, but the property $cdata was declared to be of type FFI\CDataArray. 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...
38
        $this->typed_cdata_class = $typed_cdata_class;
39
        $this->typed_element_cache = [];
40
    }
41
42
    public function offsetExists($offset)
43
    {
44
        return true;
45
    }
46
47
    /**
48
     * @param int $offset
49
     */
50
    public function offsetGet($offset): TypedCDataInterface
51
    {
52
        if (!isset($this->typed_element_cache[$offset])) {
53
            $this->typed_element_cache[$offset] = $this->typed_cdata_class::fromCData($this->cdata[$offset]);
54
        }
55
        return $this->typed_element_cache[$offset];
56
    }
57
58
    /**
59
     * @param int $offset
60
     * @param TypedCDataInterface $value
61
     */
62
    public function offsetSet($offset, $value)
63
    {
64
        $value->toCData($this->cdata[$offset]);
65
    }
66
67
    public function offsetUnset($offset)
68
    {
69
    }
70
71
    /**
72
     * @template T2 of TypedCDataInterface
73
     * @param \FFI\CDataArray $cdata
74
     * @param class-string<T2> $typed_cdata_class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T2> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T2>.
Loading history...
75
     * @return self<T2>
76
     */
77
    public static function fromCData(CData $cdata, string $typed_cdata_class): TypedCDataArray
78
    {
79
        return new self($cdata, $typed_cdata_class);
80
    }
81
82
    public function toCData(?CData $cdata): CData
0 ignored issues
show
Unused Code introduced by
The parameter $cdata is not used and could be removed. ( Ignorable by Annotation )

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

82
    public function toCData(/** @scrutinizer ignore-unused */ ?CData $cdata): CData

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
    {
84
        foreach ($this->typed_element_cache as $offset => $item) {
85
            assert($item instanceof TypedCDataInterface);
86
            $item->toCData($this->cdata[$offset]);
87
        }
88
        return $this->cdata;
89
    }
90
}
91