| Conditions | 11 |
| Paths | 9 |
| Total Lines | 53 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 88 | * |
||
| 89 | * @param array $intervals The initial array. |
||
| 90 | * @return array An array containing only discrete values. |
||
| 91 | */ |
||
| 92 | public static function extractDiscreteValues(array &$intervals): array |
||
| 93 | { |
||
| 94 | $discreteValues = array_filter($intervals, static function ($interval) { |
||
| 95 | return $interval[0] === $interval[1]; |
||
| 96 | }); |
||
| 97 | |||
| 98 | $intervals = array_diff_key($intervals, $discreteValues); |
||
| 99 | |||
| 100 | return $discreteValues; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Create each new interval and calculate its value based on the active intervals on each bound. |
||
| 105 | * |
||
| 106 | * @param array[] $intervals |
||
| 107 | * @return array[] |
||
| 108 | */ |
||
| 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 === '+') |
||
| 179 |