john-wilkinson /
collections
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Jmw\Collection\Map; |
||
| 3 | |||
| 4 | use Jmw\Collection\Set\SetAbstract; |
||
| 5 | use Jmw\Collection\Lists\ArrayList; |
||
| 6 | use Jmw\Collection\Set\TupleSet; |
||
| 7 | use Jmw\Collection\Set\ArraySet; |
||
| 8 | use Jmw\Collection\Exception\UnsupportedOperationException; |
||
| 9 | /** |
||
| 10 | * Hash table based implementation of the Map interface. |
||
| 11 | * This implementation provides all of the optional map operations, |
||
| 12 | * and permits null values and the null key. |
||
| 13 | * @author john |
||
| 14 | * |
||
| 15 | */ |
||
| 16 | class HashMap implements MapInterface |
||
| 17 | { |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $array; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructs a HashTable. A standard php array may be passed in as starting data. |
||
| 25 | * HashMap only uses a copy of the passed in array, so changes made by the HashMap |
||
| 26 | * will NOT be reflected in the original array, and vice-versa |
||
| 27 | * @param array $array |
||
| 28 | */ |
||
| 29 | public function __construct($array = null) |
||
| 30 | { |
||
| 31 | $this->clear(); |
||
| 32 | |||
| 33 | if($array !== null) |
||
| 34 | { |
||
| 35 | $this->array = $array; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Removes all of the mappings from this map. |
||
| 41 | */ |
||
| 42 | public function clear() |
||
| 43 | { |
||
| 44 | $this->array = []; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Returns true if this map contains a mapping for the specified key. |
||
| 49 | * @param multitype $key |
||
| 50 | * @return boolean |
||
| 51 | */ |
||
| 52 | public function containsKey($key) |
||
| 53 | { |
||
| 54 | return array_key_exists($key, $this->array); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Returns true if this map maps one or more keys to the specified value. |
||
| 59 | * @param unknown $value |
||
| 60 | * @return boolean |
||
| 61 | */ |
||
| 62 | public function containsValue($value) |
||
| 63 | { |
||
| 64 | return in_array($value, $this->array); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns a Set view of the mappings contained in this map. |
||
| 69 | * @return SetInterface |
||
| 70 | */ |
||
| 71 | public function entrySet() |
||
| 72 | { |
||
| 73 | return new TupleSet($this->array); |
||
|
0 ignored issues
–
show
$this->array is of type array, but the function expects a object<Jmw\Collection\Set\unknown>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Compares the specified object with this map for equality. |
||
| 78 | * @return boolean |
||
| 79 | */ |
||
| 80 | public function equals($object) |
||
| 81 | { |
||
| 82 | return $this->hashCode() === spl_object_hash($object); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. |
||
| 87 | * @param multitype $key |
||
| 88 | * @return multitype |
||
| 89 | */ |
||
| 90 | public function get($key) |
||
| 91 | { |
||
| 92 | if(!array_key_exists($key, $this->array)) |
||
| 93 | { |
||
| 94 | return null; |
||
| 95 | } |
||
| 96 | return $this->array[$key]; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the hash code value for this map. |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function hashCode() |
||
| 104 | { |
||
| 105 | return spl_object_hash($this); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns true if this map contains no key-value mappings. |
||
| 110 | * @return boolean |
||
| 111 | */ |
||
| 112 | public function isEmpty() |
||
| 113 | { |
||
| 114 | return empty($this->array); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns a Set view of the keys contained in this map. |
||
| 119 | * @return SetInterface |
||
| 120 | */ |
||
| 121 | public function keySet() |
||
| 122 | { |
||
| 123 | return new ArraySet(array_keys($this->array)); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Associates the specified value with the specified key in this map (optional operation). |
||
| 128 | * @param multitype $key |
||
| 129 | * @param multitype $value |
||
| 130 | * @return multitype |
||
| 131 | */ |
||
| 132 | public function put($key, $value) |
||
| 133 | { |
||
| 134 | $this->array[$key] = $value; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Copies all of the mappings from the specified map to this map (optional operation). |
||
| 139 | * @param MapInterface $map |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function putAll(MapInterface $map) |
||
| 143 | { |
||
| 144 | foreach($map->array as $key=>$value) |
||
|
0 ignored issues
–
show
Accessing
array on the interface Jmw\Collection\Map\MapInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
| 145 | { |
||
| 146 | $this->put($key, $value); |
||
|
0 ignored issues
–
show
$key is of type integer|string, but the function expects a object<Jmw\Collection\Map\multitype>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Removes the mapping for a key from this map if it is present |
||
| 152 | * It returns the previous value associated with key, |
||
| 153 | * or null if there was no mapping for key. |
||
| 154 | * (A null return can also indicate that the map previously associated null with key.) |
||
| 155 | * @param unknown $key |
||
| 156 | * @return multitype $value | null |
||
| 157 | */ |
||
| 158 | public function remove($key) |
||
| 159 | { |
||
| 160 | if($this->containsKey($key)) |
||
| 161 | { |
||
| 162 | $value = $this->array[$key]; |
||
| 163 | unset($this->array[$key]); |
||
| 164 | } |
||
| 165 | else |
||
| 166 | { |
||
| 167 | $value = null; |
||
| 168 | } |
||
| 169 | |||
| 170 | return $value; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the number of key-value mappings in this map. |
||
| 175 | * @return int |
||
| 176 | */ |
||
| 177 | public function size() |
||
| 178 | { |
||
| 179 | return count($this->array); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns a Collection view of the values contained in this map. |
||
| 184 | * @return CollectionInterface |
||
| 185 | */ |
||
| 186 | public function values() |
||
| 187 | { |
||
| 188 | return new ArrayList(array_values($this->array)); |
||
| 189 | } |
||
| 190 | |||
| 191 | /********************************************* |
||
| 192 | ** Array Access Methods |
||
| 193 | *********************************************/ |
||
| 194 | |||
| 195 | /** |
||
| 196 | * |
||
| 197 | * {@inheritDoc} |
||
| 198 | * @see ArrayAccess::offsetExists() |
||
| 199 | */ |
||
| 200 | public function offsetExists($offset) |
||
| 201 | { |
||
| 202 | return $this->containsKey($offset); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * |
||
| 207 | * {@inheritDoc} |
||
| 208 | * @see ArrayAccess::offsetGet() |
||
| 209 | */ |
||
| 210 | public function offsetGet($offset) |
||
| 211 | { |
||
| 212 | return $this->get($offset); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * |
||
| 217 | * {@inheritDoc} |
||
| 218 | * @see ArrayAccess::offsetSet() |
||
| 219 | */ |
||
| 220 | public function offsetSet($offset, $value) |
||
| 221 | { |
||
| 222 | if($offset === null) { |
||
| 223 | throw new UnsupportedOperationException('append'); |
||
| 224 | } else { |
||
| 225 | $this->put($offset, $value); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * |
||
| 231 | * {@inheritDoc} |
||
| 232 | * @see ArrayAccess::offsetUnset() |
||
| 233 | */ |
||
| 234 | public function offsetUnset($offset) |
||
| 235 | { |
||
| 236 | $this->remove($offset); |
||
| 237 | } |
||
| 238 | |||
| 239 | } |