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 | View Code Duplication | public function __construct($values = null) |
|
|
|||
36 | { |
||
37 | $this->internal = new Map(); |
||
38 | |||
39 | if (is_array($values) || $values instanceof Traversable) { |
||
40 | $this->addAll($values); |
||
41 | } elseif (is_integer($values)) { |
||
42 | $this->allocate($values); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Adds zero or more values to the set. |
||
48 | * |
||
49 | * @param mixed ...$values |
||
50 | */ |
||
51 | public function add(...$values) |
||
57 | |||
58 | /** |
||
59 | * Adds all values in an array or iterable object to the set. |
||
60 | * |
||
61 | * @param array|\Traversable $values |
||
62 | */ |
||
63 | public function addAll($values) |
||
71 | |||
72 | /** |
||
73 | * Ensures that enough memory is allocated for a specified capacity. This |
||
74 | * potentially reduces the number of reallocations as the size increases. |
||
75 | * |
||
76 | * @param int $capacity The number of values for which capacity should be |
||
77 | * allocated. Capacity will stay the same if this value |
||
78 | * is less than or equal to the current capacity. |
||
79 | */ |
||
80 | public function allocate(int $capacity) |
||
84 | |||
85 | /** |
||
86 | * Returns the current capacity of the set. |
||
87 | * |
||
88 | * @return int |
||
89 | */ |
||
90 | public function capacity(): int |
||
94 | |||
95 | /** |
||
96 | * Clear all elements in the Set |
||
97 | */ |
||
98 | public function clear() |
||
102 | |||
103 | /** |
||
104 | * Determines whether the set contains all of zero or more values. |
||
105 | * |
||
106 | * @param mixed ...$values |
||
107 | * |
||
108 | * @return bool true if at least one value was provided and the set |
||
109 | * contains all given values, false otherwise. |
||
110 | */ |
||
111 | View Code Duplication | public function contains(...$values): bool |
|
125 | |||
126 | /** |
||
127 | * @inheritDoc |
||
128 | */ |
||
129 | public function copy() |
||
133 | |||
134 | /** |
||
135 | * Returns the number of elements in the Stack |
||
136 | * |
||
137 | * @return int |
||
138 | */ |
||
139 | public function count(): int |
||
143 | |||
144 | /** |
||
145 | * Creates a new set using values from this set that aren't in another set. |
||
146 | * |
||
147 | * Formally: A \ B = {x ∈ A | x ∉ B} |
||
148 | * |
||
149 | * @param Set $set |
||
150 | * |
||
151 | * @return Set |
||
152 | */ |
||
153 | public function diff(Set $set): Set |
||
167 | |||
168 | /** |
||
169 | * Creates a new set using values in either this set or in another set, |
||
170 | * but not in both. |
||
171 | * |
||
172 | * Formally: A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)} |
||
173 | * |
||
174 | * @param Set $set |
||
175 | * |
||
176 | * @return Set |
||
177 | */ |
||
178 | public function xor(Set $set): Set |
||
182 | |||
183 | /** |
||
184 | * Returns a new set containing only the values for which a predicate |
||
185 | * returns true. A boolean test will be used if a predicate is not provided. |
||
186 | * |
||
187 | * @param callable|null $predicate Accepts a value, returns a boolean: |
||
188 | * true : include the value, |
||
189 | * false: skip the value. |
||
190 | * |
||
191 | * @return Set |
||
192 | */ |
||
193 | View Code Duplication | public function filter(callable $predicate = null): Set |
|
205 | |||
206 | /** |
||
207 | * Returns the first value in the set. |
||
208 | * |
||
209 | * @return mixed the first value in the set. |
||
210 | */ |
||
211 | public function first() |
||
215 | |||
216 | /** |
||
217 | * Returns the value at a specified position in the set. |
||
218 | * |
||
219 | * @param int $position |
||
220 | * |
||
221 | * @return mixed|null |
||
222 | * |
||
223 | * @throws OutOfRangeException |
||
224 | */ |
||
225 | public function get(int $position) |
||
229 | |||
230 | /** |
||
231 | * Creates a new set using values common to both this set and another set. |
||
232 | * In other words, returns a copy of this set with all values removed that |
||
233 | * aren't in the other set. |
||
234 | * |
||
235 | * Formally: A ∩ B = {x : x ∈ A ∧ x ∈ B} |
||
236 | * |
||
237 | * @param Set $set |
||
238 | * |
||
239 | * @return Set |
||
240 | */ |
||
241 | public function intersect(Set $set): Set |
||
245 | |||
246 | /** |
||
247 | * @inheritDoc |
||
248 | */ |
||
249 | public function isEmpty(): bool |
||
253 | |||
254 | /** |
||
255 | * Joins all values of the set into a string, adding an optional 'glue' |
||
256 | * between them. Returns an empty string if the set is empty. |
||
257 | * |
||
258 | * @param string $glue |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | public function join(string $glue = null): string |
||
266 | |||
267 | /** |
||
268 | * Returns the last value in the set. |
||
269 | * |
||
270 | * @return mixed the last value in the set. |
||
271 | */ |
||
272 | public function last() |
||
276 | |||
277 | /** |
||
278 | * Iteratively reduces the set to a single value using a callback. |
||
279 | * |
||
280 | * @param callable $callback Accepts the carry and current value, and |
||
281 | * returns an updated carry value. |
||
282 | * |
||
283 | * @param mixed|null $initial Optional initial carry value. |
||
284 | * |
||
285 | * @return mixed The carry value of the final iteration, or the initial |
||
286 | * value if the set was empty. |
||
287 | */ |
||
288 | public function reduce(callable $callback, $initial = null) |
||
298 | |||
299 | /** |
||
300 | * Removes zero or more values from the set. |
||
301 | * |
||
302 | * @param mixed ...$values |
||
303 | */ |
||
304 | public function remove(...$values) |
||
310 | |||
311 | /** |
||
312 | * Returns a reversed copy of the set. |
||
313 | * |
||
314 | * @return Set |
||
315 | */ |
||
316 | public function reverse(): Set |
||
323 | |||
324 | /** |
||
325 | * Returns a subset of a given length starting at a specified offset. |
||
326 | * |
||
327 | * @param int $offset If the offset is non-negative, the set will start |
||
328 | * at that offset in the set. If offset is negative, |
||
329 | * the set will start that far from the end. |
||
330 | * |
||
331 | * @param int $length If a length is given and is positive, the resulting |
||
332 | * set will have up to that many values in it. |
||
333 | * If the requested length results in an overflow, only |
||
334 | * values up to the end of the set will be included. |
||
335 | * |
||
336 | * If a length is given and is negative, the set |
||
337 | * will stop that many values from the end. |
||
338 | * |
||
339 | * If a length is not provided, the resulting set |
||
340 | * will contains all values between the offset and the |
||
341 | * end of the set. |
||
342 | * |
||
343 | * @return Set |
||
344 | */ |
||
345 | public function slice(int $offset, int $length = null): Set |
||
352 | |||
353 | /** |
||
354 | * Returns a sorted copy of the set, based on an optional callable |
||
355 | * comparator. Natural ordering will be used if a comparator is not given. |
||
356 | * |
||
357 | * @param callable|null $comparator Accepts two values to be compared. |
||
358 | * Should return the result of a <=> b. |
||
359 | * |
||
360 | * @return Set |
||
361 | */ |
||
362 | public function sort(callable $comparator = null): Set |
||
369 | |||
370 | /** |
||
371 | * @inheritDoc |
||
372 | */ |
||
373 | public function toArray(): array |
||
377 | |||
378 | /** |
||
379 | * Creates a new set that contains the values of this set as well as the |
||
380 | * values of another set. |
||
381 | * |
||
382 | * Formally: A ∪ B = {x: x ∈ A ∨ x ∈ B} |
||
383 | * |
||
384 | * @param Set $set |
||
385 | * |
||
386 | * @return Set |
||
387 | */ |
||
388 | public function union(Set $set): Set |
||
402 | |||
403 | /** |
||
404 | * Get iterator |
||
405 | */ |
||
406 | public function getIterator() |
||
412 | |||
413 | /** |
||
414 | * @inheritdoc |
||
415 | * |
||
416 | * @throws OutOfBoundsException |
||
417 | */ |
||
418 | public function offsetSet($offset, $value) |
||
427 | |||
428 | /** |
||
429 | * @inheritdoc |
||
430 | */ |
||
431 | public function offsetGet($offset) |
||
435 | |||
436 | /** |
||
437 | * @inheritdoc |
||
438 | * |
||
439 | * @throws Error |
||
440 | */ |
||
441 | public function offsetExists($offset) |
||
445 | |||
446 | /** |
||
447 | * @inheritdoc |
||
448 | * |
||
449 | * @throws Error |
||
450 | */ |
||
451 | public function offsetUnset($offset) |
||
455 | } |
||
456 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.