HashSet::remove()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
namespace Jmw\Collection\Set;
3
4
use Jmw\Collection\Map\HashMap;
5
use Jmw\Collection\Lists\ArrayList;
6
7
/**
8
 * This class implements the Set interface, backed by a hash table (actually a HashMap instance). 
9
 * It makes no guarantees as to the iteration order of the set; 
10
 * in particular, it does not guarantee that the order will remain constant over time. 
11
 * This class permits the null element.
12
 * @author john
13
 *
14
 */
15
class HashSet extends SetAbstract
16
{
17
	/**
18
	 * @var HashMap
19
	 */
20
	protected $map;
21
	
22
	/**
23
	 * Constructs a new HashSet instance
24
	 * It can be passed default values, and will implicitely remove
25
	 * any duplicates
26
	 * @param array
27
	 */
28
	public function __construct($array = [])
29
	{
30
		$this->clear();
31
		
32
		$this->addAll(new ArrayList($array));
33
	}
34
	
35
	/**
36
	 * Adds the specified element to this set if it is not already present. 
37
	 * More formally, adds the specified element e to this set if this set 
38
	 * contains no element e2 such that (e==null ? e2==null : e.equals(e2)). 
39
	 * If this set already contains the element, the call leaves the set unchanged 
40
	 * and returns false.
41
	 * @param multitype $element
42
	 * @return boolean
43
	 */
44
	public function add($element)
45
	{
46
		$changed = false;
47
		if(!$this->contains($element))
48
		{
49
			$key = $this->getHash($element);
50
			$this->map->put($key, $element);
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a object<Jmw\Collection\Map\multitype>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
			$changed = true;
52
		}
53
		
54
		return $changed;
55
	}
56
	
57
	/**
58
	 * Removes all of the elements from this set. 
59
	 * The set will be empty after this call returns.
60
	 * @return void
61
	*/
62
	public function clear()
63
	{
64
		$this->map = new HashMap();
65
	}
66
67
	/**
68
	 * Returns true if this collection contains the specified element.
69
	 * @return boolean;
0 ignored issues
show
Documentation introduced by
The doc-type boolean; could not be parsed: Expected "|" or "end of type", but got ";" at position 7. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
70
	 */
71
	public function contains($element)
72
	{
73
		return $this->map->containsKey($this->getHash($element));
0 ignored issues
show
Documentation introduced by
$this->getHash($element) is of type string, but the function expects a object<Jmw\Collection\Map\multitype>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
	}
75
	
76
	/**
77
	 * Returns an iterator over the elements in this collection.
78
	 * @return IteratorInterface
79
	*/
80
	public function iterator()
81
	{
82
		return new SetIterator($this);
83
	}
84
	
85
	/**
86
	 * Removes a single instance of the specified element from this collection, if it is present (optional operation).
87
	 * @param multitype $element
88
	 * @return boolean
89
	*/
90
	public function remove($element)
91
	{
92
		$changed = false;
93
		if($this->contains($element))
94
		{
95
			$this->map->remove($this->getHash($element));
0 ignored issues
show
Documentation introduced by
$this->getHash($element) is of type string, but the function expects a object<Jmw\Collection\Map\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
			$changed = true;
97
		}
98
		return $changed;
99
	}
100
	
101
	/**
102
	 * Returns the number of elements in this collection.
103
	 * @return int
104
	*/
105
	public function size()
106
	{
107
		return $this->map->size();
108
	}
109
	
110
	/**
111
	 * Returns an array containing all of the elements in this collection.
112
	 * @return array
113
	*/
114
	public function toArray()
115
	{
116
		return $this->map->values()->toArray();
117
	}
118
	
119
	/**
120
	 * Return a general hashing function for elements
121
	 * @param unknown $element
122
	 * @return string
123
	 */
124
	protected function getHash($element)
125
	{
126
		return hash('sha256', serialize($element));
127
	}
128
}