Completed
Push — master ( 8010fb...ddaa9f )
by Ben
10s
created

Queue::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the ramsey/collection library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9
 * @license http://opensource.org/licenses/MIT MIT
10
 * @link https://benramsey.com/projects/ramsey-collection/ Documentation
11
 * @link https://packagist.org/packages/ramsey/collection Packagist
12
 * @link https://github.com/ramsey/collection GitHub
13
 */
14
15
namespace Ramsey\Collection;
16
17
use Ramsey\Collection\Exception\NoSuchElementException;
18
use Ramsey\Collection\Tool\TypeTrait;
19
use Ramsey\Collection\Tool\ValueToStringTrait;
20
21
class Queue extends AbstractArray implements QueueInterface
22
{
23
    use TypeTrait;
24
    use ValueToStringTrait;
25
26
    /**
27
     * @var string
28
     */
29
    private $queueType;
30
31
    /**
32
     * @var int
33
     */
34
    private $index = 0;
35
36
    /**
37
     * Queue constructor.
38
     */
39
    public function __construct($type, array $data = [])
40
    {
41
        $this->queueType = $type;
42
        parent::__construct($data);
43
    }
44
45 View Code Duplication
    public function offsetSet($offset, $value)
0 ignored issues
show
Duplication introduced by
This method 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...
46
    {
47
        if ($this->checkType($this->getType(), $value) === false) {
48
            throw new \InvalidArgumentException(
49
                'Value must be of type ' . $this->getType() . '; value is '
50
                . $this->toolValueToString($value)
51
            );
52
        }
53
        
54
        $this->data[] = $value;
55
    }
56
57
58
    /**
59
     * Ensures that this queue contains the specified element (optional
60
     * operation). Returns true if this queue changed as a result of the
61
     * call. (Returns false if this queue does not permit duplicates and
62
     * already contains the specified element.)
63
     *
64
     * Queues that support this operation may place limitations on what
65
     * elements may be added to this queue. In particular, some
66
     * queues will refuse to add null elements, and others will impose
67
     * restrictions on the type of elements that may be added. Queue
68
     * classes should clearly specify in their documentation any restrictions
69
     * on what elements may be added.
70
     *
71
     * If a queue refuses to add a particular element for any reason other
72
     * than that it already contains the element, it must throw an exception
73
     * (rather than returning false). This preserves the invariant that a
74
     * queue always contains the specified element after this call returns.
75
     *
76
     * @param mixed $element
77
     * @return bool true if this queue changed as a result of the call
78
     */
79
    public function add($element)
80
    {
81
        $this[] = $element;
82
83
        return true;
84
    }
85
86
    /**
87
     * Retrieves, but does not remove, the head of this queue. This method
88
     * differs from peek only in that it throws an exception if this queue is empty.
89
     *
90
     * @return mixed the head of this queue
91
     * @throws NoSuchElementException
92
     */
93
    public function element()
94
    {
95
        if ($this->count() === 0) {
96
            throw new NoSuchElementException(
97
                'Can\'t return element from Queue. Queue is empty.'
98
            );
99
        }
100
101
        return $this[$this->index];
102
    }
103
104
    /**
105
     * Inserts the specified element into this queue if it is possible to do so
106
     * immediately without violating capacity restrictions. When using a
107
     * capacity-restricted queue, this method is generally preferable to add(E),
108
     * which can fail to insert an element only by throwing an exception.
109
     *
110
     * @param $element
111
     * @return mixed true if the element was added to this queue, else false
112
     */
113
    public function offer($element)
114
    {
115
        $this[] = $element;
116
117
        return true;
118
    }
119
120
    /**
121
     * Retrieves, but does not remove, the head of this queue, or returns null
122
     * if this queue is empty.
123
     *
124
     * @return mixed the head of this queue, or null if this queue is empty
125
     */
126
    public function peek()
127
    {
128
        if ($this->count() === 0) {
129
            return null;
130
        }
131
132
        return $this[$this->index];
133
    }
134
135
    /**
136
     * Retrieves and removes the head of this queue, or returns null
137
     * if this queue is empty.
138
     *
139
     * @return mixed the head of this queue, or null if this queue is empty
140
     */
141 View Code Duplication
    public function poll()
0 ignored issues
show
Duplication introduced by
This method 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...
142
    {
143
        if ($this->count() === 0) {
144
            return null;
145
        }
146
147
        $head = $this[$this->index];
148
149
        unset($this[$this->index]);
150
        $this->index++;
151
152
        return $head;
153
    }
154
155
    /**
156
     * Retrieves and removes the head of this queue. This method differs
157
     * from poll only in that it throws an
158
     * exception if this queue is empty.
159
     *
160
     * @return mixed the head of this queue
161
     * @throws NoSuchElementException
162
     */
163 View Code Duplication
    public function remove()
0 ignored issues
show
Duplication introduced by
This method 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...
164
    {
165
        if ($this->count() === 0) {
166
            throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.');
167
        }
168
        $head = $this[$this->index];
169
170
        unset($this[$this->index]);
171
        $this->index++;
172
173
        return $head;
174
    }
175
176
    /**
177
     * Returns the type associated with this collection
178
     *
179
     * @return string
180
     */
181
    public function getType()
182
    {
183
        return $this->queueType;
184
    }
185
}
186