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
|
9 |
|
public function add($object, $key = null) |
27
|
|
|
{ |
28
|
9 |
|
if (call_user_func($this->validateTypeFct, $object) === false) { |
29
|
8 |
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
9 |
|
if (is_null($key)) { |
33
|
8 |
|
$this->data[] = $object; |
34
|
|
|
} else { |
35
|
1 |
|
$this->data[$key] = $object; |
36
|
|
|
} |
37
|
|
|
|
38
|
9 |
|
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"); |
|
|
|
|
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
|
2 |
|
public function offsetGet($offset) |
98
|
|
|
{ |
99
|
2 |
|
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
|
2 |
|
public function offsetSet($offset, $value) |
114
|
|
|
{ |
115
|
2 |
|
if (call_user_func($this->validateTypeFct, $value) === false) { |
116
|
1 |
|
throw new \TypeError("Object cannot be added. Type not managed by this collection"); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
View Code Duplication |
if (is_null($offset)) { |
|
|
|
|
120
|
1 |
|
$this->data[] = $value; |
121
|
|
|
} else { |
122
|
2 |
|
$this->data[$offset] = $value; |
123
|
|
|
} |
124
|
2 |
|
} |
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
|
|
|
|
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.