Completed
Pull Request — master (#2)
by Emily
02:10
created

HashSet::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Collection;
16
17
use ArrayIterator;
18
use Spaark\CompositeUtils\Service\HashProducer;
19
20
/**
21
 * Represents an List stored in a PHP array
22
 */
23
class HashSet extends AbstractSet
24
{
25
    /**
26
     * @var ValueType[]
27
     */
28
    protected $data = [];
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function add($item)
34
    {
35
        $this->data[HashProducer::getHash($item)] = $item;
36
    }
37
38
    /**
39
     * Checks if an element exists
40
     *
41
     * @param ValueType The item to search for
42
     * @return boolean If the item exists
43
     */
44
    public function contains($item) : bool
45
    {
46
        return isset($this->data[HashProducer::getHash($item)]);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function remove($item)
53
    {
54
        unset($this->data[HashProducer::getHash($item)]);
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function getIterator()
61
    {
62
        return new ArrayIterator($this->data);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function size() : int
69
    {
70
        return count($this->data);
71
    }
72
}
73
74