Completed
Push — master ( df6e21...211684 )
by Roman
03:55 queued 58s
created

Stack::push()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace PeacefulBit\Packet\Context;
4
5
use function Nerd\Common\Arrays\append;
6
use function Nerd\Common\Functional\tail;
7
8
use PeacefulBit\Packet\Exception\RuntimeException;
9
10
class Stack
11
{
12
    private $stack = [];
13
14
    public function push($value)
15
    {
16
        array_push($this->stack, $value);
17
    }
18
19
    public function shift()
20
    {
21
        if (empty($this->stack)) {
22
            throw new RuntimeException("Stack is empty");
23
        }
24
25
        return array_pop($this->stack);
26
    }
27
28
    public function shiftGroup($number)
29
    {
30
        $iter = tail(function ($left, $acc) use (&$iter) {
31
            if ($left == 0) {
32
                return array_reverse($acc);
33
            }
34
            return $iter($left - 1, append($acc, $this->shift()));
35
        });
36
37
        return $iter($number, []);
38
    }
39
}
40