Set   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 66
rs 10
c 0
b 0
f 0

5 Methods

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