MiniStack::push()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
/*
3
 * This file is part of Rivescript-php
4
 *
5
 * (c) Johnny Mast <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Axiom\Rivescript\Cortex\MiniStack;
12
13
use Axiom\Collections\Collection;
14
15
/**
16
 * MiniStack class
17
 *
18
 * The mini stack remembers an x number of items on
19
 * its stack. This value is passed through to the constructor.
20
 *
21
 * PHP version 7.4 and higher.
22
 *
23
 * @category Core
24
 * @package  Cortext\MiniStack
25
 * @author   Johnny Mast <[email protected]>
26
 * @license  https://opensource.org/licenses/MIT MIT
27
 * @link     https://github.com/axiom-labs/rivescript-php
28
 * @since    0.4.0
29
 */
30
class MiniStack extends Collection
31
{
32
    /**
33
     * The stack size.
34
     *
35
     * @var int
36
     */
37
    protected int $size = 0;
38
39
    /**
40
     * MiniStack Constructor.
41
     *
42
     * @param int $size The maximum amount of slices the stack can contain.
43
     */
44
    public function __construct(int $size = 0)
45
    {
46
        parent::__construct();
47
48
        $this->size = $size;
49
    }
50
51
    /**
52
     * Push an item to the MiniStack.
53
     *
54
     * @param mixed $value The value to push
55
     *
56
     * @return void
57
     */
58
    public function push($value): void
59
    {
60
        if ($this->count() >= $this->size) {
61
            $keys = array_keys($this->all());
62
            $this->remove($keys[0]);
63
        }
64
65
        parent::push($value);
66
    }
67
68
    /**
69
     * Return the MiniStack size.
70
     *
71
     * @return int
72
     */
73
    public function getSize(): int
74
    {
75
        return $this->size;
76
    }
77
}
78