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 RowCollection 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 RowCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class RowCollection extends BaseRow implements ArrayAccess, Iterator, Countable |
||
| 13 | { |
||
| 14 | private $rows = []; |
||
| 15 | private $idAsKey = true; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Magic method to set properties to all rows. |
||
| 19 | * |
||
| 20 | * @see self::set() |
||
| 21 | */ |
||
| 22 | public function __set($name, $value) |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Magic method to get properties from all rows. |
||
| 29 | * |
||
| 30 | * @see self::get() |
||
| 31 | */ |
||
| 32 | public function __get($name) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set whether or not use the id as key. |
||
| 82 | * |
||
| 83 | * @param bool $idAsKey |
||
| 84 | * |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | public function idAsKey($idAsKey) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Magic method to print the row values (and subvalues). |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public function __toString() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @see ArrayAccess |
||
| 106 | */ |
||
| 107 | public function offsetSet($offset, $value) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @see ArrayAccess |
||
| 128 | */ |
||
| 129 | public function offsetExists($offset) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @see ArrayAccess |
||
| 136 | */ |
||
| 137 | public function offsetUnset($offset) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @see ArrayAccess |
||
| 144 | */ |
||
| 145 | public function offsetGet($offset) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @see Iterator |
||
| 152 | */ |
||
| 153 | public function rewind() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @see Iterator |
||
| 160 | */ |
||
| 161 | public function current() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @see Iterator |
||
| 168 | */ |
||
| 169 | public function key() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @see Iterator |
||
| 176 | */ |
||
| 177 | public function next() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @see Iterator |
||
| 184 | */ |
||
| 185 | public function valid() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @see Countable |
||
| 192 | */ |
||
| 193 | public function count() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | public function toArray($idAsKey = false, array $parentEntities = array()) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Set values to all children. |
||
| 218 | * |
||
| 219 | * @param string $name |
||
| 220 | * @param string $value |
||
| 221 | * |
||
| 222 | * @return self |
||
| 223 | */ |
||
| 224 | public function set($name, $value) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns one or all values of the collections. |
||
| 235 | * |
||
| 236 | * @param string $name The value name. If it's not defined returns all values |
||
| 237 | * @param string $key The parameter name used for the keys. If it's not defined, returns a numeric array |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public function get($name = null, $key = null) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns a slice of the content. |
||
| 286 | * |
||
| 287 | * @param int $offset |
||
| 288 | * @param int|null|true $length |
||
| 289 | * |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function slice($offset = null, $length = null) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Add new values to the collection. |
||
| 303 | * |
||
| 304 | * @param array|RowInterface $rows The new rows |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function add($rows) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Filter the rows by a value. |
||
| 323 | * |
||
| 324 | * @param string $name The value name |
||
| 325 | * @param mixed $value The value to filter |
||
| 326 | * @param bool $strict Strict mode |
||
| 327 | * |
||
| 328 | * @return RowCollection |
||
| 329 | */ |
||
| 330 | public function filter($name, $value, $strict = true) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Find a row by a value. |
||
| 345 | * |
||
| 346 | * @param string $name The value name |
||
| 347 | * @param mixed $value The value to filter |
||
| 348 | * @param bool $strict Strict mode |
||
| 349 | * |
||
| 350 | * @return Row|null The rows found |
||
| 351 | */ |
||
| 352 | public function find($name, $value, $strict = true) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Distribute a rowcollection througth all rows. |
||
| 363 | * |
||
| 364 | * @param RowCollection $rows |
||
| 365 | * |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | public function joinMany(RowCollection $rows) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Distribute a rowcollection througth all rows. |
||
| 397 | * Its the opposite of $this->joinMany(). |
||
| 398 | * |
||
| 399 | * @param RowCollection $rows |
||
| 400 | * |
||
| 401 | * @return $this |
||
| 402 | */ |
||
| 403 | public function joinOne(RowCollection $rows) |
||
| 409 | } |
||
| 410 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: