Completed
Push — master ( 6bd4a8...c4104c )
by Ducatel
03:37
created

Set::offsetExists()   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 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    public function offsetExists($offset)
37
    {
38
        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
    public function offsetGet($offset)
54
    {
55
        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
    public function offsetSet($offset, $value)
70
    {
71
        if ($this->contains($value)) {
72
            throw new NoDuplicateAllowedException("Object already inserted");
73
        }
74
75 View Code Duplication
        if (is_null($offset)) {
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...
76
            $this->data[] = $value;
77
        } else {
78
            $this->data[$offset] = $value;
79
        }
80
    }
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
    public function offsetUnset($offset)
95
    {
96
        unset($this->data[$offset]);
97
    }
98
}
99