Completed
Pull Request — master (#3)
by Cristiano
01:50
created

ArrayList   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 72
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add() 0 9 2
A addAll() 0 7 2
A remove() 0 8 2
A removeAll() 0 7 2
1
<?php
2
namespace phootwork\collection;
3
4
use \Iterator;
5
6
/**
7
 * Represents a List
8
 * 
9
 * @author Thomas Gossmann
10
 */
11
class ArrayList extends AbstractList {
12
	
13
	/**
14
	 * Creates a new ArrayList
15
	 * 
16
	 * @param array|Iterator $collection
17
	 */
18 24
	public function __construct($collection = []) {
19 24
		$this->addAll($collection);
20 24
	}
21
	
22
	/**
23
	 * Adds an element to that list
24
	 * 
25
	 * @param mixed $element
26
	 * @param int $index
27
	 * @return $this
28
	 */
29 24
	public function add($element, $index = null) {
30 24
		if ($index === null) {
31 24
			$this->collection[$this->size()] = $element;
32
		} else {
33 1
			array_splice($this->collection, $index, 0, $element);
34
		}
35
		
36 24
		return $this;
37
	}
38
	
39
	/**
40
	 * Adds all elements to the list
41
	 * 
42
	 * @param array|Iterator $collection
43
	 * @return $this
44
	 */
45 24
	public function addAll($collection) {
46 24
		foreach ($collection as $element) {
47 18
			$this->add($element);
48
		}
49
		
50 24
		return $this;
51
	}
52
53
	/**
54
	 * Removes an element from the list
55
	 * 
56
	 * @param mixed $element
57
	 * @return $this
58
	 */
59 2
	public function remove($element) {
60 2
		$index = array_search($element, $this->collection, true);
61 2
		if ($index !== null) {
62 2
			unset($this->collection[$index]);
63
		}
64
65 2
		return $this;
66
	}
67
68
	/**
69
	 * Removes all elements from the list
70
	 *
71
	 * @param array|Iterator $collection
72
	 * @return $this
73
	 */
74 2
	public function removeAll($collection) {
75 2
		foreach ($collection as $element) {
76 2
			$this->remove($element);
77
		}
78
		
79 2
		return $this;
80
	}
81
82
}
83