Completed
Push — master ( c01e0d...08b38d )
by Thomas
01:22
created

Queue::poll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
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 phootwork\collection;
3
4
use \Iterator;
5
6
/**
7
 * Represents a Queue
8
 * 
9
 * FIFO - first in first out
10
 * 
11
 * @author Thomas Gossmann
12
 */
13
class Queue extends AbstractList {
14
	
15
	/**
16
	 * Creates a new Queue
17
	 * 
18
	 * @param array|Iterator $collection
19
	 */
20 7
	public function __construct($collection = []) {
21 7
		foreach ($collection as $element) {
22 5
			$this->collection[] = $element;
23 7
		}
24 7
	}
25
	
26
	/**
27
	 * Enqueues an element
28
	 * 
29
	 * @param mixed $element
30
	 * @return $this
31
	 */
32 3
	public function enqueue($element) {
33 3
		array_unshift($this->collection, $element);
34
		
35 3
		return $this;
36
	}
37
	
38
	/**
39
	 * Enqueues many elements
40
	 *
41
	 * @param array|Iterator $collection
42
	 * @return $this
43
	 */
44 1
	public function enqueueAll($collection) {
45 1
		foreach ($collection as $element) {
46 1
			$this->enqueue($element);
47 1
		}
48
	
49 1
		return $this;
50
	}
51
	
52
	/**
53
	 * Returns the element at the head or null if the queue is empty but doesn't remove that element  
54
	 * 
55
	 * @return mixed
56
	 */
57 2
	public function peek() {
58 2
		if ($this->size() > 0) {
59 1
			return $this->collection[0];
60
		}
61
		
62 1
		return null;
63
	}
64
	
65
	/**
66
	 * Removes and returns the element at the head or null if the is empty
67
	 * 
68
	 * @return mixed
69
	 */
70 2
	public function poll() {
71 2
		return array_shift($this->collection);
72
	}
73
	
74
}