Conditions | 15 |
Paths | 97 |
Total Lines | 36 |
Code Lines | 21 |
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 |
||
134 | public function normalize(int $containerSize): NormalizedSlice |
||
135 | { |
||
136 | // TODO: Need refactor |
||
137 | $step = $this->step ?? 1; |
||
138 | |||
139 | if ($step === 0) { |
||
140 | throw new IndexError("Step cannot be 0."); |
||
141 | } |
||
142 | |||
143 | $defaultEnd = ($step < 0 && $this->end === null) ? -1 : null; |
||
144 | |||
145 | $start = $this->start ?? ($step > 0 ? 0 : $containerSize - 1); |
||
146 | $end = $this->end ?? ($step > 0 ? $containerSize : -1); |
||
147 | |||
148 | $start = intval(round($start)); |
||
149 | $end = intval(round($end)); |
||
150 | $step = intval(round($step)); |
||
151 | |||
152 | $start = Util::normalizeIndex($start, $containerSize, false); |
||
153 | $end = Util::normalizeIndex($end, $containerSize, false); |
||
154 | |||
155 | if ($step > 0 && $start >= $containerSize) { |
||
156 | $start = $end = $containerSize - 1; |
||
157 | } elseif ($step < 0 && $start < 0) { |
||
158 | $start = $end = 0; |
||
159 | $defaultEnd = 0; |
||
160 | } |
||
161 | |||
162 | $start = $this->squeezeInBounds($start, 0, $containerSize - 1); |
||
163 | $end = $this->squeezeInBounds($end, $step > 0 ? 0 : -1, $containerSize); |
||
164 | |||
165 | if (($step > 0 && $end < $start) || ($step < 0 && $end > $start)) { |
||
166 | $end = $start; |
||
167 | } |
||
168 | |||
169 | return new NormalizedSlice($start, $defaultEnd ?? $end, $step); |
||
170 | } |
||
204 |