Passed
Push — 1.x ( 56076d...c7c61a )
by Ulises Jeremias
02:29
created

Queue::__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 namespace Mbh\Collection;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
12
use Traversable;
13
use ArrayAccess;
14
use IteratorAggregate;
15
use Error;
16
use OutOfBoundsException;
17
18
/**
19
 * A “first in, first out” or “FIFO” collection that only allows access to the
20
 * value at the front of the queue and iterates in that order, destructively.
21
 *
22
 * @package structures
23
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
24
 */
25
26
class Queue implements ArrayAccess, CollectionInterface, IteratorAggregate
27
{
28
    use Traits\Collection;
29
30
    const MIN_CAPACITY = 8;
31
32
    /**
33
     * @var Deque internal deque to store values.
34
     */
35
    private $deque;
36
37
    /**
38
     * Creates an instance using the values of an array or Traversable object.
39
     *
40
     * @param array|Traversable $values
41
     */
42
    public function __construct($values = null)
43
    {
44
        $this->deque = new Deque($values ?? []);
0 ignored issues
show
Bug introduced by
It seems like $values ?? array() can also be of type array and array; however, parameter $array of Mbh\Collection\Deque::__construct() does only seem to accept Traversable, maybe add an additional type check? ( Ignorable by Annotation )

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

44
        $this->deque = new Deque(/** @scrutinizer ignore-type */ $values ?? []);
Loading history...
45
    }
46
47
    /**
48
     * Ensures that enough memory is allocated for a specified capacity. This
49
     * potentially reduces the number of reallocations as the size increases.
50
     *
51
     * @param int $capacity The number of values for which capacity should be
52
     *                      allocated. Capacity will stay the same if this value
53
     *                      is less than or equal to the current capacity.
54
     */
55
    public function allocate(int $capacity)
56
    {
57
        $this->deque->allocate($capacity);
58
    }
59
60
    /**
61
     * Returns the current capacity of the queue.
62
     *
63
     * @return int
64
     */
65
    public function capacity(): int
66
    {
67
        return $this->deque->capacity();
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function clear()
74
    {
75
        $this->deque->clear();
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function copy()
82
    {
83
        return new self($this->deque);
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function count(): int
90
    {
91
        return count($this->deque);
92
    }
93
94
    /**
95
     * Returns the value at the front of the queue without removing it.
96
     *
97
     * @return
98
     */
99
    public function peek()
100
    {
101
        return $this->deque->first();
102
    }
103
104
    /**
105
     * Returns and removes the value at the front of the Queue.
106
     *
107
     * @return mixed
108
     */
109
    public function pop()
110
    {
111
        return $this->deque->shift();
112
    }
113
114
    /**
115
     * Pushes zero or more values into the front of the queue.
116
     *
117
     * @param mixed ...$values
118
     */
119
    public function push(...$values)
120
    {
121
        $this->deque->push(...$values);
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    public function toArray(): array
128
    {
129
        return $this->deque->toArray();
130
    }
131
132
    /**
133
     * Get iterator
134
     */
135
    public function getIterator()
136
    {
137
        while (!$this->isEmpty()) {
138
            yield $this->pop();
139
        }
140
    }
141
142
    /**
143
     * @inheritdoc
144
     *
145
     * @throws OutOfBoundsException
146
     */
147
    public function offsetSet($offset, $value)
148
    {
149
        if ($offset === null) {
150
            $this->push($value);
151
        } else {
152
            throw new OutOfBoundsException();
153
        }
154
    }
155
156
    /**
157
     * @inheritdoc
158
     *
159
     * @throws Error
160
     */
161
    public function offsetGet($offset)
162
    {
163
        throw new Error();
164
    }
165
166
    /**
167
     * @inheritdoc
168
     *
169
     * @throws Error
170
     */
171
    public function offsetUnset($offset)
172
    {
173
        throw new Error();
174
    }
175
176
    /**
177
     * @inheritdoc
178
     *
179
     * @throws Error
180
     */
181
    public function offsetExists($offset)
182
    {
183
        throw new Error();
184
    }
185
}
186