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