Completed
Push — master ( cc51b8...3f6140 )
by Nate
02:16
created

HashSet   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 155
Duplicated Lines 5.81 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 9
loc 155
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A add() 0 10 2
A addAll() 9 9 2
A clear() 0 4 1
A remove() 0 7 1
A contains() 0 4 1
A toArray() 0 4 1
A filter() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Collection;
8
9
use ArrayIterator;
10
11
/**
12
 * Class HashSet
13
 *
14
 * An [@see SetInterface] backed by a [@see HashMap]
15
 *
16
 * @author Nate Brunette <[email protected]>
17
 */
18
class HashSet extends AbstractSet
19
{
20
    /**
21
     * The data storage
22
     *
23
     * @var MapInterface
24
     */
25
    protected $map;
26
27
    /**
28
     * Constructor
29
     *
30
     * Use [@see AbstractSet::add] to ensure uniqueness
31
     *
32
     * @param CollectionInterface $elements
33
     */
34 9
    public function __construct(CollectionInterface $elements = null)
35
    {
36 9
        $this->map = new HashMap();
37
38 9
        if (null !== $elements) {
39 8
            $this->addAll($elements);
40
        }
41 9
    }
42
43
    /**
44
     * Adds the element to the collection if it doesn't exist
45
     *
46
     * By default this method will use strict comparison checking, passing false
47
     * in will use a double equals (==) instead.
48
     *
49
     * @param mixed $element
50
     * @param bool $strict
51
     * @return bool
52
     */
53 19
    public function add($element, bool $strict = true): bool
54
    {
55 19
        if ($this->contains($element, $strict)) {
56 5
            return false;
57
        }
58
59 19
        $this->map->put($element, true);
60
61 19
        return true;
62
    }
63
64
    /**
65
     * Adds any elements from specified collection that do not already exist
66
     *
67
     * By default this method will use strict comparison checking, passing false
68
     * in will use a double equals (==) instead.
69
     *
70
     * @param CollectionInterface $collection
71
     * @param bool $strict
72
     * @return bool
73
     */
74 11 View Code Duplication
    public function addAll(CollectionInterface $collection, bool $strict = true): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76 11
        $size = $this->count();
77 11
        foreach ($collection as $element) {
78 11
            $this->add($element, $strict);
79
        }
80
81 11
        return $size !== $this->count();
82
    }
83
84
    /**
85
     * Removes all elements from a collection
86
     *
87
     * @return void
88
     */
89 1
    public function clear()
90
    {
91 1
        $this->map->clear();
92 1
    }
93
94
    /**
95
     * Removes object if it exists
96
     *
97
     * By default this method will use strict comparison checking, passing false
98
     * in will use a double equals (==) instead.
99
     *
100
     * Returns true if the element was removed
101
     *
102
     * @param mixed $element
103
     * @param bool $strict
104
     * @return bool
105
     */
106 3
    public function remove($element, bool $strict = true): bool
107
    {
108 3
        $size = $this->map->count();
109 3
        $this->map->remove($element, $strict);
110
111 3
        return $size !== $this->map->count();
112
    }
113
114
    /**
115
     * Returns true if the collection contains element
116
     *
117
     * By default this method will use strict comparison checking, passing false
118
     * in will use a double equals (==) instead.
119
     *
120
     * @param mixed $element
121
     * @param bool $strict
122
     * @return bool
123
     */
124 19
    public function contains($element, bool $strict = true): bool
125
    {
126 19
        return $this->map->containsKey($element, $strict);
127
    }
128
129
    /**
130
     * Returns an array of all elements in the collection
131
     *
132
     * @return array
133
     */
134 5
    public function toArray(): array
135
    {
136 5
        return $this->map->keys()->toArray();
137
    }
138
139
    /**
140
     * Filter the collection using closure
141
     *
142
     * The closure will get passed each element.  Returning true from the
143
     * closure will include that element in the new collection.
144
     *
145
     * @param callable $filter
146
     * @return CollectionInterface
147
     */
148 1
    public function filter(callable $filter): CollectionInterface
149
    {
150 1
        return new HashSet(new ArrayList(array_filter($this->map->keys()->toArray(), $filter)));
151
    }
152
153
    /**
154
     * Retrieve an external iterator
155
     *
156
     * @return ArrayIterator
157
     */
158 1
    public function getIterator(): ArrayIterator
159
    {
160 1
        return new ArrayIterator($this->map->keys()->toArray());
161
    }
162
163
    /**
164
     * Returns the size of the collection
165
     *
166
     * @return int
167
     */
168 14
    public function count(): int
169
    {
170 14
        return count($this->map);
171
    }
172
}
173