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 Collection 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 Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | class Collection extends ArrayCollection |
||
| 52 | { |
||
| 53 | /** @var ResolverInterface */ |
||
| 54 | private $resolver; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param VersionInterface[]|\Traversable $versions |
||
| 58 | * @param ResolverInterface $resolver |
||
|
|
|||
| 59 | * |
||
| 60 | * @throws AlreadyExistsException |
||
| 61 | * @throws InvalidArgumentException |
||
| 62 | * @throws CollectionException |
||
| 63 | */ |
||
| 64 | 156 | public function __construct( |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @return ResolverInterface |
||
| 92 | */ |
||
| 93 | 118 | final protected function getResolver() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Gets an element. |
||
| 100 | * |
||
| 101 | * @param mixed $key If an alias is given then it will be resolved to an element. Otherwise the $key will be used |
||
| 102 | * to fetch the element by index. |
||
| 103 | * @param bool $resolve Whether to use the resolver or not. |
||
| 104 | * |
||
| 105 | * @return VersionInterface|null Null if not present |
||
| 106 | */ |
||
| 107 | 98 | public function get($key, $resolve = true) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Gets a version by id |
||
| 128 | * |
||
| 129 | * @param $id |
||
| 130 | * |
||
| 131 | * @return VersionInterface |
||
| 132 | */ |
||
| 133 | 44 | public function getById($id) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the index of the version that has the given id. Returns null if not found. |
||
| 141 | * |
||
| 142 | * @param string $id |
||
| 143 | * |
||
| 144 | * @return int|null |
||
| 145 | */ |
||
| 146 | 68 | public function indexOfId($id) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Resolves an alias in to a version |
||
| 177 | * |
||
| 178 | * @param string $alias |
||
| 179 | * |
||
| 180 | * @return VersionInterface|null |
||
| 181 | */ |
||
| 182 | 88 | protected function resolve($alias) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Returns whether the key exists in the collection. |
||
| 189 | * |
||
| 190 | * @param $index |
||
| 191 | * @param bool $resolve |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | 8 | public function has($index, $resolve = true) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Returns true if the specified version is valid (can be added) to the collection. Otherwise, it MUST throw |
||
| 202 | * an exception. |
||
| 203 | * |
||
| 204 | * @param VersionInterface $version |
||
| 205 | * |
||
| 206 | * @return bool |
||
| 207 | * |
||
| 208 | * @throws AlreadyExistsException |
||
| 209 | * @throws CollectionException |
||
| 210 | */ |
||
| 211 | 141 | public function validate(VersionInterface $version) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * invalidateResolverCache |
||
| 225 | */ |
||
| 226 | 42 | protected function invalidateResolverCache() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Add a version to the collection |
||
| 233 | * |
||
| 234 | * @param mixed $element |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | * |
||
| 238 | * @throws CollectionException |
||
| 239 | * @throws InvalidArgumentException |
||
| 240 | */ |
||
| 241 | 42 | public function add($element) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param $key |
||
| 265 | * |
||
| 266 | * @return VersionInterface|null |
||
| 267 | */ |
||
| 268 | public function remove($key) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Adds a new version to the collection if it doesn't exist or replaces it if it does. |
||
| 284 | * |
||
| 285 | * @param VersionInterface $version |
||
| 286 | */ |
||
| 287 | public function addOrReplace(VersionInterface $version) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns a new collection with "enriched" elements based on the information provided in the parameter. |
||
| 298 | * An "enriched" Version is one that was originally not linked and now is linked, not migrated and now is migrated, |
||
| 299 | * or both. |
||
| 300 | * |
||
| 301 | * @param Collection $versions |
||
| 302 | * |
||
| 303 | * @return static |
||
| 304 | * |
||
| 305 | * @throws CollectionException |
||
| 306 | * @throws InvalidArgumentException |
||
| 307 | */ |
||
| 308 | public function hydrate(Collection $versions) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Merges another set into this one, replacing versions that exist and adding those that don't. |
||
| 331 | * |
||
| 332 | * @param Collection $collection |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function merge(Collection $collection) |
||
| 343 | } |
||
| 344 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.