Completed
Push — master ( 79f24a...4b5497 )
by Emily
02:16
created

FixedList::resizeToFull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Collection\ListCollection;
16
17
use IteratorIterator;
18
use SplFixedArray;
19
20
/**
21
 * Represents an List stored in a PHP array
22
 */
23
class FixedList extends AbstractList
24
{
25
    /**
26
     * @var ValueType[]
27
     */
28
    protected $data;
29
30
    /**
31
     * @var int
32
     */
33
    protected $pointer = 0;
34
35 37
    public function __construct(int $size = 0)
36
    {
37 37
        $this->data = new SplFixedArray($size);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SplFixedArray($size) of type object<SplFixedArray> is incompatible with the declared type array<integer,object<Spa...tCollection\ValueType>> of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38 37
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 34
    public function add($item)
44
    {
45 34
        $this->data[$this->pointer++] = $item;
46 34
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51 10
    public function get(int $index)
52
    {
53 10
        return $this->data[$index];
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function splice
60
    (
61
        int $offset,
62
        ?int $length = null,
63
        array $replacement = []
64
    )
65
    {
66
        $end = $offset + $length;
67
68
        for ($i = $offset, $j = 0; $i < $end; $i++, $j++)
69
        {
70
            $this->data[$i] = $replacement[$j] ?? null;
71
        }
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 2
    public function set(int $index, $value)
78
    {
79 2
        $this->data[$index] = $value;
80 2
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 1
    public function remove(int $item)
86
    {
87 1
        $this->set($item, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Spaark\CompositeU...stCollection\ValueType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88 1
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93 17
    public function getIterator()
94
    {
95 17
        return new IteratorIterator($this->data);
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101 6
    public function size() : int
102
    {
103 6
        return count($this->data);
104
    }
105
106
    /**
107
     * Resizes the FixedList, throwing away any unused elements
108
     *
109
     * @param int $size The new size
110
     */
111 31
    public function resize(int $size)
112
    {
113 31
        $this->data->setSize($size);
0 ignored issues
show
Bug introduced by
The method setSize cannot be called on $this->data (of type array<integer,object<Spa...tCollection\ValueType>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
114 31
    }
115
116
    /**
117
     * Returns the current pointer position
118
     *
119
     * @return int
120
     */
121 30
    public function getCurrentPosition() : int
122
    {
123 30
        return $this->pointer;
124
    }
125
126
    /**
127
     * Resizes to the current pointer
128
     */
129 30
    public function resizeToFull()
130
    {
131 30
        $this->resize($this->getCurrentPosition());
132 30
    }
133
}
134
135