Completed
Pull Request — master (#43)
by
unknown
08:27
created

Stack::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Ds;
3
4
use Error;
5
use OutOfBoundsException;
6
7
/**
8
 * A “last in, first out” or “LIFO” collection that only allows access to the
9
 * value at the top of the structure and iterates in that order, destructively.
10
 *
11
 * @package Ds
12
 */
13 View Code Duplication
final class Stack implements \IteratorAggregate, \ArrayAccess, Collection
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    use Traits\GenericCollection;
16
17
    /**
18
     * @var Vector internal vector to store values of the stack.
19
     */
20
    private $vector;
21
22
    /**
23
     * Creates an instance using the values of an array or Traversable object.
24
     *
25
     * @param array|\Traversable $values
0 ignored issues
show
Documentation introduced by
Should the type for parameter $values not be array|\Traversable|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
26
     */
27
    public function __construct($values = null)
28
    {
29
        $this->vector = new Vector($values ?: []);
30
    }
31
32
    /**
33
     * Clear all elements in the Stack
34
     */
35
    public function clear()
36
    {
37
        $this->vector->clear();
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function copy(): Collection
44
    {
45
        return new self($this->vector);
46
    }
47
48
    /**
49
     * Returns the number of elements in the Stack
50
     *
51
     * @return int
52
     */
53
    public function count(): int
54
    {
55
        return count($this->vector);
56
    }
57
58
    /**
59
     * Ensures that enough memory is allocated for a specified capacity. This
60
     * potentially reduces the number of reallocations as the size increases.
61
     *
62
     * @param int $capacity The number of values for which capacity should be
63
     *                      allocated. Capacity will stay the same if this value
64
     *                      is less than or equal to the current capacity.
65
     */
66
    public function allocate(int $capacity)
67
    {
68
        $this->vector->allocate($capacity);
69
    }
70
71
    /**
72
     * Returns the current capacity of the stack.
73
     *
74
     * @return int
75
     */
76
    public function capacity(): int
77
    {
78
        return $this->vector->capacity();
79
    }
80
81
    /**
82
     * Returns the value at the top of the stack without removing it.
83
     *
84
     * @return mixed
85
     *
86
     * @throws \UnderflowException if the stack is empty.
87
     */
88
    public function peek()
89
    {
90
        return $this->vector->last();
91
    }
92
93
    /**
94
     * Returns and removes the value at the top of the stack.
95
     *
96
     * @return mixed
97
     *
98
     * @throws \UnderflowException if the stack is empty.
99
     */
100
    public function pop()
101
    {
102
        return $this->vector->pop();
103
    }
104
105
    /**
106
     * Pushes zero or more values onto the top of the stack.
107
     *
108
     * @param mixed ...$values
109
     */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $values a bit more specific; maybe use array.
Loading history...
110
    public function push(...$values)
111
    {
112
        $this->vector->push(...$values);
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function toArray(): array
119
    {
120
        return array_reverse($this->vector->toArray());
121
    }
122
123
    /**
124
     *
125
     */
126
    public function getIterator()
127
    {
128
        while ( ! $this->isEmpty()) {
129
            yield $this->pop();
130
        }
131
    }
132
133
    /**
134
     * @inheritdoc
135
     *
136
     * @throws OutOfBoundsException
137
     */
138
    public function offsetSet($offset, $value)
139
    {
140
        if ($offset === null) {
141
            $this->push($value);
142
        } else {
143
            throw new OutOfBoundsException();
144
        }
145
    }
146
147
    /**
148
     * @inheritdoc
149
     *
150
     * @throws Error
151
     */
152
    public function offsetGet($offset)
153
    {
154
        throw new Error();
155
    }
156
157
    /**
158
     * @inheritdoc
159
     *
160
     * @throws Error
161
     */
162
    public function offsetUnset($offset)
163
    {
164
        throw new Error();
165
    }
166
167
    /**
168
     * @inheritdoc
169
     *
170
     * @throws Error
171
     */
172
    public function offsetExists($offset)
173
    {
174
        throw new Error();
175
    }
176
}
177