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 $table; |
||
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 | * @param array|\Traversable|null $values |
||
30 | */ |
||
31 | public function __construct($values = null) |
||
39 | |||
40 | private function addAll($values) |
||
46 | |||
47 | /** |
||
48 | * Adds zero or more values to the set. |
||
49 | * |
||
50 | * @param mixed ...$values |
||
51 | */ |
||
|
|||
52 | public function add(...$values) |
||
56 | |||
57 | /** |
||
58 | * Ensures that enough memory is allocated for a specified capacity. This |
||
59 | * potentially reduces the number of reallocations as the size increases. |
||
60 | * |
||
61 | * @param int $capacity The number of values for which capacity should be |
||
62 | * allocated. Capacity will stay the same if this value |
||
63 | * is less than or equal to the current capacity. |
||
64 | */ |
||
65 | public function allocate(int $capacity) |
||
69 | |||
70 | /** |
||
71 | * Returns the current capacity of the set. |
||
72 | * |
||
73 | * @return int |
||
74 | */ |
||
75 | public function capacity(): int |
||
79 | |||
80 | /** |
||
81 | * Clear all elements in the Set |
||
82 | */ |
||
83 | public function clear() |
||
87 | |||
88 | /** |
||
89 | * Determines whether the set contains all of zero or more values. |
||
90 | * |
||
91 | * @param mixed ...$values |
||
92 | * |
||
93 | * @return bool true if at least one value was provided and the set |
||
94 | * contains all given values, false otherwise. |
||
95 | */ |
||
96 | View Code Duplication | public function contains(...$values): bool |
|
110 | |||
111 | /** |
||
112 | * @inheritDoc |
||
113 | */ |
||
114 | public function copy(): \Ds\Collection |
||
118 | |||
119 | /** |
||
120 | * Returns the number of elements in the Stack |
||
121 | * |
||
122 | * @return int |
||
123 | */ |
||
124 | public function count(): int |
||
128 | |||
129 | /** |
||
130 | * Creates a new set using values from this set that aren't in another set. |
||
131 | * |
||
132 | * Formally: A \ B = {x ∈ A | x ∉ B} |
||
133 | * |
||
134 | * @param Set $set |
||
135 | * |
||
136 | * @return Set |
||
137 | */ |
||
138 | public function diff(Set $set): Set |
||
152 | |||
153 | /** |
||
154 | * Creates a new set using values in either this set or in another set, |
||
155 | * but not in both. |
||
156 | * |
||
157 | * Formally: A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)} |
||
158 | * |
||
159 | * @param Set $set |
||
160 | * |
||
161 | * @return Set |
||
162 | */ |
||
163 | public function xor(Set $set): Set |
||
167 | |||
168 | /** |
||
169 | * Returns a new set containing only the values for which a predicate |
||
170 | * returns true. A boolean test will be used if a predicate is not provided. |
||
171 | * |
||
172 | * @param callable|null $predicate Accepts a value, returns a boolean: |
||
173 | * true : include the value, |
||
174 | * false: skip the value. |
||
175 | * |
||
176 | * @return Set |
||
177 | */ |
||
178 | View Code Duplication | public function filter(callable $predicate = null): Set |
|
190 | |||
191 | /** |
||
192 | * Returns the first value in the set. |
||
193 | * |
||
194 | * @return mixed the first value in the set. |
||
195 | */ |
||
196 | public function first() |
||
200 | |||
201 | /** |
||
202 | * Returns the value at a specified position in the set. |
||
203 | * |
||
204 | * @param int $position |
||
205 | * |
||
206 | * @return mixed|null |
||
207 | * |
||
208 | * @throws OutOfRangeException |
||
209 | */ |
||
210 | public function get(int $position) |
||
214 | |||
215 | /** |
||
216 | * Creates a new set using values common to both this set and another set. |
||
217 | * In other words, returns a copy of this set with all values removed that |
||
218 | * aren't in the other set. |
||
219 | * |
||
220 | * Formally: A ∩ B = {x : x ∈ A ∧ x ∈ B} |
||
221 | * |
||
222 | * @param Set $set |
||
223 | * |
||
224 | * @return Set |
||
225 | */ |
||
226 | public function intersect(Set $set): Set |
||
230 | |||
231 | /** |
||
232 | * @inheritDoc |
||
233 | */ |
||
234 | public function isEmpty(): bool |
||
238 | |||
239 | /** |
||
240 | * Joins all values of the set into a string, adding an optional 'glue' |
||
241 | * between them. Returns an empty string if the set is empty. |
||
242 | * |
||
243 | * @param string $glue |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | public function join(string $glue = null): string |
||
251 | |||
252 | /** |
||
253 | * Returns the last value in the set. |
||
254 | * |
||
255 | * @return mixed the last value in the set. |
||
256 | */ |
||
257 | public function last() |
||
261 | |||
262 | /** |
||
263 | * Iteratively reduces the set to a single value using a callback. |
||
264 | * |
||
265 | * @param callable $callback Accepts the carry and current value, and |
||
266 | * returns an updated carry value. |
||
267 | * |
||
268 | * @param mixed|null $initial Optional initial carry value. |
||
269 | * |
||
270 | * @return mixed The carry value of the final iteration, or the initial |
||
271 | * value if the set was empty. |
||
272 | */ |
||
273 | public function reduce(callable $callback, $initial = null) |
||
283 | |||
284 | /** |
||
285 | * Removes zero or more values from the set. |
||
286 | * |
||
287 | * @param mixed ...$values |
||
288 | */ |
||
289 | public function remove(...$values) |
||
295 | |||
296 | /** |
||
297 | * Reverses the set in-place. |
||
298 | */ |
||
299 | public function reverse() |
||
303 | |||
304 | /** |
||
305 | * Returns a reversed copy of the set. |
||
306 | * |
||
307 | * @return Set |
||
308 | */ |
||
309 | public function reversed(): Set |
||
316 | |||
317 | /** |
||
318 | * Returns a subset of a given length starting at a specified offset. |
||
319 | * |
||
320 | * @param int $offset If the offset is non-negative, the set will start |
||
321 | * at that offset in the set. If offset is negative, |
||
322 | * the set will start that far from the end. |
||
323 | * |
||
324 | * @param int $length If a length is given and is positive, the resulting |
||
325 | * set will have up to that many values in it. |
||
326 | * If the requested length results in an overflow, only |
||
327 | * values up to the end of the set will be included. |
||
328 | * |
||
329 | * If a length is given and is negative, the set |
||
330 | * will stop that many values from the end. |
||
331 | * |
||
332 | * If a length is not provided, the resulting set |
||
333 | * will contains all values between the offset and the |
||
334 | * end of the set. |
||
335 | * |
||
336 | * @return Set |
||
337 | */ |
||
338 | public function slice(int $offset, int $length = null): Set |
||
345 | |||
346 | /** |
||
347 | * Sorts the set in-place, based on an optional callable comparator. |
||
348 | * |
||
349 | * @param callable|null $comparator Accepts two values to be compared. |
||
350 | * Should return the result of a <=> b. |
||
351 | */ |
||
352 | public function sort(callable $comparator = null) |
||
356 | |||
357 | /** |
||
358 | * Returns a sorted copy of the set, based on an optional callable |
||
359 | * comparator. Natural ordering will be used if a comparator is not given. |
||
360 | * |
||
361 | * @param callable|null $comparator Accepts two values to be compared. |
||
362 | * Should return the result of a <=> b. |
||
363 | * |
||
364 | * @return Set |
||
365 | */ |
||
366 | public function sorted(callable $comparator = null): Set |
||
373 | |||
374 | /** |
||
375 | * @inheritDoc |
||
376 | */ |
||
377 | public function toArray(): array |
||
381 | |||
382 | /** |
||
383 | * Returns the sum of all values in the set. |
||
384 | * |
||
385 | * @return int|float The sum of all the values in the set. |
||
386 | */ |
||
387 | public function sum() |
||
391 | |||
392 | /** |
||
393 | * Creates a new set that contains the values of this set as well as the |
||
394 | * values of another set. |
||
395 | * |
||
396 | * Formally: A ∪ B = {x: x ∈ A ∨ x ∈ B} |
||
397 | * |
||
398 | * @param Set $set |
||
399 | * |
||
400 | * @return Set |
||
401 | */ |
||
402 | public function union(Set $set): Set |
||
416 | |||
417 | /** |
||
418 | * Get iterator |
||
419 | */ |
||
420 | public function getIterator() |
||
426 | |||
427 | /** |
||
428 | * @inheritdoc |
||
429 | * |
||
430 | * @throws OutOfBoundsException |
||
431 | */ |
||
432 | public function offsetSet($offset, $value) |
||
441 | |||
442 | /** |
||
443 | * @inheritdoc |
||
444 | */ |
||
445 | public function offsetGet($offset) |
||
449 | |||
450 | /** |
||
451 | * @inheritdoc |
||
452 | * |
||
453 | * @throws Error |
||
454 | */ |
||
455 | public function offsetExists($offset) |
||
459 | |||
460 | /** |
||
461 | * @inheritdoc |
||
462 | * |
||
463 | * @throws Error |
||
464 | */ |
||
465 | public function offsetUnset($offset) |
||
469 | } |
||
470 |