Complex classes like Range 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 Range, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Range implements IEqualable, \ArrayAccess |
||
|
|
|||
| 47 | { |
||
| 48 | /** @var ITotallyOrderedType */ |
||
| 49 | private $subtype; |
||
| 50 | /** @var IRangeCanonicalFunc */ |
||
| 51 | private $canonicalFunc; |
||
| 52 | /** @var bool */ |
||
| 53 | private $empty; |
||
| 54 | /** @var mixed */ |
||
| 55 | private $lower; |
||
| 56 | /** @var mixed */ |
||
| 57 | private $upper; |
||
| 58 | /** @var bool */ |
||
| 59 | private $lowerInc; |
||
| 60 | /** @var bool */ |
||
| 61 | private $upperInc; |
||
| 62 | |||
| 63 | |||
| 64 | //region construction |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Creates a new range with given lower and upper bounds. |
||
| 68 | * |
||
| 69 | * There are two ways of calling this factory method: whether the bounds are inclusive or exclusive may be |
||
| 70 | * specified: |
||
| 71 | * - either by passing a specification string as the `$boundsOrLowerInc` argument (e.g., `'[)'`), |
||
| 72 | * - or by passing two boolean values as the `$boundsOrLowerInc` and `$upperInc` telling whichever bound is |
||
| 73 | * inclusive. |
||
| 74 | * |
||
| 75 | * If the lower bound is greater than the upper bound, or if both bounds are equal but any of them is exclusive, an |
||
| 76 | * empty range gets created, forgetting the given upper and lower bound (just as PostgreSQL does). For creating an |
||
| 77 | * empty range explicitly, see {@link createEmpty()}. |
||
| 78 | * |
||
| 79 | * @param ITotallyOrderedType $subtype the range subtype |
||
| 80 | * @param IRangeCanonicalFunc $canonicalFunc if given, the range will be created in the canonical form according to |
||
| 81 | * this function; |
||
| 82 | * skip with <tt>null</tt> to create the range as is; |
||
| 83 | * NOTE: while one could expect the canonical function usage at the range |
||
| 84 | * *type* level, passing it to this factory method makes it clear that |
||
| 85 | * if no canonical function is given, the range will not get |
||
| 86 | * canonicalized |
||
| 87 | * @param mixed $lower the range lower bound, or <tt>null</tt> if unbounded |
||
| 88 | * @param mixed $upper the range upper bound, or <tt>null</tt> if unbounded |
||
| 89 | * @param bool|string $boundsOrLowerInc either the string of complete bounds specification, or a boolean telling |
||
| 90 | * whether the lower bound is inclusive; |
||
| 91 | * the complete specification is similar to PostgreSQL - it is a two-character |
||
| 92 | * string of <tt>'['</tt> or <tt>'('</tt>, and <tt>']'</tt> or <tt>')'</tt> |
||
| 93 | * (brackets denoting an inclusive bound, parentheses denoting an exclusive |
||
| 94 | * bound; |
||
| 95 | * when the boolean is used, also the <tt>$upperInc</tt> argument is used; |
||
| 96 | * either bound specification is irrelevant if the corresponding range edge is |
||
| 97 | * <tt>null</tt> - the range is open by definition on that side, and thus |
||
| 98 | * will be created as such |
||
| 99 | * @param bool $upperInc whether the upper bound is inclusive; |
||
| 100 | * only relevant if <tt>$boundsOrLowerInc</tt> is also boolean |
||
| 101 | * @return Range |
||
| 102 | */ |
||
| 103 | public static function createFromBounds( |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Creates a new empty range. |
||
| 162 | * |
||
| 163 | * @param ITotallyOrderedType $subtype the range subtype |
||
| 164 | * @return Range |
||
| 165 | */ |
||
| 166 | public static function createEmpty(ITotallyOrderedType $subtype): Range |
||
| 170 | |||
| 171 | private static function processBoundSpec($boundsOrLowerInc = '[)', ?bool $upperInc = null) |
||
| 195 | |||
| 196 | |||
| 197 | private function __construct( |
||
| 214 | |||
| 215 | //endregion |
||
| 216 | |||
| 217 | //region getters |
||
| 218 | |||
| 219 | final public function getSubtype(): ITotallyOrderedType |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return bool whether the range is empty |
||
| 226 | */ |
||
| 227 | final public function isEmpty(): bool |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return mixed lower bound, or <tt>null</tt> if the range is lower-unbounded or empty |
||
| 234 | */ |
||
| 235 | final public function getLower() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return mixed upper bound, or <tt>null</tt> if the range is upper-unbounded or empty |
||
| 242 | */ |
||
| 243 | final public function getUpper() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return bool|null whether the range includes its lower bound, or <tt>null</tt> if the range is empty; |
||
| 250 | * for lower-unbounded ranges, <tt>false</tt> is returned by definition |
||
| 251 | */ |
||
| 252 | final public function isLowerInc(): ?bool |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return bool|null whether the range includes its upper bound, or <tt>null</tt> if the range is empty; |
||
| 259 | * for upper-unbounded ranges, <tt>false</tt> is returned by definition |
||
| 260 | */ |
||
| 261 | final public function isUpperInc(): ?bool |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return string|null the bounds inclusive/exclusive specification, as accepted by {@link createFromBounds()}, or |
||
| 268 | * <tt>null</tt> if the range is empty |
||
| 269 | */ |
||
| 270 | final public function getBoundsSpec(): ?string |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return bool whether the range is just a single point |
||
| 281 | */ |
||
| 282 | final public function isSinglePoint(): bool |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Returns the range bounds according to the requested bound specification. |
||
| 306 | * |
||
| 307 | * **Only defined on ranges of {@link IDiscreteType discrete} subtypes.** |
||
| 308 | * |
||
| 309 | * E.g., on an integer range `(null,3)`, if bounds `[]` are requested, a pair of `null` and `2` is returned. |
||
| 310 | * |
||
| 311 | * @param bool|string $boundsOrLowerInc either the string of complete bounds specification, or a boolean telling |
||
| 312 | * whether the lower bound is inclusive; |
||
| 313 | * the complete specification is similar to PostgreSQL - it is a two-character |
||
| 314 | * string of <tt>'['</tt> or <tt>'('</tt>, and <tt>']'</tt> or <tt>')'</tt> |
||
| 315 | * (brackets denoting an inclusive bound, parentheses denoting an exclusive |
||
| 316 | * bound; |
||
| 317 | * when the boolean is used, also the <tt>$upperInc</tt> argument is used |
||
| 318 | * @param bool $upperInc whether the upper bound is inclusive; |
||
| 319 | * only relevant if <tt>$boundsOrLowerInc</tt> is also boolean |
||
| 320 | * @return array|null pair of the lower and upper bound, or <tt>null</tt> if the range is empty |
||
| 321 | * @throws UnsupportedException if the range subtype is not an {@link IDiscreteType} |
||
| 322 | */ |
||
| 323 | public function toBounds($boundsOrLowerInc, ?bool $upperInc = null): ?array |
||
| 357 | |||
| 358 | final public function __toString() |
||
| 372 | |||
| 373 | //endregion |
||
| 374 | |||
| 375 | //region range operations |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param mixed $element a value of the range subtype |
||
| 379 | * @return bool|null whether this range contains the given element; |
||
| 380 | * <tt>null</tt> on <tt>null</tt> input |
||
| 381 | */ |
||
| 382 | public function containsElement($element): ?bool |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param mixed $element value of the range subtype |
||
| 410 | * @return bool|null <tt>true</tt> iff this range is left of the given element - value of the range subtype; |
||
| 411 | * <tt>false</tt> otherwise, especially if this range is empty; |
||
| 412 | * <tt>null</tt> on <tt>null</tt> input |
||
| 413 | */ |
||
| 414 | public function leftOfElement($element): ?bool |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param mixed $element value of the range subtype |
||
| 432 | * @return bool|null <tt>true</tt> iff this range is right of the given element - value of the range subtype; |
||
| 433 | * <tt>false</tt> otherwise, especially if this range is empty; |
||
| 434 | * <tt>null</tt> on <tt>null</tt> input |
||
| 435 | */ |
||
| 436 | public function rightOfElement($element): ?bool |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param Range $other a range of the same subtype as this range |
||
| 454 | * @return bool|null whether this range entirely contains the other range; |
||
| 455 | * an empty range is considered to be contained in any range, even an empty one; |
||
| 456 | * <tt>null</tt> on <tt>null</tt> input |
||
| 457 | */ |
||
| 458 | public function containsRange(?Range $other): ?bool |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param Range $other a range of the same subtype as this range |
||
| 497 | * @return bool|null whether this range is entirely contained in the other range; |
||
| 498 | * an empty range is considered to be contained in any range, even an empty one; |
||
| 499 | * <tt>null</tt> on <tt>null</tt> input |
||
| 500 | */ |
||
| 501 | public function containedInRange(?Range $other): ?bool |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @param Range $other a range of the same subtype as this range |
||
| 511 | * @return bool|null whether this and the other range overlap, i.e., have a non-empty intersection; |
||
| 512 | * <tt>null</tt> on <tt>null</tt> input |
||
| 513 | */ |
||
| 514 | public function overlaps(?Range $other): ?bool |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Computes the intersection of this range with another range. |
||
| 540 | * |
||
| 541 | * @param Range $other a range of the same subtype as this range |
||
| 542 | * @return Range|null intersection of this and the other range |
||
| 543 | * <tt>null</tt> on <tt>null</tt> input |
||
| 544 | */ |
||
| 545 | public function intersect(?Range $other): ?Range |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @return bool whether the range is finite, i.e., neither starts nor ends in the infinity; |
||
| 602 | * note that an empty range is considered as finite |
||
| 603 | */ |
||
| 604 | final public function isFinite(): bool |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @param Range|null $other a range of the same subtype as this range |
||
| 611 | * @return bool|null <tt>true</tt> iff this range is strictly left of the other range, i.e., it ends before the |
||
| 612 | * other starts; |
||
| 613 | * <tt>false</tt> otherwise, especially if either range is empty; |
||
| 614 | * <tt>null</tt> on <tt>null</tt> input |
||
| 615 | */ |
||
| 616 | public function strictlyLeftOf(?Range $other): ?bool |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param Range|null $other a range of the same subtype as this range |
||
| 634 | * @return bool|null <tt>true</tt> iff this range is strictly left of the other range, i.e., it ends before the |
||
| 635 | * other starts; |
||
| 636 | * <tt>false</tt> otherwise, especially if either range is empty;; |
||
| 637 | * <tt>null</tt> on <tt>null</tt> input |
||
| 638 | */ |
||
| 639 | public function strictlyRightOf(?Range $other): ?bool |
||
| 654 | |||
| 655 | //endregion |
||
| 656 | |||
| 657 | //region IEqualable |
||
| 658 | |||
| 659 | public function equals($object): ?bool |
||
| 715 | |||
| 716 | //endregion |
||
| 717 | |||
| 718 | //region ArrayAccess |
||
| 719 | |||
| 720 | public function offsetExists($offset) |
||
| 724 | |||
| 725 | public function offsetGet($offset) |
||
| 736 | |||
| 737 | public function offsetSet($offset, $value) |
||
| 741 | |||
| 742 | public function offsetUnset($offset) |
||
| 746 | |||
| 747 | //endregion |
||
| 748 | } |
||
| 749 |