| Conditions | 12 |
| Paths | 36 |
| Total Lines | 68 |
| Code Lines | 40 |
| Lines | 12 |
| Ratio | 17.65 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 53 | private function alphaID($in, $to_num = false, $pad_up = 3, $passKey = "prephe.ro") |
||
| 54 | { |
||
| 55 | $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
||
| 56 | if ($passKey !== null) { |
||
| 57 | // Although this function's purpose is to just make the |
||
| 58 | // ID short - and not so much secure, |
||
| 59 | // with this patch by Simon Franz (http://blog.snaky.org/) |
||
| 60 | // you can optionally supply a password to make it harder |
||
| 61 | // to calculate the corresponding numeric ID |
||
| 62 | |||
| 63 | for ($n = 0; $n<strlen($index); $n++) { |
||
| 64 | $i[] = substr($index, $n, 1); |
||
| 65 | } |
||
| 66 | |||
| 67 | $passhash = hash('sha256', $passKey); |
||
| 68 | $passhash = (strlen($passhash) < strlen($index)) |
||
| 69 | ? hash('sha512', $passKey) |
||
| 70 | : $passhash; |
||
| 71 | |||
| 72 | for ($n=0; $n < strlen($index); $n++) { |
||
| 73 | $p[] = substr($passhash, $n, 1); |
||
| 74 | } |
||
| 75 | |||
| 76 | array_multisort($p, SORT_DESC, $i); |
||
| 77 | $index = implode($i); |
||
| 78 | } |
||
| 79 | |||
| 80 | $base = strlen($index); |
||
| 81 | |||
| 82 | if ($to_num) { |
||
| 83 | // Digital number <<-- alphabet letter code |
||
| 84 | $in = strrev($in); |
||
| 85 | $out = 0; |
||
| 86 | $len = strlen($in) - 1; |
||
| 87 | for ($t = 0; $t <= $len; $t++) { |
||
| 88 | $bcpow = bcpow($base, $len - $t); |
||
| 89 | $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow; |
||
| 90 | } |
||
| 91 | |||
| 92 | View Code Duplication | if (is_numeric($pad_up)) { |
|
| 93 | $pad_up--; |
||
| 94 | if ($pad_up > 0) { |
||
| 95 | $out -= pow($base, $pad_up); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | $out = sprintf('%F', $out); |
||
| 99 | $out = substr($out, 0, strpos($out, '.')); |
||
| 100 | } else { |
||
| 101 | // Digital number -->> alphabet letter code |
||
| 102 | View Code Duplication | if (is_numeric($pad_up)) { |
|
| 103 | $pad_up--; |
||
| 104 | if ($pad_up > 0) { |
||
| 105 | $in += pow($base, $pad_up); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | $out = ""; |
||
| 110 | for ($t = floor(log($in, $base)); $t >= 0; $t--) { |
||
| 111 | $bcp = bcpow($base, $t); |
||
| 112 | $a = floor($in / $bcp) % $base; |
||
| 113 | $out = $out . substr($index, $a, 1); |
||
| 114 | $in = $in - ($a * $bcp); |
||
| 115 | } |
||
| 116 | $out = strrev($out); // reverse |
||
| 117 | } |
||
| 118 | |||
| 119 | return $out; |
||
| 120 | } |
||
| 121 | } |
||
| 122 |