Completed
Push — master ( 1afeda...28f8e2 )
by Zoltán
04:50
created

Set::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
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 Set extends AbstractCollection implements SetInterface {
21
22
    /**
23
     * Set constructor.
24
     *
25
     * @param null|array $values
26
     */
27 27
    public function __construct($values = NULL) {
28 27
        if(is_array($values)) {
29 7
            $this->addAll($values);
30 7
        }
31 27
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 6
    public function equals(SetInterface $collection) {
37
        //First check the size, if this not equals the tow collection
38
        //not be identical
39 6
        if($collection->size() !== $this->size()) {
40 1
            return FALSE;
41
        }
42
43
        //Use strict comparison to check arrays are equals
44
        //(Values and orders)
45 5
        return $collection->toArray() === $this->data;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 27
    public function add($element) {
52 27
        if(!is_scalar($element)) {
53 4
            throw CollectionException::nonScalarTypeGiven(gettype($element));
54
        }
55
56 23
        if(!$this->contains($element)) {
57 23
            $this->data[] = $element;
58 23
        }
59 23
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 21
    public function addAll($elements) {
65 21
        $elements = $this->collectionToArray($elements);
66
67 21
        foreach($elements as $item) {
68 21
            $this->add($item);
69 21
        }
70 21
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public function remove($element) {
76 2
        if(($index = array_search($element, $this->data)) !== FALSE) {
77 2
            unset($this->data[$index]);
78
79 2
            return TRUE;
80
        }
81
82 2
        return FALSE;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 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...
89 1
        $elements = $this->collectionToArray($elements);
90
91 1
        $result = FALSE;
92
93 1
        foreach($elements as $item) {
94 1
            if($this->remove($item) === TRUE) {
95 1
                $result = TRUE;
96 1
            }
97 1
        }
98
99 1
        return $result;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    public function retainAll($elements) {
106 1
        $elements = $this->collectionToArray($elements);
107
108 1
        $result = FALSE;
109
110 1
        foreach($this->data as $index => $data) {
111 1
            if(array_search($data, $elements) === FALSE) {
112 1
                unset($this->data[$index]);
113
114 1
                $result = TRUE;
115 1
            }
116 1
        }
117
118 1
        return $result;
119
    }
120
121
}
122