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 | * @param array|\Traversable|null $values |
||
| 30 | */ |
||
| 31 | public function __construct($values = null) |
||
| 32 | { |
||
| 33 | $this->internal = new Map(); |
||
| 34 | |||
| 35 | if (func_num_args()) { |
||
| 36 | $this->add(...$values); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Adds zero or more values to the set. |
||
| 42 | * |
||
| 43 | * @param mixed ...$values |
||
| 44 | */ |
||
|
|
|||
| 45 | public function add(...$values) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Ensures that enough memory is allocated for a specified capacity. This |
||
| 54 | * potentially reduces the number of reallocations as the size increases. |
||
| 55 | * |
||
| 56 | * @param int $capacity The number of values for which capacity should be |
||
| 57 | * allocated. Capacity will stay the same if this value |
||
| 58 | * is less than or equal to the current capacity. |
||
| 59 | */ |
||
| 60 | public function allocate(int $capacity) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Returns the current capacity of the set. |
||
| 67 | * |
||
| 68 | * @return int |
||
| 69 | */ |
||
| 70 | public function capacity(): int |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Clear all elements in the Set |
||
| 77 | */ |
||
| 78 | public function clear() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Determines whether the set contains all of zero or more values. |
||
| 85 | * |
||
| 86 | * @param mixed ...$values |
||
| 87 | * |
||
| 88 | * @return bool true if at least one value was provided and the set |
||
| 89 | * contains all given values, false otherwise. |
||
| 90 | */ |
||
| 91 | public function contains(...$values): bool |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @inheritDoc |
||
| 104 | */ |
||
| 105 | public function copy(): \Ds\Collection |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the number of elements in the Stack |
||
| 112 | * |
||
| 113 | * @return int |
||
| 114 | */ |
||
| 115 | public function count(): int |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Creates a new set using values from this set that aren't in another set. |
||
| 122 | * |
||
| 123 | * Formally: A \ B = {x ∈ A | x ∉ B} |
||
| 124 | * |
||
| 125 | * @param Set $set |
||
| 126 | * |
||
| 127 | * @return Set |
||
| 128 | */ |
||
| 129 | public function diff(Set $set): Set |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Creates a new set using values in either this set or in another set, |
||
| 136 | * but not in both. |
||
| 137 | * |
||
| 138 | * Formally: A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)} |
||
| 139 | * |
||
| 140 | * @param Set $set |
||
| 141 | * |
||
| 142 | * @return Set |
||
| 143 | */ |
||
| 144 | public function xor(Set $set): Set |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns a new set containing only the values for which a callback |
||
| 151 | * returns true. A boolean test will be used if a callback is not provided. |
||
| 152 | * |
||
| 153 | * @param callable|null $callback Accepts a value, returns a boolean: |
||
| 154 | * true : include the value, |
||
| 155 | * false: skip the value. |
||
| 156 | * |
||
| 157 | * @return Set |
||
| 158 | */ |
||
| 159 | public function filter(callable $callback = null): Set |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns the first value in the set. |
||
| 166 | * |
||
| 167 | * @return mixed the first value in the set. |
||
| 168 | */ |
||
| 169 | public function first() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the value at a specified position in the set. |
||
| 176 | * |
||
| 177 | * @param int $position |
||
| 178 | * |
||
| 179 | * @return mixed|null |
||
| 180 | * |
||
| 181 | * @throws OutOfRangeException |
||
| 182 | */ |
||
| 183 | public function get(int $position) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Creates a new set using values common to both this set and another set. |
||
| 190 | * |
||
| 191 | * In other words, returns a copy of this set with all values removed that |
||
| 192 | * aren't in the other set. |
||
| 193 | * |
||
| 194 | * Formally: A ∩ B = {x : x ∈ A ∧ x ∈ B} |
||
| 195 | * |
||
| 196 | * @param Set $set |
||
| 197 | * |
||
| 198 | * @return Set |
||
| 199 | */ |
||
| 200 | public function intersect(Set $set): Set |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @inheritDoc |
||
| 207 | */ |
||
| 208 | public function isEmpty(): bool |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Joins all values of the set into a string, adding an optional 'glue' |
||
| 215 | * between them. Returns an empty string if the set is empty. |
||
| 216 | * |
||
| 217 | * @param string $glue |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function join(string $glue = null): string |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the last value in the set. |
||
| 228 | * |
||
| 229 | * @return mixed the last value in the set. |
||
| 230 | */ |
||
| 231 | public function last() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Iteratively reduces the set to a single value using a callback. |
||
| 238 | * |
||
| 239 | * @param callable $callback Accepts the carry and current value, and |
||
| 240 | * returns an updated carry value. |
||
| 241 | * |
||
| 242 | * @param mixed|null $initial Optional initial carry value. |
||
| 243 | * |
||
| 244 | * @return mixed The carry value of the final iteration, or the initial |
||
| 245 | * value if the set was empty. |
||
| 246 | */ |
||
| 247 | public function reduce(callable $callback, $initial = null) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Removes zero or more values from the set. |
||
| 260 | * |
||
| 261 | * @param mixed ...$values |
||
| 262 | */ |
||
| 263 | public function remove(...$values) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Reverses the set in-place. |
||
| 272 | */ |
||
| 273 | public function reverse() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns a reversed copy of the set. |
||
| 280 | * |
||
| 281 | * @return Set |
||
| 282 | */ |
||
| 283 | public function reversed(): Set |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns a subset of a given length starting at a specified offset. |
||
| 293 | * |
||
| 294 | * @param int $offset If the offset is non-negative, the set will start |
||
| 295 | * at that offset in the set. If offset is negative, |
||
| 296 | * the set will start that far from the end. |
||
| 297 | * |
||
| 298 | * @param int $length If a length is given and is positive, the resulting |
||
| 299 | * set will have up to that many values in it. |
||
| 300 | * If the requested length results in an overflow, only |
||
| 301 | * values up to the end of the set will be included. |
||
| 302 | * |
||
| 303 | * If a length is given and is negative, the set |
||
| 304 | * will stop that many values from the end. |
||
| 305 | * |
||
| 306 | * If a length is not provided, the resulting set |
||
| 307 | * will contains all values between the offset and the |
||
| 308 | * end of the set. |
||
| 309 | * |
||
| 310 | * @return Set |
||
| 311 | */ |
||
| 312 | public function slice(int $offset, int $length = null): Set |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Sorts the set in-place, based on an optional callable comparator. |
||
| 322 | * |
||
| 323 | * @param callable|null $comparator Accepts two values to be compared. |
||
| 324 | * Should return the result of a <=> b. |
||
| 325 | */ |
||
| 326 | public function sort(callable $comparator = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Returns a sorted copy of the set, based on an optional callable |
||
| 333 | * comparator. Natural ordering will be used if a comparator is not given. |
||
| 334 | * |
||
| 335 | * @param callable|null $comparator Accepts two values to be compared. |
||
| 336 | * Should return the result of a <=> b. |
||
| 337 | * |
||
| 338 | * @return Set |
||
| 339 | */ |
||
| 340 | public function sorted(callable $comparator = null): Set |
||
| 347 | |||
| 348 | /** |
||
| 349 | * |
||
| 350 | */ |
||
| 351 | public function merge($values): Set |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @inheritDoc |
||
| 364 | */ |
||
| 365 | public function toArray(): array |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Returns the sum of all values in the set. |
||
| 372 | * |
||
| 373 | * @return int|float The sum of all the values in the set. |
||
| 374 | */ |
||
| 375 | public function sum() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Creates a new set that contains the values of this set as well as the |
||
| 382 | * values of another set. |
||
| 383 | * |
||
| 384 | * Formally: A ∪ B = {x: x ∈ A ∨ x ∈ B} |
||
| 385 | * |
||
| 386 | * @param Set $set |
||
| 387 | * |
||
| 388 | * @return Set |
||
| 389 | */ |
||
| 390 | public function union(Set $set): Set |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get iterator |
||
| 407 | */ |
||
| 408 | public function getIterator() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @inheritdoc |
||
| 417 | * |
||
| 418 | * @throws OutOfBoundsException |
||
| 419 | */ |
||
| 420 | public function offsetSet($offset, $value) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @inheritdoc |
||
| 432 | */ |
||
| 433 | public function offsetGet($offset) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @inheritdoc |
||
| 440 | * |
||
| 441 | * @throws Error |
||
| 442 | */ |
||
| 443 | public function offsetExists($offset) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @inheritdoc |
||
| 450 | * |
||
| 451 | * @throws Error |
||
| 452 | */ |
||
| 453 | public function offsetUnset($offset) |
||
| 457 | } |
||
| 458 |