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

Stack   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 30
rs 10
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A push() 0 4 1
A shift() 0 8 2
A shiftGroup() 0 11 2
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