TupleSet   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 94
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A add() 0 17 3
A clear() 0 4 1
A iterator() 0 4 1
A remove() 0 15 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\Exception\InvalidTypeException;
6
use Jmw\Collection\Lists\ArrayList;
7
/**
8
 * A set of Tuple elements, which ensures that only Tuple elements
9
 * may be added to the set, and no Tuple element may be duplicated
10
 * @author john
11
 *
12
 */
13
class TupleSet extends SetAbstract
14
{
15
	protected $arrayList;
16
	
17
	/**
18
	 * Construct a TupleSet from an array
19
	 * Each key=>value pair will become a Tuple<key, value>
20
	 * @param unknown $array
21
	 */
22
	public function __construct($array)
23
	{
24
		$this->clear();
25
		foreach ($array as $value1=>$value2)
26
		{
27
			$this->add(new Tuple($value1, $value2));
0 ignored issues
show
Documentation introduced by
new \Jmw\Collection\Lists\Tuple($value1, $value2) is of type object<Jmw\Collection\Lists\Tuple>, but the function expects a object<Jmw\Collection\Set\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...
28
		}
29
	}
30
	
31
	/**
32
	 * Ensures that this collection contains the specified element (optional operation).
33
	 * @param multitype $element
34
	 * @return boolean
35
	 */
36
	public function add($element)
37
	{
38
		$changed = false;
39
		if($element instanceof Tuple)
40
		{
41
			if(!$this->contains($element))
42
			{
43
				$this->arrayList->add($element);
44
				$changed = true;
45
			}
46
		}
47
		else
48
		{
49
			throw new InvalidTypeException('Jmw\Collection\Lists\Tuple', gettype($element));
0 ignored issues
show
Documentation introduced by
'Jmw\\Collection\\Lists\\Tuple' is of type string, but the function expects a object<Jmw\Collection\Exception\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...
Documentation introduced by
gettype($element) is of type string, but the function expects a object<Jmw\Collection\Exception\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...
50
		}
51
		$changed;
52
	}
53
	
54
	/**
55
	 * Removes all of the elements from this collection (optional operation).
56
	*/
57
	public function clear()
58
	{
59
		$this->arrayList = new ArrayList();
60
	}
61
	
62
	/**
63
	 * Returns an iterator over the elements in this collection.
64
	 * @return IteratorInterface
65
	*/
66
	public function iterator()
67
	{
68
		return new SetIterator($this);
69
	}
70
	
71
	/**
72
	 * Removes a single instance of the specified element from this collection, if it is present (optional operation).
73
	 * @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...
74
	*/
75
	public function remove($tuple)
76
	{
77
		$iterator = $this->arrayList->iterator();
78
		
79
		while($iterator->hasNext())
80
		{
81
			$element = $iterator->next();
82
			if($element->equals($tuple))
83
			{
84
				$iterator->remove();
85
				return true;
86
			}
87
		}
88
		return false;
89
	}
90
	
91
	/**
92
	 * Returns the number of elements in this collection.
93
	*/
94
	public function size()
95
	{
96
		return $this->arrayList->size();
97
	}
98
	
99
	/**
100
	 * Returns an array containing all of the elements in this collection.
101
	*/
102
	public function toArray()
103
	{
104
		return $this->arrayList->toArray();
105
	}
106
}