Passed
Push — master ( 9a224a...3e0fbd )
by Sebastian
02:02
created

StackTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 46
ccs 15
cts 15
cp 1
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
 * @property $array Base array of this data structure
18
 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $array at position 0 could not be parsed: Unknown type name '$array' at position 0 in $array.
Loading history...
19
trait StackTrait
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 4
    public function push($item)
25
    {
26 4
        $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 4
        return $this;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function pop()
34
    {
35 1
        return array_pop($this->array);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function peek()
42
    {
43 1
        return end($this->array);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function search($element)
50
    {
51 1
        $pos = array_search($element, $this->array);
52 1
        if ($pos === false) {
53 1
            return 0;
54
        }
55 1
        $count = $this->count();
56 1
        return $count - $pos;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 4
    public function count()
63
    {
64 4
        return count($this->array);
65
    }
66
}
67