StackTrait   A
last analyzed

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