Complex classes like Sequence 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 Sequence, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | final class Sequence implements Countable, IteratorAggregate |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var Period[] |
||
| 38 | */ |
||
| 39 | private $intervals = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * new instance. |
||
| 43 | * |
||
| 44 | * @param Period... $intervals |
||
|
|
|||
| 45 | */ |
||
| 46 | public function __construct(Period ...$intervals) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function count(): int |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public function getIterator(): Iterator |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Tells whether the sequence is empty. |
||
| 71 | */ |
||
| 72 | public function isEmpty(): bool |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Removes all intervals from the collection. |
||
| 79 | */ |
||
| 80 | public function clear(): void |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the array representation of the sequence. |
||
| 87 | * |
||
| 88 | * @return Period[] |
||
| 89 | */ |
||
| 90 | public function toArray(): array |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the interval specified at a given offset. |
||
| 97 | * |
||
| 98 | * @throws Exception If the offset is illegal for the current sequence |
||
| 99 | */ |
||
| 100 | public function get(int $offset): Period |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Removes from the sequence and returns the interval at the given offset. |
||
| 112 | * |
||
| 113 | * The sequence is re-indexed after removal |
||
| 114 | * |
||
| 115 | * @throws Exception If the offset is illegal for the current sequence. |
||
| 116 | */ |
||
| 117 | public function remove(int $offset): Period |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Adds new interval at the end of the sequence. |
||
| 129 | * @param Period... $intervals |
||
| 130 | */ |
||
| 131 | public function push(Period $interval, Period ...$intervals): void |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Update the interval at the specify offset. |
||
| 138 | * |
||
| 139 | * @throws Exception If the offset is illegal for the current sequence. |
||
| 140 | */ |
||
| 141 | public function set(int $offset, Period $interval): void |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Tells whether the given interval is present in the sequence. |
||
| 149 | */ |
||
| 150 | public function contains(Period $interval): bool |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Attempts to find the first offset attached to the submitted interval. |
||
| 157 | * |
||
| 158 | * If no offset is found the method returns null. |
||
| 159 | * |
||
| 160 | * @return ?int |
||
| 161 | */ |
||
| 162 | public function find(Period $interval): ?int |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the collection boundaries as a Period instance. |
||
| 175 | * |
||
| 176 | * @return ?Period |
||
| 177 | */ |
||
| 178 | public function getInterval(): ?Period |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Sort the current instance according to the given comparison callable |
||
| 190 | * and maintain index association. |
||
| 191 | * |
||
| 192 | * Returns true on success or false on failure |
||
| 193 | */ |
||
| 194 | public function sort(callable $compare): bool |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns an instance sorted according to the given comparison callable |
||
| 201 | * but does not maintain index association. |
||
| 202 | * |
||
| 203 | * This method MUST retain the state of the current instance, and return |
||
| 204 | * an instance that contains the sorted intervals. The key are re-indexed |
||
| 205 | */ |
||
| 206 | public function sorted(callable $compare): self |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Filters the sequence according to the given predicate. |
||
| 219 | * |
||
| 220 | * This method MUST retain the state of the current instance, and return |
||
| 221 | * an instance that contains the interval which validate the predicate. |
||
| 222 | */ |
||
| 223 | public function filter(callable $predicate): self |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Tells whether some interval in the current instance satisfies the predicate. |
||
| 230 | */ |
||
| 231 | public function some(callable $predicate): bool |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Tells whether all interval in the current instance satisfies the predicate. |
||
| 244 | */ |
||
| 245 | public function every(callable $predicate): bool |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the gaps inside the instance. |
||
| 258 | */ |
||
| 259 | public function getGaps(): self |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Sort two Interval instance using their start datepoint. |
||
| 285 | */ |
||
| 286 | private function sortByStartDate(Period $interval1, Period $interval2): int |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns the intersections inside the instance. |
||
| 293 | */ |
||
| 294 | public function getIntersections(): self |
||
| 322 | } |
||
| 323 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.