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:
Complex classes like ArrayList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ArrayList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ArrayList extends CollectionAbstract implements ListInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Underlying data store |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $array; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructs an ArrayList |
||
| 26 | * Defaults with the given array |
||
| 27 | * @param array $array |
||
| 28 | */ |
||
| 29 | public function __construct($array = []) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Appends the specified element to the end of this list. |
||
| 40 | * @param multitype $element |
||
| 41 | * @return void |
||
| 42 | */ |
||
| 43 | public function add($element) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Inserts the specified element at the specified position in this list. |
||
| 50 | * @param int $index |
||
| 51 | * @param multitype $element |
||
| 52 | * @return void |
||
| 53 | */ |
||
| 54 | public function addAt($index, $element) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Removes all of the elements from this list. |
||
| 63 | * @return void |
||
| 64 | */ |
||
| 65 | public function clear() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Compares the specified object with this list for equality. |
||
| 72 | * @param multitype $object |
||
| 73 | * @return boolean |
||
| 74 | */ |
||
| 75 | public function equals($object) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the element at the specified position in this list. |
||
| 94 | * @param int $index |
||
| 95 | * @return multitype |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function get($index) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Returns the index of the first occurrence of the |
||
| 109 | * specified element in this list, |
||
| 110 | * or -1 if this list does not contain the element. |
||
| 111 | * @param multitype $element |
||
| 112 | * @return int |
||
| 113 | */ |
||
| 114 | public function indexOf($element) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns an iterator over the elements in this list in proper sequence. |
||
| 125 | * @return IteratorInterface |
||
| 126 | */ |
||
| 127 | public function iterator() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns the index of the last occurrence of the |
||
| 134 | * specified element in this list, |
||
| 135 | * or -1 if this list does not contain the element. |
||
| 136 | * @param multitype $element |
||
| 137 | * @return int |
||
| 138 | */ |
||
| 139 | public function lastIndexOf($element) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns a list iterator over the elements |
||
| 157 | * in this list (in proper sequence). |
||
| 158 | * @return ListIterator |
||
| 159 | */ |
||
| 160 | public function listIterator() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns a list iterator over the elements in this list (in proper sequence), |
||
| 167 | * starting at the specified position in the list. |
||
| 168 | * @param int $index |
||
| 169 | * @return ListIterator |
||
| 170 | */ |
||
| 171 | public function listIteratorAt($index) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Removes the first occurrence of the specified element |
||
| 178 | * from this list, if it is present |
||
| 179 | * @param multitype $element |
||
| 180 | * @return boolean |
||
| 181 | * @throws IndexOutOfBoundsException |
||
| 182 | */ |
||
| 183 | public function remove($element) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Removes from this list all of its elements that |
||
| 197 | * are contained in the specified collection |
||
| 198 | * @param CollectionInterface $collection |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | public function removeAll(CollectionInterface $collection) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Removes the element at the specified |
||
| 208 | * position in this list (optional operation). |
||
| 209 | * @param int $index |
||
| 210 | * @return boolean |
||
| 211 | * @throws IndexOutOfBoundsException |
||
| 212 | */ |
||
| 213 | public function removeAt($index) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Removes from this list all of the elements whose index is between |
||
| 230 | * from, inclusive, and to, exclusive. |
||
| 231 | * @param int $from |
||
| 232 | * @param int $to |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | public function removeRange($from, $to) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Replaces the element at the specified position |
||
| 242 | * in this list with the specified element. |
||
| 243 | * @param int $index |
||
| 244 | * @param int $element |
||
| 245 | * @return void |
||
| 246 | * @throws IndexOutOfBoundsException |
||
| 247 | */ |
||
| 248 | View Code Duplication | public function set($index, $element) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Returns the number of elements in this list. |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | public function size() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns a view of the portion of this list between the |
||
| 271 | * specified from, inclusive, and to, exclusive. |
||
| 272 | * @param int $from |
||
| 273 | * @param int $to |
||
| 274 | * @return ArrayList |
||
| 275 | */ |
||
| 276 | public function subList($from, $to) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns an array containing all of the elements in this list |
||
| 285 | * in proper sequence (from first to last element). |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function toArray() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Returns the hash code value for this collection. |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function hashCode() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Checks to see if a variable is an int. |
||
| 304 | * This is meant to help ensure that all indexes |
||
| 305 | * remain integers. |
||
| 306 | * @param int $var |
||
| 307 | * @throws InvalidTypeException |
||
| 308 | */ |
||
| 309 | protected function checkInt($var) |
||
| 316 | |||
| 317 | /********************************************* |
||
| 318 | ** Array Access Methods |
||
| 319 | *********************************************/ |
||
| 320 | |||
| 321 | /** |
||
| 322 | * |
||
| 323 | * {@inheritDoc} |
||
| 324 | * @see ArrayAccess::offsetExists() |
||
| 325 | */ |
||
| 326 | View Code Duplication | public function offsetExists($offset) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * |
||
| 337 | * {@inheritDoc} |
||
| 338 | * @see ArrayAccess::offsetGet() |
||
| 339 | */ |
||
| 340 | public function offsetGet($offset) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * |
||
| 347 | * {@inheritDoc} |
||
| 348 | * @see ArrayAccess::offsetSet() |
||
| 349 | */ |
||
| 350 | public function offsetSet($offset, $value) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * |
||
| 361 | * {@inheritDoc} |
||
| 362 | * @see ArrayAccess::offsetUnset() |
||
| 363 | */ |
||
| 364 | public function offsetUnset($offset) |
||
| 368 | } |
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: