Passed
Pull Request — master (#24)
by Igor
02:46
created

ArrayQueue   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 13
c 2
b 0
f 0
dl 0
loc 41
ccs 15
cts 17
cp 0.8824
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A offer() 0 3 1
A poll() 0 8 2
A peek() 0 7 2
A stream() 0 3 1
A getIndexIterator() 0 3 1
A parallelStream() 0 3 1
1
<?php
2
/**
3
 * @author  Igor Pomiluyko [email protected]
4
 * @license MIT
5
 */
6
7
namespace WS\Utils\Collections;
8
9
use RuntimeException;
10
use WS\Utils\Collections\Iterator\Iterator;
11
use WS\Utils\Collections\Iterator\IteratorFactory;
12
13
class ArrayQueue extends AbstractCollection implements Queue, IndexIterable
14
{
15
    use RemoveTraverseTrait;
16
17 1
    public function offer($element): bool
18
    {
19 1
        return $this->add($element);
20
    }
21
22 4
    public function poll()
23
    {
24 4
        if ($this->isEmpty()) {
25 1
            throw new RuntimeException('Queue is empty');
26
        }
27
28 3
        $this->pointer--;
29 3
        return array_shift($this->elements);
30
    }
31
32 3
    public function peek()
33
    {
34 3
        if ($this->isEmpty()) {
35 1
            throw new RuntimeException('Queue is empty');
36
        }
37
38 2
        return $this->elements[0];
39
    }
40
41 1
    public function stream(): Stream
42
    {
43 1
        return new SerialStream($this);
44
    }
45
46
    public function parallelStream($workersPool = null): Stream
47
    {
48
        return new ParallelStream($this, $workersPool);
49
    }
50
51 5
    public function getIndexIterator(): Iterator
52
    {
53 5
        return IteratorFactory::directSequence($this->size());
54
    }
55
}
56