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 |
||
| 39 | class Dictionary extends Object implements SetInterface |
||
| 40 | { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Holds the keys with the objects. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $keys = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Holds the items of the Dictionary |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $items = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Holds the internal counter for the keys. |
||
| 58 | * |
||
| 59 | * @var integer |
||
| 60 | */ |
||
| 61 | protected $key = 0; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Standard constructor that adds the values of the array passed |
||
| 65 | * as parameter to the internal member variable. |
||
| 66 | * |
||
| 67 | * @param array $items An array to initialize the Dictionary |
||
| 68 | * |
||
| 69 | * @throws \AppserverIo\Lang\ClassCastException Is thrown if nor NULL or an object that is not a Dictionary is passed |
||
| 70 | * @see \AppserverIo\Collections\Dictionary::add($key, $value) |
||
| 71 | */ |
||
| 72 | public function __construct($items = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * This method adds the passed value with the passed key |
||
| 92 | * to the Dictionary. |
||
| 93 | * |
||
| 94 | * @param object $key Holds the key as object to add the value under |
||
| 95 | * @param mixed $value Holds the value to add |
||
| 96 | * |
||
| 97 | * @return \AppserverIo\Collections\Dictionary The instance |
||
| 98 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
||
| 99 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key OR value are NULL |
||
| 100 | */ |
||
| 101 | public function add($key, $value) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * This method returns the element with the passed key |
||
| 125 | * from the Dictionary. |
||
| 126 | * |
||
| 127 | * @param object $key Holds the key of the element to return |
||
| 128 | * |
||
| 129 | * @return mixed The requested element |
||
| 130 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
||
| 131 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key OR value are NULL |
||
| 132 | * @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Dictionary |
||
| 133 | */ |
||
| 134 | public function get($key) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * This method initializes the Collection and removes |
||
| 158 | * all exiting entries. |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | * @see \AppserverIo\Collections\CollectionInterface::clear() |
||
| 162 | */ |
||
| 163 | public function clear() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * This method returns the number of entries of the Collection. |
||
| 173 | * |
||
| 174 | * @return integer The number of entries |
||
| 175 | */ |
||
| 176 | public function size() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * This method checks if the element with the passed |
||
| 183 | * key exists in the Dictionary. |
||
| 184 | * |
||
| 185 | * @param object $key Holds the key to check the elements of the Dictionary for |
||
| 186 | * |
||
| 187 | * @return boolean Returns true if an element with the passed key exists in the Dictionary |
||
| 188 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
||
| 189 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
||
| 190 | */ |
||
| 191 | View Code Duplication | public function exists($key) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * This returns true if the Collection has no |
||
| 213 | * entries, otherwise false. |
||
| 214 | * |
||
| 215 | * @return boolean |
||
| 216 | * @see \AppserverIo\Collections\CollectionInterface::isEmpty() |
||
| 217 | */ |
||
| 218 | public function isEmpty() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * This method returns an array with the |
||
| 229 | * items of the Dictionary. |
||
| 230 | * |
||
| 231 | * The keys are lost in the array. |
||
| 232 | * |
||
| 233 | * @return array Holds an array with the items of the Dictionary |
||
| 234 | * @see \AppserverIo\Collections\CollectionInterface::toArray() |
||
| 235 | */ |
||
| 236 | public function toArray() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * This method returns the keys as |
||
| 243 | * an array. |
||
| 244 | * |
||
| 245 | * @return array Holds an array with the keys |
||
| 246 | */ |
||
| 247 | public function keysToArray() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * This method removes the element with the passed |
||
| 254 | * key, that has to be an object, from the Dictionary. |
||
| 255 | * |
||
| 256 | * @param object $key Holds the key of the element to remove |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
||
| 260 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
||
| 261 | * @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Dictionary |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function remove($key) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * This method appends all elements of the |
||
| 287 | * passed array to the Dictionary. |
||
| 288 | * |
||
| 289 | * @param array $array Holds the array with the values to add |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | public function addAll($array) |
||
| 299 | } |
||
| 300 |
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: