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 sequence. |
||
| 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 InvalidIndex If the offset is illegal for the current sequence |
||
| 99 | */ |
||
| 100 | public function get(int $offset): Period |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Removes an interval from the sequence at the given offset and returns it. |
||
| 112 | * |
||
| 113 | * The sequence is re-indexed after removal |
||
| 114 | * |
||
| 115 | * @throws InvalidIndex 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 | * Updates the interval at the specify offset. |
||
| 138 | * |
||
| 139 | * @throws InvalidIndex 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 | * @param Period... $intervals |
||
| 150 | */ |
||
| 151 | public function contains(Period $interval, Period ... $intervals): bool |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Attempts to find the first offset attached to the submitted interval. |
||
| 165 | * |
||
| 166 | * If no offset is found the method returns null. |
||
| 167 | * |
||
| 168 | * @return ?int |
||
| 169 | */ |
||
| 170 | public function indexOf(Period $interval): ?int |
||
| 171 | { |
||
| 172 | foreach ($this->intervals as $offset => $period) { |
||
| 173 | if ($period->equals($interval)) { |
||
| 174 | return $offset; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | return null; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the sequence boundaries as a Period instance. |
||
| 183 | * |
||
| 184 | * If the sequence contains no interval null is returned. |
||
| 185 | * |
||
| 186 | * @return ?Period |
||
| 187 | */ |
||
| 188 | public function getBoundaries(): ?Period |
||
| 189 | { |
||
| 190 | $period = reset($this->intervals); |
||
| 191 | if (false === $period) { |
||
| 192 | return null; |
||
| 193 | } |
||
| 194 | |||
| 195 | return $period->merge(...$this->intervals); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Sort the current instance according to the given comparison callable |
||
| 200 | * and maintain index association. |
||
| 201 | * |
||
| 202 | * Returns true on success or false on failure |
||
| 203 | */ |
||
| 204 | public function sort(callable $compare): bool |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns an instance sorted according to the given comparison callable |
||
| 211 | * but does not maintain index association. |
||
| 212 | * |
||
| 213 | * This method MUST retain the state of the current instance, and return |
||
| 214 | * an instance that contains the sorted intervals. The key are re-indexed |
||
| 215 | */ |
||
| 216 | public function sorted(callable $compare): self |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Filters the sequence according to the given predicate. |
||
| 229 | * |
||
| 230 | * This method MUST retain the state of the current instance, and return |
||
| 231 | * an instance that contains the interval which validate the predicate. |
||
| 232 | */ |
||
| 233 | public function filter(callable $predicate): self |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Tells whether some intervals in the current instance satisfies the predicate. |
||
| 245 | */ |
||
| 246 | public function some(callable $predicate): bool |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Tells whether all intervals in the current instance satisfies the predicate. |
||
| 259 | */ |
||
| 260 | public function every(callable $predicate): bool |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns the gaps inside the instance. |
||
| 273 | */ |
||
| 274 | public function getGaps(): self |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Sort two Interval instance using their start datepoint. |
||
| 300 | */ |
||
| 301 | private function sortByStartDate(Period $interval1, Period $interval2): int |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns the intersections inside the instance. |
||
| 308 | */ |
||
| 309 | public function getIntersections(): self |
||
| 337 | } |
||
| 338 |
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.