| Total Complexity | 45 |
| Total Lines | 302 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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) |
||
| 47 | { |
||
| 48 | $this->intervals = $intervals; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function count(): int |
||
| 55 | { |
||
| 56 | return count($this->intervals); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public function getIterator(): Iterator |
||
| 63 | { |
||
| 64 | foreach ($this->intervals as $offset => $interval) { |
||
| 65 | yield $offset => $interval; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Tells whether the sequence is empty. |
||
| 71 | */ |
||
| 72 | public function isEmpty(): bool |
||
| 73 | { |
||
| 74 | return [] === $this->intervals; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Removes all intervals from the sequence. |
||
| 79 | */ |
||
| 80 | public function clear(): void |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the array representation of the sequence. |
||
| 87 | * |
||
| 88 | * @return Period[] |
||
| 89 | */ |
||
| 90 | public function toArray(): array |
||
| 91 | { |
||
| 92 | return $this->intervals; |
||
| 93 | } |
||
| 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 |
||
| 101 | { |
||
| 102 | $period = $this->intervals[$offset] ?? null; |
||
| 103 | if (null !== $period) { |
||
| 104 | return $period; |
||
| 105 | } |
||
| 106 | |||
| 107 | throw new InvalidIndex(sprintf('%s is an invalid offset in the current sequence', $offset)); |
||
| 108 | } |
||
| 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 |
||
| 118 | { |
||
| 119 | $interval = $this->get($offset); |
||
| 120 | unset($this->intervals[$offset]); |
||
| 121 | |||
| 122 | $this->intervals = array_values($this->intervals); |
||
| 123 | |||
| 124 | return $interval; |
||
| 125 | } |
||
| 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 |
||
| 132 | { |
||
| 133 | $this->intervals = array_merge($this->intervals, [$interval], $intervals); |
||
| 134 | } |
||
| 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 |
||
| 142 | { |
||
| 143 | $this->get($offset); |
||
| 144 | $this->intervals[$offset] = $interval; |
||
| 145 | } |
||
| 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 |
||
| 152 | { |
||
| 153 | $intervals[] = $interval; |
||
| 154 | foreach ($intervals as $period) { |
||
| 155 | if (null === $this->indexOf($period)) { |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return true; |
||
| 161 | } |
||
| 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 |
||
| 205 | { |
||
| 206 | return uasort($this->intervals, $compare); |
||
| 207 | } |
||
| 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 |
||
| 217 | { |
||
| 218 | $intervals = $this->intervals; |
||
| 219 | usort($intervals, $compare); |
||
| 220 | if ($intervals === $this->intervals) { |
||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | return new self(...$intervals); |
||
| 225 | } |
||
| 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 |
||
| 234 | { |
||
| 235 | $intervals = array_filter($this->intervals, $predicate); |
||
| 236 | if ($intervals === $this->intervals) { |
||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | return new self(...$intervals); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Tells whether some intervals in the current instance satisfies the predicate. |
||
| 245 | */ |
||
| 246 | public function some(callable $predicate): bool |
||
| 247 | { |
||
| 248 | foreach ($this->intervals as $interval) { |
||
| 249 | if (true === $predicate($interval)) { |
||
| 250 | return true; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | return false; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Tells whether all intervals in the current instance satisfies the predicate. |
||
| 259 | */ |
||
| 260 | public function every(callable $predicate): bool |
||
| 261 | { |
||
| 262 | foreach ($this->intervals as $interval) { |
||
| 263 | if (true !== $predicate($interval)) { |
||
| 264 | return false; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | return [] !== $this->intervals; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns the gaps inside the instance. |
||
| 273 | */ |
||
| 274 | public function getGaps(): self |
||
| 275 | { |
||
| 276 | $sequence = new self(); |
||
| 277 | $interval = null; |
||
| 278 | $currentInterval = null; |
||
| 279 | foreach ($this->sorted([$this, 'sortByStartDate']) as $period) { |
||
| 280 | $currentInterval = $period; |
||
| 281 | if (null === $interval) { |
||
| 282 | $interval = $currentInterval; |
||
| 283 | continue; |
||
| 284 | } |
||
| 285 | |||
| 286 | if (!$interval->overlaps($currentInterval) && !$interval->abuts($currentInterval)) { |
||
| 287 | $sequence->push($interval->gap($currentInterval)); |
||
| 288 | } |
||
| 289 | |||
| 290 | if (!$interval->contains($currentInterval)) { |
||
| 291 | $interval = $currentInterval; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | return $sequence; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Sort two Interval instance using their start datepoint. |
||
| 300 | */ |
||
| 301 | private function sortByStartDate(Period $interval1, Period $interval2): int |
||
| 302 | { |
||
| 303 | return $interval1->getStartDate() <=> $interval2->getStartDate(); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns the intersections inside the instance. |
||
| 308 | */ |
||
| 309 | public function getIntersections(): self |
||
| 336 | } |
||
| 337 | } |
||
| 338 |