ArraySet   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 90
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A add() 0 10 2
A clear() 0 4 1
A iterator() 0 4 1
A remove() 0 14 3
A size() 0 4 1
A toArray() 0 4 1
1
<?php
2
namespace Jmw\Collection\Set;
3
4
use Jmw\Collection\Lists\Tuple;
5
use Jmw\Collection\Lists\ArrayList;
6
/**
7
 * An implementation of a Set using a php array
8
 * It is advisable to use a HashSet in most instances,
9
 * as it is significantly faster
10
 * @author john
11
 *
12
 */
13
class ArraySet extends SetAbstract
14
{
15
	/**
16
	 * @var array
17
	 */
18
	protected $array;
19
	
20
	/**
21
	 * @param array $array
22
	 */
23
	public function __construct($array)
24
	{
25
		$this->clear();
26
		foreach ($array as $element)
27
		{
28
			$this->add($element);
29
		}
30
	}
31
	
32
	/**
33
	 * Ensures that this collection contains the specified element (optional operation).
34
	 * @param multitype $element
35
	 * @return boolean
36
	 */
37
	public function add($element)
38
	{
39
		$changed = false;
40
		if(!$this->contains($element))
41
		{
42
			$this->array[] = $element;
43
			$changed = true;
44
		}
45
		return $changed;
46
	}
47
	
48
	/**
49
	 * Removes all of the elements from this collection (optional operation).
50
	 * @return void
51
	 */
52
	public function clear()
53
	{
54
		$this->array = [];
55
	}
56
	
57
	/**
58
	 * Returns an iterator over the elements in this collection.
59
	 * @return IteratorInterface
60
	 */
61
	public function iterator()
62
	{
63
		return new SetIterator($this);
64
	}
65
	
66
	/**
67
	 * Removes a single instance of the specified element from this collection, if it is present (optional operation).
68
	 * @param boolean $changed
0 ignored issues
show
Bug introduced by
There is no parameter named $changed. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
69
	 */
70
	public function remove($element)
71
	{
72
		$changed = false;
73
		foreach ($this->array as $key=>$value)
74
		{
75
			if($element === $value)
76
			{
77
				$changed = true;
78
				unset($this->array[$key]);
79
				$this->array = array_values($this->array);
80
			}
81
		}
82
		return $changed;
83
	}
84
	
85
	/**
86
	 * Returns the number of elements in this collection.
87
	 * @return int
88
	 */
89
	public function size()
90
	{
91
		return count($this->array);
92
	}
93
	
94
	/**
95
	 * Returns an array containing all of the elements in this collection.
96
	 * @return array
97
	 */
98
	public function toArray()
99
	{
100
		return $this->array;
101
	}
102
}