| Conditions | 38 |
| Paths | 43 |
| Total Lines | 133 |
| Code Lines | 92 |
| 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 |
||
| 42 | private function parseOCRASuite($ocraSuite) { |
||
| 43 | if (!is_string($ocraSuite)) { |
||
| 44 | throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
||
| 45 | } |
||
| 46 | |||
| 47 | $ocraSuite = strtoupper($ocraSuite); |
||
| 48 | $this->OCRASuite = $ocraSuite; |
||
| 49 | |||
| 50 | $s = explode(':', $ocraSuite); |
||
| 51 | if (count($s) != 3) { |
||
| 52 | throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
||
| 53 | } |
||
| 54 | |||
| 55 | $algo = explode('-', $s[0]); |
||
| 56 | if (count($algo) != 2) { |
||
| 57 | throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($algo[0] !== 'OCRA') { |
||
| 61 | throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($algo[1] !== '1') { |
||
| 65 | throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
||
| 66 | } |
||
| 67 | $this->OCRAVersion = $algo[1]; |
||
| 68 | |||
| 69 | $cf = explode('-', $s[1]); |
||
| 70 | if (count($cf) != 3) { |
||
| 71 | throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($cf[0] !== 'HOTP') { |
||
| 75 | throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
||
| 76 | } |
||
| 77 | $this->CryptoFunctionType = $cf[0]; |
||
| 78 | |||
| 79 | if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
||
| 80 | throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
||
| 81 | } |
||
| 82 | $this->CryptoFunctionHash = $cf[1]; |
||
| 83 | $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
||
| 84 | |||
| 85 | if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
||
| 86 | throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
||
| 87 | } |
||
| 88 | $this->CryptoFunctionTruncation = intval($cf[2]); |
||
| 89 | |||
| 90 | $di = explode('-', $s[2]); |
||
| 91 | if (count($cf) == 0) { |
||
| 92 | throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
||
| 93 | } |
||
| 94 | |||
| 95 | $data_input = array(); |
||
| 96 | foreach($di as $elem) { |
||
| 97 | $letter = $elem[0]; |
||
| 98 | if (array_key_exists($letter, $data_input)) { |
||
| 99 | throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
||
| 100 | } |
||
| 101 | $data_input[$letter] = 1; |
||
| 102 | |||
| 103 | if ($letter === 'C' && strlen($elem) == 1) { |
||
| 104 | $this->C = TRUE; |
||
| 105 | } elseif ($letter === 'Q') { |
||
| 106 | if (strlen($elem) == 1) { |
||
| 107 | $this->Q = TRUE; |
||
| 108 | } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
||
| 109 | $q_len = intval($match[2]); |
||
| 110 | if ($q_len < 4 || $q_len > 64) { |
||
| 111 | throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
||
| 112 | } |
||
| 113 | $this->Q = TRUE; |
||
| 114 | $this->QType = $match[1]; |
||
| 115 | $this->QLength = $q_len; |
||
| 116 | } else { |
||
| 117 | throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
||
| 118 | } |
||
| 119 | } elseif ($letter === 'P') { |
||
| 120 | if (strlen($elem) == 1) { |
||
| 121 | $this->P = TRUE; |
||
| 122 | } else { |
||
| 123 | $p_algo = substr($elem, 1); |
||
| 124 | if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
||
| 125 | throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
||
| 126 | } |
||
| 127 | $this->P = TRUE; |
||
| 128 | $this->PType = $p_algo; |
||
| 129 | $this->PLength = $this->supportedHashFunctions[$p_algo]; |
||
| 130 | } |
||
| 131 | } elseif ($letter === 'S') { |
||
| 132 | if (strlen($elem) == 1) { |
||
| 133 | $this->S = TRUE; |
||
| 134 | } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
||
| 135 | $s_len = intval($match[1]); |
||
| 136 | if ($s_len <= 0 || $s_len > 512) { |
||
| 137 | throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->S = TRUE; |
||
| 141 | $this->SLength = $s_len; |
||
| 142 | } else { |
||
| 143 | throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
||
| 144 | } |
||
| 145 | } elseif ($letter === 'T') { |
||
| 146 | if (strlen($elem) == 1) { |
||
| 147 | $this->T = TRUE; |
||
| 148 | } elseif (preg_match('/^T(\d+[HMS])+$/', $elem)) { |
||
| 149 | preg_match_all('/(\d+)([HMS])/', $elem, $match); |
||
| 150 | |||
| 151 | if (count($match[1]) !== count(array_unique($match[2]))) { |
||
| 152 | throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
||
| 153 | } |
||
| 154 | |||
| 155 | $length = 0; |
||
| 156 | for ($i = 0; $i < count($match[1]); $i++) { |
||
| 157 | $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
||
| 158 | } |
||
| 159 | if ($length <= 0) { |
||
| 160 | throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
||
| 161 | } |
||
| 162 | |||
| 163 | $this->T = TRUE; |
||
| 164 | $this->TLength = $length; |
||
| 165 | } else { |
||
| 166 | throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
||
| 167 | } |
||
| 168 | } else { |
||
| 169 | throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | if (!$this->Q) { |
||
| 174 | throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
||
| 175 | } |
||
| 297 |