Test Setup Failed
Branch version-2.0 (c21687)
by Sebastian
04:11
created

StackTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A pop() 0 3 1
A count() 0 3 1
A push() 0 4 1
A peek() 0 3 1
A search() 0 8 2
1
<?php
2
/*
3
 * Copyright (C) 2018 Sebastian Böttger <[email protected]>
4
 * You may use, distribute and modify this code under the
5
 * terms of the MIT license.
6
 *
7
 * You should have received a copy of the MIT license with
8
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
9
 */
10
11
namespace Seboettg\Collection\Stack;
12
13
/**
14
 * Trait StackTrait
15
 * @package Seboettg\Collection
16
 * @author Sebastian Böttger <[email protected]>
17
 */
18
trait StackTrait
19
{
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function push($item)
25
    {
26
        $this->array[] = $item;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
        return $this;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function pop()
34
    {
35
        return array_pop($this->array);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function peek()
42
    {
43
        return end($this->array);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function count()
50
    {
51
        return count($this->array);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function search($element)
58
    {
59
        $pos = array_search($element, $this->array);
60
        if ($pos === false) {
61
            return 0;
62
        }
63
        $count = $this->count();
64
        return $count - $pos;
65
    }
66
}
67