Passed
Push — master ( 72301e...841a4b )
by Rudi
03:02
created

Queue::copy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace Ds;
3
4
use Error;
5
use OutOfBoundsException;
6
7
/**
8
 * A “first in, first out” or “FIFO” collection that only allows access to the
9
 * value at the front of the queue and iterates in that order, destructively.
10
 *
11
 * @package Ds
12
 */
13 View Code Duplication
final class Queue implements \IteratorAggregate, \ArrayAccess, Collection
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    use Traits\GenericCollection;
16
17
    const MIN_CAPACITY = 8;
18
19
    /**
20
     * @var Deque internal deque to store values.
21
     */
22
    private $deque;
23
24
    /**
25
     * Creates an instance using the values of an array or Traversable object.
26
     *
27
     * @param array|\Traversable $values
0 ignored issues
show
Documentation introduced by
Should the type for parameter $values not be array|\Traversable|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

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