Completed
Push — master ( 0cb451...e9bf68 )
by Zoltán
11:26
created

HashSet::contains()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php namespace BuildR\Collection\Set;
2
3
use BuildR\Collection\Collection\AbstractCollection;
4
use BuildR\Collection\Exception\CollectionException;
5
6
/**
7
 * Set type (Collection implementation)
8
 * Sets only store scalar types
9
 *
10
 * BuildR PHP Framework
11
 *
12
 * @author Zoltán Borsos <[email protected]>
13
 * @package Collection
14
 * @subpackage Set
15
 *
16
 * @copyright    Copyright 2015, Zoltán Borsos.
17
 * @license      https://github.com/Zolli/BuildR/blob/master/LICENSE.md
18
 * @link         https://github.com/Zolli/BuildR
19
 */
20
class HashSet extends AbstractCollection implements SetInterface {
21
22
    /**
23
     * Set constructor.
24
     *
25
     * @param null|array $values
26
     */
27 31
    public function __construct($values = NULL) {
28 31
        if(is_array($values)) {
29 11
            $this->addAll($values);
30 11
        }
31 31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 27
    public function contains($element) {
37 27
        return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 10 View Code Duplication
    public function containsAll($elements) {
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...
44 10
        $elements = $this->collectionToArray($elements);
45
46 10
        $result = TRUE;
47
48 10
        foreach($elements as $item) {
49 10
            if($this->contains($item) === FALSE) {
50 5
                $result = FALSE;
51
52 5
                break;
53
            }
54 10
        }
55
56 10
        return $result;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 6 View Code Duplication
    public function containsAny($elements) {
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...
63 6
        $elements = $this->collectionToArray($elements);
64
65 6
        foreach($elements as $item) {
66 6
            if($this->contains($item) === TRUE) {
67 5
                return TRUE;
68
            }
69 6
        }
70
71 6
        return FALSE;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 6
    public function equals(SetInterface $collection) {
78
        //First check the size, if this not equals the tow collection
79
        //not be identical
80 6
        if($collection->size() !== $this->size()) {
81 1
            return FALSE;
82
        }
83
84
        //Use strict comparison to check arrays are equals
85
        //(Values and orders)
86 5
        return $collection->toArray() === $this->data;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 31
    public function add($element) {
93 31
        if(!is_scalar($element)) {
94 4
            throw CollectionException::nonScalarTypeGiven(gettype($element));
95
        }
96
97 27
        if(!$this->contains($element)) {
98 27
            $this->data[] = $element;
99 27
        }
100 27
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 25
    public function addAll($elements) {
106 25
        $elements = $this->collectionToArray($elements);
107
108 25
        foreach($elements as $item) {
109 25
            $this->add($item);
110 25
        }
111 25
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 2
    public function remove($element) {
117 2
        if(($index = array_search($element, $this->data)) !== FALSE) {
118 2
            unset($this->data[$index]);
119
120 2
            return TRUE;
121
        }
122
123 2
        return FALSE;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 1 View Code Duplication
    public function removeAll($elements) {
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...
130 1
        $elements = $this->collectionToArray($elements);
131
132 1
        $result = FALSE;
133
134 1
        foreach($elements as $item) {
135 1
            if($this->remove($item) === TRUE) {
136 1
                $result = TRUE;
137 1
            }
138 1
        }
139
140 1
        return $result;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 1
    public function retainAll($elements) {
147 1
        $elements = $this->collectionToArray($elements);
148
149 1
        $result = FALSE;
150
151 1 View Code Duplication
        foreach($this->data as $index => $data) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
152 1
            if(array_search($data, $elements) === FALSE) {
153 1
                unset($this->data[$index]);
154
155 1
                $result = TRUE;
156 1
            }
157 1
        }
158
159 1
        return $result;
160
    }
161
162
}
163