Completed
Push — master ( 8d1df5...05ea10 )
by Rudi
03:16
created

Queue::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Ds;
3
4
use OutOfBoundsException;
5
6
/**
7
 * Queue
8
 * @package Ds
9
 */
10 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...
11
{
12
    use Traits\Collection;
13
14
    const MIN_CAPACITY = 8;
15
16
    /**
17
     * @var Deque
18
     */
19
    private $internal;
20
21
    /**
22
     * Creates an instance using the values of an array or Traversable object.
23
     *
24
     * @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...
25
     */
26 96
    public function __construct($values = null)
27
    {
28 96
        $this->internal = new Deque($values ?: []);
29 96
    }
30
31
    /**
32
     * Ensures that enough memory is allocated for a specified capacity. This
33
     * potentially reduces the number of reallocations as the size increases.
34
     *
35
     * @param int $capacity The number of values for which capacity should be
36
     *                      allocated. Capacity will stay the same if this value
37
     *                      is less than or equal to the current capacity.
38
     */
39 6
    public function allocate(int $capacity)
40
    {
41 6
        $this->internal->allocate($capacity);
42 6
    }
43
44
    /**
45
     * Returns the current capacity of the queue.
46
     *
47
     * @return int
48
     */
49 9
    public function capacity(): int
50
    {
51 9
        return $this->internal->capacity();
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 2
    public function clear()
58
    {
59 2
        $this->internal->clear();
60 2
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65 5
    public function copy(): \Ds\Collection
66
    {
67 5
        return new self($this->internal);
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 40
    public function count(): int
74
    {
75 40
        return count($this->internal);
76
    }
77
78
    /**
79
     * Returns the value at the front of the queue without removing it.
80
     *
81
     * @return
82
     */
83 3
    public function peek()
84
    {
85 3
        return $this->internal->first();
86
    }
87
88
    /**
89
     * Returns and removes the value at the front of the Queue.
90
     *
91
     * @return mixed
92
     */
93 12
    public function pop()
94
    {
95 12
        return $this->internal->shift();
96
    }
97
98
    /**
99
     * Pushes zero or more values into the front of the queue.
100
     *
101
     * @param mixed ...$values
102
     */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $values a bit more specific; maybe use array.
Loading history...
103 17
    public function push(...$values)
104
    {
105 17
        $this->internal->push(...$values);
106 17
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111 63
    public function toArray(): array
112
    {
113 63
        return $this->internal->toArray();
114
    }
115
116
    /**
117
     * Get iterator
118
     */
119 7
    public function getIterator()
120
    {
121 7
        while ( ! $this->isEmpty()) {
122 6
            yield $this->pop();
123
        }
124 7
    }
125
126
127
    /**
128
     * @inheritdoc
129
     *
130
     * @throws OutOfBoundsException
131
     */
132 5
    public function offsetSet($offset, $value)
133
    {
134 5
        if ($offset === null) {
135 4
            $this->push($value);
136
        } else {
137 1
            throw new OutOfBoundsException();
138
        }
139 4
    }
140
141
    /**
142
     * @inheritdoc
143
     *
144
     * @throws Error
145
     */
146 1
    public function offsetGet($offset)
147
    {
148 1
        throw new Error();
149
    }
150
151
    /**
152
     * @inheritdoc
153
     *
154
     * @throws Error
155
     */
156 1
    public function offsetUnset($offset)
157
    {
158 1
        throw new Error();
159
    }
160
161
    /**
162
     * @inheritdoc
163
     *
164
     * @throws Error
165
     */
166 2
    public function offsetExists($offset)
167
    {
168 2
        throw new Error();
169
    }
170
}
171