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 | public function calcAdjacentIntervals(array $intervals): array |
||
89 | { |
||
90 | |||
91 | $bounds = self::intervalsToSignedBounds($intervals); |
||
92 | $newIntervals = []; |
||
93 | $activeIntervals = []; |
||
94 | |||
95 | $boundsCount = count($bounds); |
||
96 | |||
97 | // Create new intervals for each set of two consecutive bounds, |
||
98 | // and calculate its total value. |
||
99 | for ($i = 1; $i < $boundsCount; $i++) { |
||
100 | |||
101 | // Set the current bound. |
||
102 | [$curBoundValue, $curBoundType, $curBoundIncluded, $curBoundIntervalKey] = $bounds[$i - 1]; |
||
103 | [$nextBoundValue, $nextBoundType, $nextBoundIncluded] = $bounds[$i]; |
||
104 | |||
105 | if ($curBoundType === '+') { |
||
106 | // If this is a low bound, |
||
107 | // add the key of the interval to the array of active intervals. |
||
108 | $activeIntervals[$curBoundIntervalKey] = true; |
||
109 | } else { |
||
110 | // If this is an high bound, remove the key. |
||
111 | unset($activeIntervals[$curBoundIntervalKey]); |
||
112 | } |
||
113 | |||
114 | if ( |
||
115 | isset($this->addStep, $this->substractStep) && ( |
||
116 | ($nextBoundIncluded && $nextBoundType === '+') |
||
117 | || (!$nextBoundIncluded && $nextBoundType === '+') |
||
118 | ) |
||
119 | ) { |
||
120 | $newHighBound = ($this->substractStep)($nextBoundValue); |
||
121 | } else { |
||
122 | $newHighBound = $nextBoundValue; |
||
123 | } |
||
124 | |||
125 | if ( |
||
126 | isset($this->addStep, $this->substractStep) && $curBoundType === '-' && $curBoundIncluded |
||
127 | ) { |
||
128 | $newLowBound = ($this->addStep)($curBoundValue); |
||
129 | } else { |
||
130 | $newLowBound = $curBoundValue; |
||
131 | } |
||
132 | |||
133 | $newIntervals[] = [ |
||
134 | $newLowBound, |
||
135 | $newHighBound, |
||
136 | $activeIntervals |
||
137 | ]; |
||
138 | } |
||
139 | |||
140 | return $newIntervals; |
||
141 | } |
||
143 |