| Total Complexity | 52 |
| Total Lines | 262 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Der often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Der, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Der extends Oids { |
||
| 8 | protected $tag; |
||
| 9 | protected $len; |
||
| 10 | protected $value; |
||
| 11 | protected $class; |
||
| 12 | protected $constructed; |
||
| 13 | protected $buffer; |
||
| 14 | protected $stack = []; |
||
| 15 | protected $i; |
||
| 16 | private $ignoredextensions = [ |
||
|
|
|||
| 17 | 'netscape-cert-type' => 1, |
||
| 18 | ]; |
||
| 19 | private $id; |
||
| 20 | |||
| 21 | protected function init($der) { |
||
| 22 | $this->buffer = $der; |
||
| 23 | $this->i = 0; |
||
| 24 | $this->id = uniqid(); |
||
| 25 | } |
||
| 26 | |||
| 27 | protected function dump($note = '') { |
||
| 28 | $z = strlen((string) $this->buffer) - $this->i; |
||
| 29 | print_r("{$note}\n"); |
||
| 30 | print_r("len: {$z}\n"); |
||
| 31 | print_r(chunk_split(bin2hex(substr((string) $this->buffer, $this->i)), 2, ':')); |
||
| 32 | echo "\n"; |
||
| 33 | } |
||
| 34 | |||
| 35 | protected function pr($note = '') { |
||
| 36 | $savei = $this->i; |
||
| 37 | $byte = ord($this->buffer[$this->i++]); |
||
| 38 | $tag = $byte & 0x1F; |
||
| 39 | $class = $byte & 0xC0; |
||
| 40 | $constructed = $byte & 0x20; |
||
| 41 | $len = $this->vallen(); |
||
| 42 | $this->i = $savei; |
||
| 43 | print_r("{$note}\n"); |
||
| 44 | print_r("i : {$this->i}\n"); |
||
| 45 | print_r("len: {$len}\n"); |
||
| 46 | print_r("class: {$class}\n"); |
||
| 47 | print_r("tag : {$tag}\n"); |
||
| 48 | print_r(chunk_split(bin2hex(substr((string) $this->buffer, $this->i, min(32, strlen((string) $this->buffer) - $this->i))) . "\n", 2, ':')); |
||
| 49 | print_r("---\n"); |
||
| 50 | } |
||
| 51 | |||
| 52 | private function tlv($expectedtag = null) { |
||
| 64 | } |
||
| 65 | |||
| 66 | protected function next($expectedtag = null) { |
||
| 67 | $this->tlv($expectedtag); |
||
| 68 | if ($this->constructed) { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | $value = substr((string) $this->buffer, $this->i, $this->len); |
||
| 72 | if ($this->class == 0 || $this->class == 0x80) { |
||
| 73 | if ($this->tag == 2 || $this->tag == 10) { # ints and enums |
||
| 74 | $int = 0; |
||
| 75 | foreach (str_split($value) as $byte) { |
||
| 76 | $int = bcmul($int, '256', 0); |
||
| 77 | $int = bcadd($int, ord($byte), 0); |
||
| 78 | } |
||
| 79 | $this->value = $int; |
||
| 80 | } |
||
| 81 | elseif ($this->tag == 1) { # boolean |
||
| 82 | $this->value = ord($value) != 0; |
||
| 83 | } |
||
| 84 | elseif ($this->tag == 3) { # bit string |
||
| 85 | $this->value = $value; |
||
| 86 | } |
||
| 87 | elseif ($this->tag == 5) { # null |
||
| 88 | $this->value = null; |
||
| 89 | } |
||
| 90 | else { |
||
| 91 | $this->value = $value; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | $this->i += $this->len; |
||
| 95 | |||
| 96 | return $this->value; |
||
| 97 | } |
||
| 98 | |||
| 99 | protected function der($expectedtag = null, $pass = false) { |
||
| 100 | $oldi = $this->i; |
||
| 101 | $this->tlv($expectedtag); |
||
| 102 | $i = $this->i; |
||
| 103 | if (!$pass) { |
||
| 104 | $this->i = $oldi; |
||
| 105 | } |
||
| 106 | else { |
||
| 107 | $this->i += $this->len; |
||
| 108 | } |
||
| 109 | |||
| 110 | return substr((string) $this->buffer, $oldi, $this->len + $i - $oldi); |
||
| 111 | } |
||
| 112 | |||
| 113 | /* |
||
| 114 | * if provided with a tag and the tag is equal to the current tag |
||
| 115 | * peek considers it EXPLICIT, consumes it and return true |
||
| 116 | */ |
||
| 117 | protected function peek($tag = null) { |
||
| 118 | $t = null; |
||
| 119 | if ($this->i < end($this->stack)) { |
||
| 120 | $t = ord($this->buffer[$this->i]) & 0x1F; |
||
| 121 | } |
||
| 122 | if ($tag !== null) { |
||
| 123 | if ($t === $tag) { |
||
| 124 | $this->next($tag); |
||
| 125 | |||
| 126 | return true; |
||
| 127 | } |
||
| 128 | |||
| 129 | return false; |
||
| 130 | } |
||
| 131 | |||
| 132 | return $t; |
||
| 133 | } |
||
| 134 | |||
| 135 | protected function vallen() { |
||
| 136 | $byte = ord($this->buffer[$this->i++]); |
||
| 137 | $res = $len = $byte & 0x7F; |
||
| 138 | if ($byte >= 0x80) { |
||
| 139 | $res = 0; |
||
| 140 | for ($c = 0; $c < $len; ++$c) { |
||
| 141 | $res = $res * 256 + ord($this->buffer[$this->i++]); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return $res; |
||
| 146 | } |
||
| 147 | |||
| 148 | protected function beginsequence($tag = 16) { |
||
| 149 | $this->begin($tag); |
||
| 150 | } |
||
| 151 | |||
| 152 | protected function beginset($tag = 17) { |
||
| 153 | $this->begin($tag); |
||
| 154 | } |
||
| 155 | |||
| 156 | protected function begin($tag) { |
||
| 157 | $this->next($tag); |
||
| 158 | array_push($this->stack, $this->i + $this->len); |
||
| 159 | } |
||
| 160 | |||
| 161 | protected function in() { |
||
| 163 | } |
||
| 164 | |||
| 165 | protected function end() { |
||
| 166 | $end = array_pop($this->stack); |
||
| 167 | if ($end != $this->i) { |
||
| 168 | trigger_error("sequence or set length does not match: {$end} != {$this->i}", E_USER_ERROR); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | protected function extensions() { |
||
| 201 | } |
||
| 202 | |||
| 203 | protected function signatureAlgorithm() { |
||
| 212 | } |
||
| 213 | |||
| 214 | protected function name($tag = null) { |
||
| 231 | } |
||
| 232 | |||
| 233 | protected function oid($tag = 6) { |
||
| 234 | $v = $this->oid_($this->next($tag)); |
||
| 235 | |||
| 236 | return $this->oids[$v] ?? $v; |
||
| 237 | } |
||
| 238 | |||
| 239 | protected function oid_($oid) { |
||
| 240 | $len = strlen((string) $oid); |
||
| 241 | $v = ""; |
||
| 242 | $n = 0; |
||
| 243 | for ($c = 0; $c < $len; ++$c) { |
||
| 244 | $x = ord($oid[$c]); |
||
| 245 | $n = $n * 128 + ($x & 0x7F); |
||
| 246 | if ($x <= 127) { |
||
| 247 | $v .= $v ? '.' . $n : ((int) ($n / 40) . '.' . ($n % 40)); |
||
| 248 | $n = 0; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | return $v . '*'; |
||
| 253 | } |
||
| 254 | |||
| 255 | protected function time($tag = null) { |
||
| 256 | $time = $this->next($tag); |
||
| 257 | if ($this->tag == 23) { |
||
| 258 | $time = (substr((string) $time, 0, 2) < 50 ? '20' : '19') . $time; |
||
| 259 | } |
||
| 260 | elseif ($this->tag != 24) { |
||
| 261 | trigger_error('expected der utc or generalized time', E_USER_ERROR); |
||
| 262 | } |
||
| 263 | |||
| 264 | return $time; |
||
| 265 | } |
||
| 266 | |||
| 267 | protected function keyident($tag = 4) { |
||
| 269 | } |
||
| 270 | } |
||
| 271 |