Passed
Branch version-2.0 (b3c55d)
by Sebastian
01:53
created

StackTrait::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
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
     * @var array
23
     */
24
    protected $array;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 4
    public function push($item)
30
    {
31 4
        $this->array[] = $item;
32 4
        return $this;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function pop()
39
    {
40 1
        return array_pop($this->array);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function peek()
47
    {
48 1
        return end($this->array);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 4
    public function count()
55
    {
56 4
        return count($this->array);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function search($element)
63
    {
64 1
        $pos = array_search($element, $this->array);
65 1
        if ($pos === false) {
66 1
            return 0;
67
        }
68 1
        $count = $this->count();
69 1
        return $count - $pos;
70
    }
71
}
72