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 |
||
15 | abstract class AbstractCollection extends Arrayy implements CollectionInterface |
||
16 | { |
||
17 | /** |
||
18 | * The type of elements stored in this collection. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private $collectionType; |
||
23 | |||
24 | /** |
||
25 | * Constructs a collection object of the specified type, optionally with the |
||
26 | * specified data. |
||
27 | * |
||
28 | * @param mixed $data |
||
29 | * <p> |
||
30 | * The initial items to store in the collection. |
||
31 | * </p> |
||
32 | */ |
||
33 | 14 | public function __construct($data = []) |
|
57 | |||
58 | /** |
||
59 | * @return static[] |
||
60 | */ |
||
61 | 5 | public function getCollection(): array |
|
65 | |||
66 | /** |
||
67 | * The type (FQCN) associated with this collection. |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | abstract public function getType(): string; |
||
72 | |||
73 | /** |
||
74 | * Merge current items and items of given collections into a new one. |
||
75 | * |
||
76 | * @param CollectionInterface ...$collections The collections to merge. |
||
77 | * |
||
78 | * @throws \InvalidArgumentException if any of the given collections are not of the same type |
||
79 | * |
||
80 | * @return CollectionInterface |
||
81 | */ |
||
82 | 1 | public function merge(CollectionInterface ...$collections): CollectionInterface |
|
94 | |||
95 | /** |
||
96 | * Assigns a value to the specified offset + check the type. |
||
97 | * |
||
98 | * @param int|string|null $offset |
||
99 | * @param mixed $value |
||
100 | */ |
||
101 | 1 | public function offsetSet($offset, $value) |
|
107 | |||
108 | /** |
||
109 | * Prepend a (key) + value to the current array. |
||
110 | * |
||
111 | * @param mixed $value |
||
112 | * @param mixed $key |
||
113 | * |
||
114 | * @return static |
||
115 | * <p>(Mutable) Return this Arrayy object, with the prepended value.</p> |
||
116 | */ |
||
117 | 2 | public function prepend($value, $key = null): Arrayy |
|
123 | |||
124 | /** |
||
125 | * Append a (key) + value to the current array. |
||
126 | * |
||
127 | * @param mixed $value |
||
128 | * @param mixed $key |
||
129 | * |
||
130 | * @return static |
||
131 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
132 | */ |
||
133 | 3 | public function append($value, $key = null): Arrayy |
|
139 | |||
140 | /** |
||
141 | * Returns the values from given property or method. |
||
142 | * |
||
143 | * @param string $keyOrPropertyOrMethod the property or method name to filter by |
||
144 | * |
||
145 | * @throws \InvalidArgumentException if property or method is not defined |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | 1 | public function column(string $keyOrPropertyOrMethod): array |
|
160 | |||
161 | /** |
||
162 | * Returns a collection of matching items. |
||
163 | * |
||
164 | * @param string $keyOrPropertyOrMethod the property or method to evaluate |
||
165 | * @param mixed $value the value to match |
||
166 | * |
||
167 | * @throws \InvalidArgumentException if property or method is not defined |
||
168 | * |
||
169 | * @return CollectionInterface |
||
170 | */ |
||
171 | 1 | public function where(string $keyOrPropertyOrMethod, $value): CollectionInterface |
|
172 | { |
||
173 | 1 | return $this->filter( |
|
174 | function ($item) use ($keyOrPropertyOrMethod, $value) { |
||
175 | 1 | $accessorValue = $this->extractValue( |
|
176 | 1 | $item, |
|
177 | 1 | $keyOrPropertyOrMethod |
|
178 | ); |
||
179 | |||
180 | 1 | return $accessorValue === $value; |
|
181 | 1 | } |
|
182 | ); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Internal mechanic of set method. |
||
187 | * |
||
188 | * @param string|null $key |
||
189 | * @param mixed $value |
||
190 | * @param bool $checkProperties |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | 11 | protected function internalSet($key, $value, $checkProperties = true): bool |
|
200 | |||
201 | /** |
||
202 | * @param mixed $value |
||
203 | */ |
||
204 | 13 | private function checkTypeWrapper($value) |
|
212 | |||
213 | /** |
||
214 | * Extracts the value of the given property or method from the object. |
||
215 | * |
||
216 | * @param Arrayy $object the object to extract the value from |
||
217 | * @param string $keyOrPropertyOrMethod the property or method for which the |
||
218 | * value should be extracted |
||
219 | * |
||
220 | * @throws \InvalidArgumentException if the method or property is not defined |
||
221 | * |
||
222 | * @return mixed the value extracted from the specified property or method |
||
223 | */ |
||
224 | 2 | private function extractValue(Arrayy $object, string $keyOrPropertyOrMethod) |
|
248 | |||
249 | /** |
||
250 | * Returns `true` if value is of the specified type. |
||
251 | * |
||
252 | * @param string $type the type to check the value against |
||
253 | * @param mixed $value the value to check |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | 13 | private function checkType(string $type, $value): bool |
|
291 | |||
292 | /** |
||
293 | * @param mixed $value |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | 6 | private function valueToString($value): string |
|
327 | } |
||
328 |