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 Set 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 Set, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | final class Set implements \IteratorAggregate, \ArrayAccess, Collection |
||
15 | { |
||
16 | use Traits\Collection; |
||
17 | |||
18 | const MIN_CAPACITY = Map::MIN_CAPACITY; |
||
19 | |||
20 | /** |
||
21 | * @var Map |
||
22 | */ |
||
23 | private $internal; |
||
24 | |||
25 | /** |
||
26 | * Creates a new set using the values of an array or Traversable object. |
||
27 | * The keys of either will not be preserved. |
||
28 | * |
||
29 | * |
||
30 | * Should an integer be provided the Set will allocate the memory capacity |
||
31 | * to the size of $values. |
||
32 | * |
||
33 | * @param array|\Traversable|int|null $values |
||
34 | */ |
||
35 | public function __construct($values = null) |
||
43 | |||
44 | /** |
||
45 | * Adds zero or more values to the set. |
||
46 | * |
||
47 | * @param mixed ...$values |
||
48 | */ |
||
|
|||
49 | public function add(...$values) |
||
55 | |||
56 | /** |
||
57 | * Ensures that enough memory is allocated for a specified capacity. This |
||
58 | * potentially reduces the number of reallocations as the size increases. |
||
59 | * |
||
60 | * @param int $capacity The number of values for which capacity should be |
||
61 | * allocated. Capacity will stay the same if this value |
||
62 | * is less than or equal to the current capacity. |
||
63 | */ |
||
64 | public function allocate(int $capacity) |
||
68 | |||
69 | /** |
||
70 | * Returns the current capacity of the set. |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | public function capacity(): int |
||
78 | |||
79 | /** |
||
80 | * Clear all elements in the Set |
||
81 | */ |
||
82 | public function clear() |
||
86 | |||
87 | /** |
||
88 | * Determines whether the set contains all of zero or more values. |
||
89 | * |
||
90 | * @param mixed ...$values |
||
91 | * |
||
92 | * @return bool true if at least one value was provided and the set |
||
93 | * contains all given values, false otherwise. |
||
94 | */ |
||
95 | public function contains(...$values): bool |
||
109 | |||
110 | /** |
||
111 | * @inheritDoc |
||
112 | */ |
||
113 | public function copy() |
||
117 | |||
118 | /** |
||
119 | * Returns the number of elements in the Stack |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | public function count(): int |
||
127 | |||
128 | /** |
||
129 | * Creates a new set using values from this set that aren't in another set. |
||
130 | * |
||
131 | * Formally: A \ B = {x ∈ A | x ∉ B} |
||
132 | * |
||
133 | * @param Set $set |
||
134 | * |
||
135 | * @return Set |
||
136 | */ |
||
137 | public function diff(Set $set): Set |
||
151 | |||
152 | /** |
||
153 | * Creates a new set using values in either this set or in another set, |
||
154 | * but not in both. |
||
155 | * |
||
156 | * Formally: A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)} |
||
157 | * |
||
158 | * @param Set $set |
||
159 | * |
||
160 | * @return Set |
||
161 | */ |
||
162 | public function xor(Set $set): Set |
||
166 | |||
167 | /** |
||
168 | * Returns a new set containing only the values for which a predicate |
||
169 | * returns true. A boolean test will be used if a predicate is not provided. |
||
170 | * |
||
171 | * @param callable|null $predicate Accepts a value, returns a boolean: |
||
172 | * true : include the value, |
||
173 | * false: skip the value. |
||
174 | * |
||
175 | * @return Set |
||
176 | */ |
||
177 | View Code Duplication | public function filter(callable $predicate = null): Set |
|
189 | |||
190 | /** |
||
191 | * Returns the first value in the set. |
||
192 | * |
||
193 | * @return mixed the first value in the set. |
||
194 | */ |
||
195 | public function first() |
||
199 | |||
200 | /** |
||
201 | * Returns the value at a specified position in the set. |
||
202 | * |
||
203 | * @param int $position |
||
204 | * |
||
205 | * @return mixed|null |
||
206 | * |
||
207 | * @throws OutOfRangeException |
||
208 | */ |
||
209 | public function get(int $position) |
||
213 | |||
214 | /** |
||
215 | * Creates a new set using values common to both this set and another set. |
||
216 | * In other words, returns a copy of this set with all values removed that |
||
217 | * aren't in the other set. |
||
218 | * |
||
219 | * Formally: A ∩ B = {x : x ∈ A ∧ x ∈ B} |
||
220 | * |
||
221 | * @param Set $set |
||
222 | * |
||
223 | * @return Set |
||
224 | */ |
||
225 | public function intersect(Set $set): Set |
||
229 | |||
230 | /** |
||
231 | * @inheritDoc |
||
232 | */ |
||
233 | public function isEmpty(): bool |
||
237 | |||
238 | /** |
||
239 | * Joins all values of the set into a string, adding an optional 'glue' |
||
240 | * between them. Returns an empty string if the set is empty. |
||
241 | * |
||
242 | * @param string $glue |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public function join(string $glue = null): string |
||
250 | |||
251 | /** |
||
252 | * Returns the last value in the set. |
||
253 | * |
||
254 | * @return mixed the last value in the set. |
||
255 | */ |
||
256 | public function last() |
||
260 | |||
261 | /** |
||
262 | * Iteratively reduces the set to a single value using a callback. |
||
263 | * |
||
264 | * @param callable $callback Accepts the carry and current value, and |
||
265 | * returns an updated carry value. |
||
266 | * |
||
267 | * @param mixed|null $initial Optional initial carry value. |
||
268 | * |
||
269 | * @return mixed The carry value of the final iteration, or the initial |
||
270 | * value if the set was empty. |
||
271 | */ |
||
272 | public function reduce(callable $callback, $initial = null) |
||
282 | |||
283 | /** |
||
284 | * Removes zero or more values from the set. |
||
285 | * |
||
286 | * @param mixed ...$values |
||
287 | */ |
||
288 | public function remove(...$values) |
||
294 | |||
295 | /** |
||
296 | * Returns a reversed copy of the set. |
||
297 | * |
||
298 | * @return Set |
||
299 | */ |
||
300 | public function reverse(): Set |
||
307 | |||
308 | /** |
||
309 | * Returns a subset of a given length starting at a specified offset. |
||
310 | * |
||
311 | * @param int $offset If the offset is non-negative, the set will start |
||
312 | * at that offset in the set. If offset is negative, |
||
313 | * the set will start that far from the end. |
||
314 | * |
||
315 | * @param int $length If a length is given and is positive, the resulting |
||
316 | * set will have up to that many values in it. |
||
317 | * If the requested length results in an overflow, only |
||
318 | * values up to the end of the set will be included. |
||
319 | * |
||
320 | * If a length is given and is negative, the set |
||
321 | * will stop that many values from the end. |
||
322 | * |
||
323 | * If a length is not provided, the resulting set |
||
324 | * will contains all values between the offset and the |
||
325 | * end of the set. |
||
326 | * |
||
327 | * @return Set |
||
328 | */ |
||
329 | public function slice(int $offset, int $length = null): Set |
||
336 | |||
337 | /** |
||
338 | * Returns a sorted copy of the set, based on an optional callable |
||
339 | * comparator. Natural ordering will be used if a comparator is not given. |
||
340 | * |
||
341 | * @param callable|null $comparator Accepts two values to be compared. |
||
342 | * Should return the result of a <=> b. |
||
343 | * |
||
344 | * @return Set |
||
345 | */ |
||
346 | public function sort(callable $comparator = null): Set |
||
353 | |||
354 | /** |
||
355 | * @inheritDoc |
||
356 | */ |
||
357 | public function toArray(): array |
||
361 | |||
362 | /** |
||
363 | * Creates a new set that contains the values of this set as well as the |
||
364 | * values of another set. |
||
365 | * |
||
366 | * Formally: A ∪ B = {x: x ∈ A ∨ x ∈ B} |
||
367 | * |
||
368 | * @param Set $set |
||
369 | * |
||
370 | * @return Set |
||
371 | */ |
||
372 | public function union(Set $set): Set |
||
386 | |||
387 | /** |
||
388 | * Get iterator |
||
389 | */ |
||
390 | public function getIterator() |
||
396 | |||
397 | /** |
||
398 | * @inheritdoc |
||
399 | * |
||
400 | * @throws OutOfBoundsException |
||
401 | */ |
||
402 | public function offsetSet($offset, $value) |
||
411 | |||
412 | /** |
||
413 | * @inheritdoc |
||
414 | */ |
||
415 | public function offsetGet($offset) |
||
419 | |||
420 | /** |
||
421 | * @inheritdoc |
||
422 | * |
||
423 | * @throws Error |
||
424 | */ |
||
425 | public function offsetExists($offset) |
||
429 | |||
430 | /** |
||
431 | * @inheritdoc |
||
432 | * |
||
433 | * @throws Error |
||
434 | */ |
||
435 | public function offsetUnset($offset) |
||
439 | } |
||
440 |