Queue::pop()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace PhpSimple;
4
5
use Countable;
6
7
use function array_shift;
8
use function count;
9
use function current;
10
use function in_array;
11
12
/**
13
 * @template Q
14
 */
15
final class Queue implements Countable
16
{
17
    public function __construct(/** @var array<Q> */ private array $items = [])
18
    {
19
    }
20
21
    public function clear(): void
22
    {
23
        $this->items = [];
24
    }
25
26
    public function contains(mixed $item): bool
27
    {
28
        return in_array(needle: $item, haystack: $this->items, strict: true);
29
    }
30
31
    public function pop(): mixed
32
    {
33
        if ($this->isEmpty()) {
34
            return false;
35
        }
36
37
        return array_shift(array: $this->items);
38
    }
39
40
    public function push(mixed $item): void
41
    {
42
        if (null !== $item) {
43
            $this->items[] = $item;
44
        }
45
    }
46
47
    public function peek(): mixed
48
    {
49
        return current(array: $this->items);
50
    }
51
52
    public function isEmpty(): bool
53
    {
54
        return [] === $this->items;
55
    }
56
57
    public function count(): int
58
    {
59
        return count(value: $this->items);
60
    }
61
}
62