Completed
Push — master ( c4104c...a5b86a )
by Ducatel
04:35
created

Set   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 6.17 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
lcom 1
cbo 2
dl 5
loc 81
ccs 14
cts 14
cp 1
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 5 12 3
A offsetUnset() 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
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
    public function offsetSet($offset, $value)
70
    {
71 3
        if ($this->contains($value)) {
72 2
            throw new NoDuplicateAllowedException("Object already inserted");
73
        }
74
75 3 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 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