| Conditions | 6 |
| Paths | 14 |
| Total Lines | 58 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 117 | public function flatten(array $intervals): array |
||
| 118 | { |
||
| 119 | $discreteValues = self::extractDiscreteValues($intervals); |
||
| 120 | |||
| 121 | |||
| 122 | $bounds = self::intervalsToSignedBounds($intervals); |
||
| 123 | $newIntervals = []; |
||
| 124 | $activeIntervals = []; |
||
| 125 | |||
| 126 | $boundsCount = count($bounds); |
||
| 127 | |||
| 128 | // Create new intervals for each set of two consecutive bounds, |
||
| 129 | // and calculate its total value. |
||
| 130 | for ($i = 1; $i < $boundsCount; $i++) { |
||
| 131 | // Set the current bound. |
||
| 132 | [$curBoundValue, $curBoundType, $curBoundIncluded, $curBoundIntervalKey] = $bounds[$i - 1]; |
||
| 133 | [$nextBoundValue, $nextBoundType, $nextBoundIncluded] = $bounds[$i]; |
||
| 134 | |||
| 135 | if ($curBoundType === '+') { |
||
| 136 | // If this is a low bound, |
||
| 137 | // add the key of the interval to the array of active intervals. |
||
| 138 | $activeIntervals[$curBoundIntervalKey] = true; |
||
| 139 | } else { |
||
| 140 | // If this is an high bound, remove the key. |
||
| 141 | unset($activeIntervals[$curBoundIntervalKey]); |
||
| 142 | } |
||
| 143 | |||
| 144 | // If the current bound is the same as the next, which happens when multiple intervals |
||
| 145 | // begin or end at the same time, skip interval creation. |
||
| 146 | // Use weak type comparison by default, in order to correctly compare object types like DateTime. |
||
| 147 | if ($curBoundValue == $nextBoundValue) { |
||
| 148 | continue; |
||
| 149 | } |
||
| 150 | |||
| 151 | $newHighBound = $this->makeHighBound($nextBoundType, $nextBoundIncluded, $nextBoundValue); |
||
| 152 | $newLowBound = $this->makeLowBound($curBoundType, $curBoundIncluded, $curBoundValue); |
||
| 153 | |||
| 154 | // If the new high bound is lower or equal to the new low bound, |
||
| 155 | // which can happen when using steps, |
||
| 156 | // skip interval creation. |
||
| 157 | // TODO Check validity and add tests |
||
| 158 | if ($newHighBound <= $newLowBound) { |
||
| 159 | continue; |
||
| 160 | } |
||
| 161 | |||
| 162 | $newIntervals[] = [ |
||
| 163 | $newLowBound, |
||
| 164 | $newHighBound, |
||
| 165 | $activeIntervals |
||
| 166 | ]; |
||
| 167 | } |
||
| 168 | |||
| 169 | // Push discrete values back into the array. |
||
| 170 | if (!empty($discreteValues)) { |
||
| 171 | array_push($newIntervals, ...$discreteValues); |
||
| 172 | } |
||
| 173 | |||
| 174 | return $newIntervals; |
||
| 175 | } |
||
| 216 |