Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

HashSet::equal()   C

Complexity

Conditions 12
Paths 34

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 14.25

Importance

Changes 0
Metric Value
cc 12
eloc 20
nc 34
nop 1
dl 0
loc 33
ccs 18
cts 24
cp 0.75
crap 14.25
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Wandu\Math\Foundation\Set;
3
4
use InvalidArgumentException;
5
use Wandu\Math\Foundation\SetInterface;
6
use ArrayIterator;
7
8
class HashSet implements SetInterface
9
{
10
    /** @var array */
11
    protected $scalars = [];
12
13
    /** @var array */
14
    protected $objects = [];
15
16
    /**
17
     * @param array $items
18
     */
19 5
	public function __construct(array $items)
20
	{
21 5
        foreach ($items as $item) {
22 4
            $this->add($item);
23 5
        }
24 5
	}
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function count()
30
    {
31 1
        return count($this->scalars) + count($this->objects);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function equal(SetInterface $other)
38
    {
39 2
        if (!$other instanceof HashSet) {
40
            throw new InvalidArgumentException('unsupported type of Set.');
41
        }
42 2
        if (count($other->scalars) !== count($this->scalars)) {
43
            return false;
44
        }
45 2
        if (count($other->objects) !== count($this->objects)) {
46
            return false;
47
        }
48 2
        foreach ($this->scalars as $key => $item) {
49 2
            if (!array_key_exists($key, $other->scalars)) {
50 1
                return false;
51
            }
52 2
        }
53 2
        foreach ($other->scalars as $key => $item) {
54 2
            if (!array_key_exists($key, $this->scalars)) {
55
                return false;
56
            }
57 2
        }
58 2
        foreach ($this->objects as $key => $item) {
59 1
            if (!array_key_exists($key, $other->objects)) {
60
                return false;
61
            }
62 2
        }
63 2
        foreach ($other->objects as $key => $item) {
64 1
            if (!array_key_exists($key, $this->objects)) {
65
                return false;
66
            }
67 2
        }
68 2
        return true;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function intersection(SetInterface $other)
75
    {
76
        if (!$other instanceof HashSet) {
77
            throw new InvalidArgumentException('unsupported type of Set.');
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function union(SetInterface $other)
85
    {
86
        if (!$other instanceof HashSet) {
87
            throw new InvalidArgumentException('unsupported type of Set.');
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function difference(SetInterface $other)
95
    {
96
        if (!$other instanceof HashSet) {
97
            throw new InvalidArgumentException('unsupported type of Set.');
98
        }
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function has($item)
105
    {
106 1
        if (is_scalar($item)) {
107 1
            return array_key_exists(gettype($item) . $item, $this->scalars);
108
        }
109
        if (is_object($item)) {
110
            return array_key_exists(spl_object_hash($item), $this->objects);
111
        }
112
        return false;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 4
    public function add($item)
119
    {
120 4
        if (is_scalar($item)) {
121 4
            return $this->scalars[gettype($item) . $item] = $item;
122
        }
123 2
        if (is_object($item)) {
124 2
            return $this->objects[spl_object_hash($item)] = $item;
125
        }
126
        throw new InvalidArgumentException(
127
            'unsupported value: ' . str_replace("\n", '', var_export($item, true))
128
        );
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 1
    public function remove($item)
135
    {
136 1
        if (is_scalar($item)) {
137 1
            unset($this->scalars[gettype($item) . $item]);
138 1
        }
139 1
        if (is_object($item)) {
140
            unset($this->objects[spl_object_hash($item)]);
141
        }
142 1
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 1
    public function getIterator()
148
    {
149 1
        return new ArrayIterator(array_merge(
150 1
            array_values($this->scalars),
151 1
            array_values($this->objects)
152 1
        ));
153
    }
154
}
155