TypedArray::delete()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 2
b 0
f 1
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Ducatel\PHPCollection;
4
5
/**
6
 * This class represent a array which can contains only one type of object.
7
 * Characteristics:
8
 *
9
 *     - Values: Typed value, duplicates allowed
10
 *     - Ordering: same as input unless when explicitly sorted
11
 *
12
 * @package Ducatel\PHPCollection
13
 * @author  D.Ducatel
14
 */
15
class TypedArray extends Base\AbstractTypedCollection implements \ArrayAccess
16
{
17
    /**
18
     * Add an object to this collection
19
     *
20
     * @param object $object The object you want to add
21
     *
22
     * @param null|object $key The key where object will be stored. Null if you won't to use this as associative array
23
     *
24
     * @return True when added with success, else false
25
     */
26 10
    public function add($object, $key = null)
27
    {
28 10
        if ($this->validateType($object) === false) {
29 8
            return false;
30
        }
31
32 10
        if (is_null($key)) {
33 9
            $this->data[] = $object;
34
        } else {
35 1
            $this->data[$key] = $object;
36
        }
37
38 10
        return true;
39
    }
40
41
    /**
42
     * Add an object to this collection
43
     *
44
     * @param Integer $key The key (index) of object in the collection.
45
     *
46
     * @return false|object The deleted object or false if not found
47
     * @throws \TypeError Throw when key is not an integer
48
     */
49 2
    public function delete($key)
50
    {
51 2
        if (is_integer($key) === false) {
52 1
            throw new \TypeError("The key must be an integer");
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with 'The key must be an integer'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
53
        }
54
55 1
        $result = array_splice($this->data, $key, 1);
56 1
        if (count($result) == 0) {
57 1
            return false;
58
        }
59
60 1
        return $result[0];
61
    }
62
63
64
65
    /**
66
     * Whether a offset exists
67
     *
68
     * @link  http://php.net/manual/en/arrayaccess.offsetexists.php
69
     *
70
     * @param mixed $offset <p>
71
     *                      An offset to check for.
72
     *                      </p>
73
     *
74
     * @return boolean true on success or false on failure.
75
     * </p>
76
     * <p>
77
     * The return value will be casted to boolean if non-boolean was returned.
78
     * @since 5.0.0
79
     */
80 2
    public function offsetExists($offset)
81
    {
82 2
        return isset($this->data[$offset]);
83
    }
84
85
    /**
86
     * Offset to retrieve
87
     *
88
     * @link  http://php.net/manual/en/arrayaccess.offsetget.php
89
     *
90
     * @param mixed $offset <p>
91
     *                      The offset to retrieve.
92
     *                      </p>
93
     *
94
     * @return mixed Can return all value types.
95
     * @since 5.0.0
96
     */
97 4
    public function offsetGet($offset)
98
    {
99 4
        return $this->data[$offset];
100
    }
101
102
    /**
103
     * Offset to set
104
     *
105
     * @link  http://php.net/manual/en/arrayaccess.offsetset.php
106
     *
107
     * @param mixed $offset The offset to assign the value to.
108
     * @param mixed $value  The value to set.
109
     *
110
     * @throws \TypeError When try to add type not managed by this collection
111
     * @since 5.0.0
112
     */
113 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...
114
    {
115 3
        if ($this->validateType($value) === false) {
116 1
            throw new \TypeError("Object cannot be added. Type not managed by this collection");
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with 'Object cannot be added....ged by this collection'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
117
        }
118
119 3
        if (is_null($offset)) {
120 2
            $this->data[] = $value;
121
        } else {
122 2
            $this->data[$offset] = $value;
123
        }
124 3
    }
125
126
    /**
127
     * Offset to unset
128
     *
129
     * @link  http://php.net/manual/en/arrayaccess.offsetunset.php
130
     *
131
     * @param mixed $offset <p>
132
     *                      The offset to unset.
133
     *                      </p>
134
     *
135
     * @return void
136
     * @since 5.0.0
137
     */
138 1
    public function offsetUnset($offset)
139
    {
140 1
        unset($this->data[$offset]);
141 1
    }
142
}
143