Conditions | 12 |
Paths | 11 |
Total Lines | 88 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
194 | public function squareRootModP(\GMP $a, \GMP $p): \GMP |
||
195 | { |
||
196 | $math = $this->adapter; |
||
197 | $four = gmp_init(4, 10); |
||
198 | $eight = gmp_init(8, 10); |
||
199 | |||
200 | $modMath = $math->getModularArithmetic($p); |
||
201 | if ($math->cmp($this->one, $p) < 0) { |
||
202 | if ($math->equals($a, $this->zero)) { |
||
203 | return $this->zero; |
||
204 | } |
||
205 | |||
206 | if ($math->equals($p, $this->two)) { |
||
207 | return $a; |
||
208 | } |
||
209 | |||
210 | $jac = $math->jacobi($a, $p); |
||
211 | if ($jac === -1) { |
||
212 | throw new SquareRootException("{$math->toString($a)} has no square root modulo {$math->toString($p)}"); |
||
213 | } |
||
214 | |||
215 | if ($math->equals($math->mod($p, $four), gmp_init(3, 10))) { |
||
216 | return $modMath->pow($a, $math->div($math->add($p, $this->one), $four)); |
||
217 | } |
||
218 | |||
219 | if ($math->equals($math->mod($p, $eight), gmp_init(5, 10))) { |
||
220 | $d = $modMath->pow($a, $math->div($math->sub($p, $this->one), $four)); |
||
221 | if ($math->equals($d, $this->one)) { |
||
222 | return $modMath->pow($a, $math->div($math->add($p, gmp_init(3, 10)), $eight)); |
||
223 | } |
||
224 | |||
225 | if ($math->equals($d, $math->sub($p, $this->one))) { |
||
226 | return $modMath->mul( |
||
227 | $math->mul( |
||
228 | $this->two, |
||
229 | $a |
||
230 | ), |
||
231 | $modMath->pow( |
||
232 | $math->mul( |
||
233 | $four, |
||
234 | $a |
||
235 | ), |
||
236 | $math->div( |
||
237 | $math->sub( |
||
238 | $p, |
||
239 | gmp_init(5, 10) |
||
240 | ), |
||
241 | $eight |
||
242 | ) |
||
243 | ) |
||
244 | ); |
||
245 | } |
||
246 | //shouldn't get here |
||
247 | } |
||
248 | |||
249 | for ($b = $this->two; $math->cmp($b, $p) < 0; $b = gmp_add($b, $this->one)) { |
||
250 | if ($math->jacobi( |
||
251 | $math->sub( |
||
252 | $math->mul($b, $b), |
||
253 | $math->mul($four, $a) |
||
254 | ), |
||
255 | $p |
||
256 | ) == -1 |
||
257 | ) { |
||
258 | $f = array($a, $math->sub($this->zero, $b), $this->one); |
||
259 | |||
260 | $ff = $this->polynomialPowMod( |
||
261 | array($this->zero, $this->one), |
||
262 | $math->div( |
||
263 | $math->add( |
||
264 | $p, |
||
265 | $this->one |
||
266 | ), |
||
267 | $this->two |
||
268 | ), |
||
269 | $f, |
||
270 | $p |
||
271 | ); |
||
272 | |||
273 | if ($math->equals($ff[1], $this->zero)) { |
||
274 | return $ff[0]; |
||
275 | } |
||
276 | // if we got here no b was found |
||
277 | } |
||
278 | } |
||
279 | } |
||
280 | |||
281 | throw new SquareRootException('Unable to calculate square root mod p!'); |
||
282 | } |
||
284 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..