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 AbstractCollection 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 AbstractCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class AbstractCollection extends Arrayy implements CollectionInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The type of elements stored in this collection. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | private $collectionType; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Constructs a collection object of the specified type, optionally with the |
||
| 24 | * specified data. |
||
| 25 | * |
||
| 26 | * @param mixed $data |
||
| 27 | * <p> |
||
| 28 | * The initial items to store in the collection. |
||
| 29 | * </p> |
||
| 30 | * <p> |
||
| 31 | * Should be an array or a generator, otherwise it will try |
||
| 32 | * to convert it into an array. |
||
| 33 | * </p> |
||
| 34 | */ |
||
| 35 | 11 | public function __construct($data = []) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @return static[] |
||
| 43 | */ |
||
| 44 | 3 | public function getCollection(): array |
|
| 48 | |||
| 49 | /** |
||
| 50 | * The type (FQCN) associated with this collection. |
||
| 51 | * |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | abstract public function getType(): string; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Merge current items and items of given collections into a new one. |
||
| 58 | * |
||
| 59 | * @param CollectionInterface ...$collections The collections to merge. |
||
| 60 | * |
||
| 61 | * @throws \InvalidArgumentException if any of the given collections are not of the same type |
||
| 62 | * |
||
| 63 | * @return CollectionInterface |
||
| 64 | */ |
||
| 65 | 1 | public function merge(CollectionInterface ...$collections): CollectionInterface |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Assigns a value to the specified offset + check the type. |
||
| 78 | * |
||
| 79 | * @param int|string|null $offset |
||
| 80 | * @param mixed $value |
||
| 81 | */ |
||
| 82 | 1 | View Code Duplication | public function offsetSet($offset, $value) |
| 92 | |||
| 93 | /** |
||
| 94 | * Prepend a (key) + value to the current array. |
||
| 95 | * |
||
| 96 | * @param mixed $value |
||
| 97 | * @param mixed $key |
||
| 98 | * |
||
| 99 | * @return static |
||
| 100 | * <p>(Mutable) Return this Arrayy object, with the prepended value.</p> |
||
| 101 | */ |
||
| 102 | 2 | View Code Duplication | public function prepend($value, $key = null): Arrayy |
| 112 | |||
| 113 | /** |
||
| 114 | * Append a (key) + value to the current array. |
||
| 115 | * |
||
| 116 | * @param mixed $value |
||
| 117 | * @param mixed $key |
||
| 118 | * |
||
| 119 | * @return static |
||
| 120 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 121 | */ |
||
| 122 | 3 | View Code Duplication | public function append($value, $key = null): Arrayy |
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the values from given property or method. |
||
| 135 | * |
||
| 136 | * @param string $keyOrPropertyOrMethod the property or method name to filter by |
||
| 137 | * |
||
| 138 | * @throws \InvalidArgumentException if property or method is not defined |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | 1 | public function column(string $keyOrPropertyOrMethod): array |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Returns a collection of matching items. |
||
| 156 | * |
||
| 157 | * @param string $keyOrPropertyOrMethod the property or method to evaluate |
||
| 158 | * @param mixed $value the value to match |
||
| 159 | * |
||
| 160 | * @throws \InvalidArgumentException if property or method is not defined |
||
| 161 | * |
||
| 162 | * @return CollectionInterface |
||
| 163 | */ |
||
| 164 | 1 | public function where(string $keyOrPropertyOrMethod, $value): CollectionInterface |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Internal mechanic of set method. |
||
| 180 | * |
||
| 181 | * @param string|null $key |
||
| 182 | * @param mixed $value |
||
| 183 | * @param bool $checkProperties |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 10 | View Code Duplication | protected function internalSet($key, $value, $checkProperties = true): bool |
| 197 | |||
| 198 | /** |
||
| 199 | * Extracts the value of the given property or method from the object. |
||
| 200 | * |
||
| 201 | * @param object $object the object to extract the value from |
||
| 202 | * @param string $keyOrPropertyOrMethod the property or method for which the |
||
| 203 | * value should be extracted |
||
| 204 | * |
||
| 205 | * @throws \InvalidArgumentException if the method or property is not defined |
||
| 206 | * |
||
| 207 | * @return mixed the value extracted from the specified property or method |
||
| 208 | */ |
||
| 209 | 2 | private function extractValue($object, string $keyOrPropertyOrMethod) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Returns `true` if value is of the specified type. |
||
| 238 | * |
||
| 239 | * @param string $type the type to check the value against |
||
| 240 | * @param mixed $value the value to check |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | 10 | private function checkType(string $type, $value): bool |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param mixed $value |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | 5 | private function valueToString($value): string |
|
| 314 | } |
||
| 315 |