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 |
||
| 13 | final class Set implements \IteratorAggregate, \ArrayAccess, Collection |
||
| 14 | { |
||
| 15 | use Traits\GenericCollection; |
||
| 16 | |||
| 17 | const MIN_CAPACITY = Map::MIN_CAPACITY; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Map internal map to store the values. |
||
| 21 | */ |
||
| 22 | private $table; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Creates a new set using the values of an array or Traversable object. |
||
| 26 | * The keys of either will not be preserved. |
||
| 27 | * |
||
| 28 | * @param array|\Traversable|null $values |
||
| 29 | */ |
||
| 30 | public function __construct($values = null) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Adds zero or more values to the set. |
||
| 41 | * |
||
| 42 | * @param mixed ...$values |
||
| 43 | */ |
||
|
|
|||
| 44 | public function add(...$values) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Ensures that enough memory is allocated for a specified capacity. This |
||
| 53 | * potentially reduces the number of reallocations as the size increases. |
||
| 54 | * |
||
| 55 | * @param int $capacity The number of values for which capacity should be |
||
| 56 | * allocated. Capacity will stay the same if this value |
||
| 57 | * is less than or equal to the current capacity. |
||
| 58 | */ |
||
| 59 | public function allocate(int $capacity) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Returns the current capacity of the set. |
||
| 66 | * |
||
| 67 | * @return int |
||
| 68 | */ |
||
| 69 | public function capacity(): int |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Clear all elements in the Set |
||
| 76 | */ |
||
| 77 | public function clear() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Determines whether the set contains all of zero or more values. |
||
| 84 | * |
||
| 85 | * @param mixed ...$values |
||
| 86 | * |
||
| 87 | * @return bool true if at least one value was provided and the set |
||
| 88 | * contains all given values, false otherwise. |
||
| 89 | */ |
||
| 90 | public function contains(...$values): bool |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @inheritDoc |
||
| 103 | */ |
||
| 104 | public function copy(): \Ds\Collection |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the number of elements in the Stack |
||
| 111 | * |
||
| 112 | * @return int |
||
| 113 | */ |
||
| 114 | public function count(): int |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Creates a new set using values from this set that aren't in another set. |
||
| 121 | * |
||
| 122 | * Formally: A \ B = {x ∈ A | x ∉ B} |
||
| 123 | * |
||
| 124 | * @param Set $set |
||
| 125 | * |
||
| 126 | * @return Set |
||
| 127 | */ |
||
| 128 | public function diff(Set $set): Set |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Creates a new set using values in either this set or in another set, |
||
| 135 | * but not in both. |
||
| 136 | * |
||
| 137 | * Formally: A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)} |
||
| 138 | * |
||
| 139 | * @param Set $set |
||
| 140 | * |
||
| 141 | * @return Set |
||
| 142 | */ |
||
| 143 | public function xor(Set $set): Set |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns a new set containing only the values for which a callback |
||
| 150 | * returns true. A boolean test will be used if a callback is not provided. |
||
| 151 | * |
||
| 152 | * @param callable|null $callback Accepts a value, returns a boolean: |
||
| 153 | * true : include the value, |
||
| 154 | * false: skip the value. |
||
| 155 | * |
||
| 156 | * @return Set |
||
| 157 | */ |
||
| 158 | public function filter(callable $callback = null): Set |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the first value in the set. |
||
| 165 | * |
||
| 166 | * @return mixed the first value in the set. |
||
| 167 | */ |
||
| 168 | public function first() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the value at a specified position in the set. |
||
| 175 | * |
||
| 176 | * @param int $position |
||
| 177 | * |
||
| 178 | * @return mixed|null |
||
| 179 | * |
||
| 180 | * @throws OutOfRangeException |
||
| 181 | */ |
||
| 182 | public function get(int $position) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Creates a new set using values common to both this set and another set. |
||
| 189 | * |
||
| 190 | * In other words, returns a copy of this set with all values removed that |
||
| 191 | * aren't in the other set. |
||
| 192 | * |
||
| 193 | * Formally: A ∩ B = {x : x ∈ A ∧ x ∈ B} |
||
| 194 | * |
||
| 195 | * @param Set $set |
||
| 196 | * |
||
| 197 | * @return Set |
||
| 198 | */ |
||
| 199 | public function intersect(Set $set): Set |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @inheritDoc |
||
| 206 | */ |
||
| 207 | public function isEmpty(): bool |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Joins all values of the set into a string, adding an optional 'glue' |
||
| 214 | * between them. Returns an empty string if the set is empty. |
||
| 215 | * |
||
| 216 | * @param string $glue |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function join(string $glue = null): string |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Returns the last value in the set. |
||
| 227 | * |
||
| 228 | * @return mixed the last value in the set. |
||
| 229 | */ |
||
| 230 | public function last() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Iteratively reduces the set to a single value using a callback. |
||
| 237 | * |
||
| 238 | * @param callable $callback Accepts the carry and current value, and |
||
| 239 | * returns an updated carry value. |
||
| 240 | * |
||
| 241 | * @param mixed|null $initial Optional initial carry value. |
||
| 242 | * |
||
| 243 | * @return mixed The carry value of the final iteration, or the initial |
||
| 244 | * value if the set was empty. |
||
| 245 | */ |
||
| 246 | public function reduce(callable $callback, $initial = null) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Removes zero or more values from the set. |
||
| 259 | * |
||
| 260 | * @param mixed ...$values |
||
| 261 | */ |
||
| 262 | public function remove(...$values) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Reverses the set in-place. |
||
| 271 | */ |
||
| 272 | public function reverse() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns a reversed copy of the set. |
||
| 279 | * |
||
| 280 | * @return Set |
||
| 281 | */ |
||
| 282 | public function reversed(): Set |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns a subset of a given length starting at a specified offset. |
||
| 292 | * |
||
| 293 | * @param int $offset If the offset is non-negative, the set will start |
||
| 294 | * at that offset in the set. If offset is negative, |
||
| 295 | * the set will start that far from the end. |
||
| 296 | * |
||
| 297 | * @param int $length If a length is given and is positive, the resulting |
||
| 298 | * set will have up to that many values in it. |
||
| 299 | * If the requested length results in an overflow, only |
||
| 300 | * values up to the end of the set will be included. |
||
| 301 | * |
||
| 302 | * If a length is given and is negative, the set |
||
| 303 | * will stop that many values from the end. |
||
| 304 | * |
||
| 305 | * If a length is not provided, the resulting set |
||
| 306 | * will contains all values between the offset and the |
||
| 307 | * end of the set. |
||
| 308 | * |
||
| 309 | * @return Set |
||
| 310 | */ |
||
| 311 | public function slice(int $offset, int $length = null): Set |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Sorts the set in-place, based on an optional callable comparator. |
||
| 321 | * |
||
| 322 | * @param callable|null $comparator Accepts two values to be compared. |
||
| 323 | * Should return the result of a <=> b. |
||
| 324 | */ |
||
| 325 | public function sort(callable $comparator = null) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns a sorted copy of the set, based on an optional callable |
||
| 332 | * comparator. Natural ordering will be used if a comparator is not given. |
||
| 333 | * |
||
| 334 | * @param callable|null $comparator Accepts two values to be compared. |
||
| 335 | * Should return the result of a <=> b. |
||
| 336 | * |
||
| 337 | * @return Set |
||
| 338 | */ |
||
| 339 | public function sorted(callable $comparator = null): Set |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns the result of adding all given values to the set. |
||
| 349 | * |
||
| 350 | * @param array|\Traversable $values |
||
| 351 | * |
||
| 352 | * @return \Ds\Set |
||
| 353 | */ |
||
| 354 | public function merge($values): Set |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @inheritDoc |
||
| 367 | */ |
||
| 368 | public function toArray(): array |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Returns the sum of all values in the set. |
||
| 375 | * |
||
| 376 | * @return int|float The sum of all the values in the set. |
||
| 377 | */ |
||
| 378 | public function sum() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Creates a new set that contains the values of this set as well as the |
||
| 385 | * values of another set. |
||
| 386 | * |
||
| 387 | * Formally: A ∪ B = {x: x ∈ A ∨ x ∈ B} |
||
| 388 | * |
||
| 389 | * @param Set $set |
||
| 390 | * |
||
| 391 | * @return Set |
||
| 392 | */ |
||
| 393 | public function union(Set $set): Set |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get iterator |
||
| 410 | */ |
||
| 411 | public function getIterator() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @inheritdoc |
||
| 420 | * |
||
| 421 | * @throws OutOfBoundsException |
||
| 422 | */ |
||
| 423 | public function offsetSet($offset, $value) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @inheritdoc |
||
| 435 | */ |
||
| 436 | public function offsetGet($offset) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @inheritdoc |
||
| 443 | * |
||
| 444 | * @throws Error |
||
| 445 | */ |
||
| 446 | public function offsetExists($offset) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @inheritdoc |
||
| 453 | * |
||
| 454 | * @throws Error |
||
| 455 | */ |
||
| 456 | public function offsetUnset($offset) |
||
| 460 | } |
||
| 461 |