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

StackTrait::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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