Passed
Push — develop ( a803f3...78e549 )
by nguereza
03:14
created

Stack::push()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Platine Collection
5
 *
6
 * Platine Collection provides a flexible and simple PHP collection implementation.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Collection
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Stack.php
33
 *
34
 *  The Stack class
35
 *
36
 *  @package    Platine\Collection\Stack
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Collection\Stack;
48
49
use Countable;
50
use Platine\Collection\Exception\InvalidOperationException;
51
use Platine\Collection\TypeCheck;
52
53
/**
54
 * Class Stack
55
 * @package Platine\Collection\Stack
56
 */
57
class Stack implements Countable
58
{
59
60
    /**
61
     *
62
     * @var string
63
     */
64
    protected string $type;
65
66
    /**
67
     *
68
     * @var array<int, mixed>
69
     */
70
    protected array $data;
71
72
    /**
73
     * Create new instance
74
     * @param string $type
75
     */
76
    public function __construct(string $type)
77
    {
78
        $this->data = [];
79
        $this->type = $type;
80
    }
81
82
    /**
83
     *
84
     * @return void
85
     */
86
    public function clear(): void
87
    {
88
        $this->data = [];
89
    }
90
91
    /**
92
     * {@inheritedoc}
93
     */
94
    public function count(): int
95
    {
96
        return count($this->data);
97
    }
98
99
    /**
100
     *
101
     * @return bool
102
     */
103
    public function isEmpty(): bool
104
    {
105
        return $this->count() === 0;
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111
    public function peek()
112
    {
113
        if ($this->isEmpty()) {
114
            throw new InvalidOperationException('The collection is empty');
115
        }
116
117
        return $this->data[$this->count() - 1];
118
    }
119
120
    /**
121
     *
122
     * @return mixed
123
     */
124
    public function pop()
125
    {
126
        return array_pop($this->data);
127
    }
128
129
130
    /**
131
     *
132
     * @param mixed $value
133
     * @return mixed
134
     */
135
    public function push($value)
136
    {
137
        TypeCheck::isValueOf(
138
            $value,
139
            $this->type,
140
            sprintf(
141
                'The type specified for this collection is [%s], '
142
                    . 'you cannot pass a value of type [%s]',
143
                $this->type,
144
                gettype($value)
145
            )
146
        );
147
148
        $this->data[] = $value;
149
150
        return $value;
151
    }
152
}
153