Set::offsetSet()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Ducatel\PHPCollection;
4
5
use Ducatel\PHPCollection\Base\AbstractCollection;
6
use Ducatel\PHPCollection\Exception\NoDuplicateAllowedException;
7
8
/**
9
 * This class represent a array which cannot contains duplicate
10
 * Characteristics:
11
 *
12
 *     - Values: anything, duplicates not allowed
13
 *     - Ordering: same as input unless when explicitly sorted
14
 *
15
 * @package Ducatel\PHPCollection
16
 * @author  D.Ducatel
17
 */
18
class Set extends AbstractCollection implements \ArrayAccess
19
{
20
21
    /**
22
     * Whether a offset exists
23
     *
24
     * @link  http://php.net/manual/en/arrayaccess.offsetexists.php
25
     *
26
     * @param mixed $offset <p>
27
     *                      An offset to check for.
28
     *                      </p>
29
     *
30
     * @return boolean true on success or false on failure.
31
     * </p>
32
     * <p>
33
     * The return value will be casted to boolean if non-boolean was returned.
34
     * @since 5.0.0
35
     */
36 1
    public function offsetExists($offset)
37
    {
38 1
        return isset($this->data[$offset]);
39
    }
40
41
    /**
42
     * Offset to retrieve
43
     *
44
     * @link  http://php.net/manual/en/arrayaccess.offsetget.php
45
     *
46
     * @param mixed $offset <p>
47
     *                      The offset to retrieve.
48
     *                      </p>
49
     *
50
     * @return mixed Can return all value types.
51
     * @since 5.0.0
52
     */
53 1
    public function offsetGet($offset)
54
    {
55 1
        return $this->data[$offset];
56
    }
57
58
    /**
59
     * Offset to set
60
     *
61
     * @link  http://php.net/manual/en/arrayaccess.offsetset.php
62
     *
63
     * @param mixed $offset The offset to assign the value to.
64
     * @param mixed $value  The value to set.
65
     *
66
     * @throws NoDuplicateAllowedException When try to add an object already added
67
     * @since 5.0.0
68
     */
69 3 View Code Duplication
    public function offsetSet($offset, $value)
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...
70
    {
71 3
        if ($this->contains($value)) {
72 2
            throw new NoDuplicateAllowedException("Object already inserted");
73
        }
74
75 3
        if (is_null($offset)) {
76 1
            $this->data[] = $value;
77
        } else {
78 2
            $this->data[$offset] = $value;
79
        }
80 3
    }
81
82
    /**
83
     * Offset to unset
84
     *
85
     * @link  http://php.net/manual/en/arrayaccess.offsetunset.php
86
     *
87
     * @param mixed $offset <p>
88
     *                      The offset to unset.
89
     *                      </p>
90
     *
91
     * @return void
92
     * @since 5.0.0
93
     */
94 1
    public function offsetUnset($offset)
95
    {
96 1
        unset($this->data[$offset]);
97 1
    }
98
}
99