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 CommonContainerMethodsTrait 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 CommonContainerMethodsTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | trait CommonContainerMethodsTrait |
||
| 28 | { |
||
| 29 | use GuardTrait; |
||
| 30 | /** |
||
| 31 | * @var ComparerInterface |
||
| 32 | */ |
||
| 33 | private $defaultComparer; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Gets the default comparer for this collection |
||
| 37 | * @return ComparerInterface |
||
| 38 | */ |
||
| 39 | public function getDefaultComparer() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Sets the default comparer for this collection |
||
| 50 | * @param ComparerInterface $defaultComparer |
||
| 51 | * @return $this |
||
| 52 | */ |
||
| 53 | public function setDefaultComparer(ComparerInterface $defaultComparer) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | public function __toString() |
||
| 67 | |||
| 68 | public function equals($obj) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | public function serialize() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public function unserialize($serialized) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function jsonSerialize() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | View Code Duplication | public function toValuesArray() |
|
| 115 | |||
| 116 | public function toKeysArray() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function toArray() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public static function fromArray(array $arr) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | public function toVector() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | public function toImmVector() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | public function toSet() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | public function toImmSet() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | public function toMap() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | public function toImmMap() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritDoc} |
||
| 210 | * @return $this |
||
| 211 | */ |
||
| 212 | public function map(callable $callable) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritDoc} |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | public function mapWithKey($callback) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritDoc} |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | public function filter(callable $callable) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritDoc} |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function filterWithKey($callback) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritDoc} |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | View Code Duplication | public function zip(Iterable $iterable) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritDoc} |
||
| 263 | * @return $this |
||
| 264 | */ |
||
| 265 | public function take($size = 1) |
||
| 273 | |||
| 274 | public function takeWhile(callable $callable) |
||
| 278 | |||
| 279 | public function skip($n) |
||
| 287 | |||
| 288 | public function skipWhile(callable $callable) |
||
| 292 | |||
| 293 | public function slice($start, $length) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * {@inheritDoc} |
||
| 307 | * @return $this |
||
| 308 | */ |
||
| 309 | public function first() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritDoc} |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function last() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritDoc} |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function each(callable $callable) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * {@inheritdoc} |
||
| 348 | */ |
||
| 349 | public function exists(callable $fn) |
||
| 359 | |||
| 360 | View Code Duplication | public function concatAll() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * {@inheritDoc} |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function groupBy($callback) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * {@inheritDoc} |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | public function indexBy($callback) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * {@inheritdoc} |
||
| 411 | */ |
||
| 412 | public function reduce(callable $callback, $initial = null) |
||
| 420 | } |
||
| 421 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: