| Conditions | 12 |
| Paths | 18 |
| Total Lines | 68 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 109 | public function flatten(array $intervals): array |
||
| 110 | { |
||
| 111 | |||
| 112 | $discreteValues = self::extractDiscreteValues($intervals); |
||
| 113 | |||
| 114 | |||
| 115 | $bounds = self::intervalsToSignedBounds($intervals); |
||
| 116 | $newIntervals = []; |
||
| 117 | $activeIntervals = []; |
||
| 118 | |||
| 119 | $boundsCount = count($bounds); |
||
| 120 | |||
| 121 | // Create new intervals for each set of two consecutive bounds, |
||
| 122 | // and calculate its total value. |
||
| 123 | for ($i = 1; $i < $boundsCount; $i++) { |
||
| 124 | |||
| 125 | // Set the current bound. |
||
| 126 | [$curBoundValue, $curBoundType, $curBoundIncluded, $curBoundIntervalKey] = $bounds[$i - 1]; |
||
| 127 | [$nextBoundValue, $nextBoundType, $nextBoundIncluded] = $bounds[$i]; |
||
| 128 | |||
| 129 | if ($curBoundType === '+') { |
||
| 130 | // If this is a low bound, |
||
| 131 | // add the key of the interval to the array of active intervals. |
||
| 132 | $activeIntervals[$curBoundIntervalKey] = true; |
||
| 133 | } else { |
||
| 134 | // If this is an high bound, remove the key. |
||
| 135 | unset($activeIntervals[$curBoundIntervalKey]); |
||
| 136 | } |
||
| 137 | |||
| 138 | if ( |
||
| 139 | isset($this->addStep, $this->substractStep) && ( |
||
| 140 | ($nextBoundIncluded && $nextBoundType === '+') |
||
| 141 | || (!$nextBoundIncluded && $nextBoundType === '+') |
||
| 142 | ) |
||
| 143 | ) { |
||
| 144 | $newHighBound = ($this->substractStep)($nextBoundValue); |
||
| 145 | } else { |
||
| 146 | $newHighBound = $nextBoundValue; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ( |
||
| 150 | isset($this->addStep, $this->substractStep) && $curBoundType === '-' && $curBoundIncluded |
||
| 151 | ) { |
||
| 152 | $newLowBound = ($this->addStep)($curBoundValue); |
||
| 153 | } else { |
||
| 154 | $newLowBound = $curBoundValue; |
||
| 155 | } |
||
| 156 | |||
| 157 | $newIntervals[] = [ |
||
| 158 | $newLowBound, |
||
| 159 | $newHighBound, |
||
| 160 | $activeIntervals |
||
| 161 | ]; |
||
| 162 | } |
||
| 163 | |||
| 164 | // Remove empty interval generated when two or more intervals share a common bound. |
||
| 165 | $newIntervals = array_values(array_filter($newIntervals, static function ($i) { |
||
| 166 | // Use weak comparison in case of object typed bounds. |
||
| 167 | return $i[0] != $i[1]; |
||
| 168 | })); |
||
| 169 | |||
| 170 | |||
| 171 | // Push discrete values back into the array. |
||
| 172 | if (!empty($discreteValues)) { |
||
| 173 | array_push($newIntervals, ...$discreteValues); |
||
| 174 | } |
||
| 175 | |||
| 176 | return $newIntervals; |
||
| 177 | } |
||
| 179 |