Stack   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 60
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A push() 0 5 1
A pushAll() 0 7 2
A peek() 0 7 2
A pop() 0 3 1
1
<?php
2
namespace phootwork\collection;
3
4
use \Iterator;
5
6
/**
7
 * Represents a Stack
8
 * 
9
 * FILO - first in last out
10
 * 
11
 * @author Thomas Gossmann
12
 */
13
class Stack extends AbstractList {
14
15
	/**
16
	 * Creates a new ArrayList
17
	 * 
18
	 * @param array|Iterator $collection
19
	 */
20
	public function __construct($collection = []) {
21
		$this->pushAll($collection);
22
	}
23
	
24
	/**
25
	 * Pushes an element onto the stack
26
	 * 
27
	 * @param mixed $element
28
	 * @return $this
29
	 */
30
	public function push($element) {
31
		array_push($this->collection, $element);
32
		
33
		return $this;
34
	}
35
	
36
	/**
37
	 * Pushes many elements onto the stack
38
	 *
39
	 * @param array|Iterator $collection
40
	 * @return $this
41
	 */
42
	public function pushAll($collection) {
43
		foreach ($collection as $element) {
44
			$this->push($element);
45
		}
46
		
47
		return $this;
48
	}
49
	
50
	/**
51
	 * Returns the element at the head or null if the stack is empty but doesn't remove that element  
52
	 * 
53
	 * @return mixed
54
	 */
55
	public function peek() {
56
		if ($this->size() > 0) {
57
			return $this->collection[$this->size() - 1];
58
		}
59
60
		return null;
61
	}
62
	
63
	/**
64
	 * Pops the element at the head from the stack or null if the stack is empty
65
	 * 
66
	 * @return mixed
67
	 */
68
	public function pop() {
69
		return array_pop($this->collection);
70
	}
71
72
}