| Conditions | 12 |
| Paths | 11 |
| Total Lines | 93 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 176 | public function squareRootModP(\GMP $a, \GMP $p) |
||
| 177 | { |
||
| 178 | $math = $this->adapter; |
||
| 179 | $zero = gmp_init(0, 10); |
||
| 180 | $one = gmp_init(1, 10); |
||
| 181 | $two = gmp_init(2, 10); |
||
| 182 | $four = gmp_init(4, 10); |
||
| 183 | $eight = gmp_init(8, 10); |
||
| 184 | |||
| 185 | $modMath = $math->getModularArithmetic($p); |
||
| 186 | if ($math->cmp($one, $p) < 0) { |
||
| 187 | if ($math->equals($a, $zero)) { |
||
| 188 | return $zero; |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($math->equals($p, $two)) { |
||
| 192 | return $a; |
||
| 193 | } |
||
| 194 | |||
| 195 | $jac = $math->jacobi($a, $p); |
||
| 196 | if ($jac == -1) { |
||
| 197 | throw new \LogicException($math->toString($a)." has no square root modulo ".$math->toString($p)); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ($math->equals($math->mod($p, $four), gmp_init(3, 10))) { |
||
| 201 | return $modMath->pow($a, $math->div($math->add($p, $one), $four)); |
||
| 202 | } |
||
| 203 | |||
| 204 | if ($math->equals($math->mod($p, $eight), gmp_init(5, 10))) { |
||
| 205 | $d = $modMath->pow($a, $math->div($math->sub($p, $one), $four)); |
||
| 206 | if ($math->equals($d, $one)) { |
||
| 207 | return $modMath->pow($a, $math->div($math->add($p, gmp_init(3, 10)), $eight)); |
||
| 208 | } |
||
| 209 | |||
| 210 | if ($math->equals($d, $math->sub($p, $one))) { |
||
| 211 | return $modMath->mul( |
||
| 212 | $math->mul( |
||
| 213 | $two, |
||
| 214 | $a |
||
| 215 | ), |
||
| 216 | $modMath->pow( |
||
| 217 | $math->mul( |
||
| 218 | $four, |
||
| 219 | $a |
||
| 220 | ), |
||
| 221 | $math->div( |
||
| 222 | $math->sub( |
||
| 223 | $p, |
||
| 224 | gmp_init(5, 10) |
||
| 225 | ), |
||
| 226 | $eight |
||
| 227 | ) |
||
| 228 | ) |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | //shouldn't get here |
||
| 232 | } |
||
| 233 | |||
| 234 | for ($b = gmp_init(2, 10); $math->cmp($b, $p) < 0; $b = gmp_add($b, gmp_init(1, 10))) { |
||
|
1 ignored issue
–
show
|
|||
| 235 | if ($math->jacobi( |
||
| 236 | $math->sub( |
||
| 237 | $math->mul($b, $b), |
||
|
1 ignored issue
–
show
|
|||
| 238 | $math->mul($four, $a) |
||
| 239 | ), |
||
| 240 | $p |
||
| 241 | ) == -1 |
||
| 242 | ) { |
||
| 243 | $f = array($a, $math->sub($zero, $b), $one); |
||
|
1 ignored issue
–
show
|
|||
| 244 | |||
| 245 | $ff = $this->polynomialPowMod( |
||
| 246 | array($zero, $one), |
||
| 247 | $math->div( |
||
| 248 | $math->add( |
||
| 249 | $p, |
||
| 250 | $one |
||
| 251 | ), |
||
| 252 | $two |
||
| 253 | ), |
||
| 254 | $f, |
||
| 255 | $p |
||
| 256 | ); |
||
| 257 | |||
| 258 | if ($math->equals($ff[1], $zero)) { |
||
| 259 | return $ff[0]; |
||
| 260 | } |
||
| 261 | // if we got here no b was found |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | throw new \InvalidArgumentException('Unable to calculate square root mod p!'); |
||
| 267 | |||
| 268 | } |
||
| 269 | } |
||
| 270 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.