| Conditions | 98 |
| Paths | > 20000 |
| Total Lines | 261 |
| Code Lines | 193 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | 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 |
||
| 288 | public function map($map, $skeleton = null) |
||
| 289 | { |
||
| 290 | $null = null; |
||
| 291 | if ($skeleton === null && $this->reader->pos() !== 0) { |
||
| 292 | $this->reader->rewind(); |
||
| 293 | } |
||
| 294 | $skeleton = $skeleton ?? $this->structure()[0] ?? null; |
||
| 295 | if (!isset($skeleton)) { |
||
| 296 | throw new ASN1Exception('No decoded data for map'); |
||
| 297 | } |
||
| 298 | if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
||
| 299 | if ($map['tag'] === ASN1::TYPE_CHOICE) { |
||
| 300 | foreach ($map['children'] as $child) { |
||
| 301 | if (isset($child['name']) && (int)$skeleton['tag'] === (int)$child['name']) { |
||
| 302 | $map = $child; |
||
| 303 | if (isset($child['value']) && $child['value']) { |
||
| 304 | return $child['value']; |
||
| 305 | } |
||
| 306 | break; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
||
| 312 | if (isset($map['implicit']) && $map['implicit']) { |
||
| 313 | $skeleton['class'] = ASN1::CLASS_UNIVERSAL; |
||
| 314 | $skeleton['tag'] = $map['tag']; |
||
| 315 | if (isset($skeleton['value'])) { |
||
| 316 | $skeleton['value'] = $this->decodeContents($skeleton['tag'], true, $skeleton['value']); |
||
| 317 | } |
||
| 318 | } else { |
||
| 319 | $skeleton = $skeleton['children'][0] ?? null; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | if ($map['tag'] === ASN1::TYPE_CHOICE) { |
||
| 323 | foreach ($map['children'] as $child) { |
||
| 324 | if ($skeleton['tag'] === $child['tag']) { |
||
| 325 | $map = $child; |
||
| 326 | if (isset($child['value']) && $child['value']) { |
||
| 327 | return $child['value']; |
||
| 328 | } |
||
| 329 | break; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | if (in_array($map['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET]) && |
||
| 334 | in_array($skeleton['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET])) { |
||
| 335 | $map['tag'] = $skeleton['tag']; |
||
| 336 | } |
||
| 337 | if ($map['tag'] === ASN1::TYPE_ANY && isset($skeleton['tag'])) { |
||
| 338 | $map['tag'] = $skeleton['tag']; |
||
| 339 | } |
||
| 340 | if (!in_array($map['tag'], [ASN1::TYPE_ANY, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_ANY_DER]) && |
||
| 341 | $map['tag'] !== $skeleton['tag'] |
||
| 342 | ) { |
||
| 343 | if (!isset($map['optional']) || !$map['optional']) { |
||
| 344 | throw new ASN1Exception('Decoded data does not match mapping - ' . $skeleton['tag']); |
||
| 345 | } |
||
| 346 | return $null; |
||
| 347 | } else { |
||
| 348 | switch ($map['tag']) { |
||
| 349 | case ASN1::TYPE_ANY_DER: |
||
| 350 | $temp = $this->reader->chunk($skeleton['start'], $skeleton['length']); |
||
| 351 | return $temp; |
||
| 352 | case ASN1::TYPE_ANY_SKIP: |
||
| 353 | return $null; |
||
| 354 | case ASN1::TYPE_ANY_RAW: |
||
| 355 | return $skeleton['value'] ?? null; |
||
| 356 | case ASN1::TYPE_SET: |
||
| 357 | if (isset($map['repeat'])) { |
||
| 358 | $result = []; |
||
| 359 | foreach ($skeleton['children'] as $v) { |
||
| 360 | $result[] = $this->map($map['repeat'], $v); |
||
| 361 | } |
||
| 362 | return $result; |
||
| 363 | } else { |
||
| 364 | if (!isset($map['children'])) { |
||
| 365 | return $null; |
||
| 366 | } |
||
| 367 | $temp = $skeleton['children']; |
||
| 368 | $result = []; |
||
| 369 | // named first |
||
| 370 | foreach ($map['children'] as $k => $v) { |
||
| 371 | if (isset($v['name'])) { |
||
| 372 | $result[$k] = null; |
||
| 373 | foreach ($temp as $kk => $vv) { |
||
| 374 | if ($vv['class'] !== ASN1::CLASS_UNIVERSAL && (int)$v['name'] === $vv['tag']) { |
||
| 375 | try { |
||
| 376 | if (isset($v['implicit']) && $v['implicit']) { |
||
| 377 | $vv['class'] = ASN1::CLASS_UNIVERSAL; |
||
| 378 | $vv['tag'] = $map['tag']; |
||
| 379 | if (isset($vv['value'])) { |
||
| 380 | $vv['value'] = $this->decodeContents($vv['tag'], true, $vv['value']); |
||
| 381 | } |
||
| 382 | } else { |
||
| 383 | $vv = $vv['children'][0] ?? null; |
||
| 384 | } |
||
| 385 | $result[$k] = $this->map($v, $vv); |
||
| 386 | unset($temp[$kk]); |
||
| 387 | break; |
||
| 388 | } catch (ASN1Exception $e) { |
||
| 389 | // continue trying other children in case of failure |
||
| 390 | } |
||
| 391 | } |
||
| 392 | } |
||
| 393 | if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
||
| 394 | throw new ASN1Exception('Missing tagged type - ' . $k); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | } |
||
| 398 | foreach ($map['children'] as $k => $v) { |
||
| 399 | if (isset($v['name'])) { |
||
| 400 | continue; |
||
| 401 | } |
||
| 402 | $result[$k] = null; |
||
| 403 | foreach ($temp as $kk => $vv) { |
||
| 404 | if ($v['tag'] === $vv['tag'] || |
||
| 405 | in_array( |
||
| 406 | $v['tag'], |
||
| 407 | [ |
||
| 408 | ASN1::TYPE_ANY, |
||
| 409 | ASN1::TYPE_ANY_DER, |
||
| 410 | ASN1::TYPE_ANY_RAW, |
||
| 411 | ASN1::TYPE_ANY_SKIP, |
||
| 412 | ASN1::TYPE_CHOICE |
||
| 413 | ] |
||
| 414 | ) |
||
| 415 | ) { |
||
| 416 | try { |
||
| 417 | $result[$k] = $this->map($v, $vv); |
||
| 418 | unset($temp[$kk]); |
||
| 419 | break; |
||
| 420 | } catch (ASN1Exception $e) { |
||
| 421 | $result[$k] = null; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
||
| 426 | throw new ASN1Exception('Decoded data does not match mapping - ' . $k); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | return $result; |
||
| 430 | } |
||
| 431 | break; |
||
| 432 | case ASN1::TYPE_SEQUENCE: |
||
| 433 | if (isset($map['repeat'])) { |
||
| 434 | $result = []; |
||
| 435 | foreach ($skeleton['children'] as $v) { |
||
| 436 | $result[] = $this->map($map['repeat'], $v); |
||
| 437 | } |
||
| 438 | return $result; |
||
| 439 | } else { |
||
| 440 | if (!isset($map['children'])) { |
||
| 441 | return $null; |
||
| 442 | } |
||
| 443 | $result = []; |
||
| 444 | foreach ($skeleton['children'] as $kk => $vv) { |
||
| 445 | foreach ($map['children'] as $k => $v) { |
||
| 446 | if (isset($v['name'])) { |
||
| 447 | $result[$k] = null; |
||
| 448 | if ($vv['class'] !== ASN1::CLASS_UNIVERSAL && (int)$v['name'] === $vv['tag']) { |
||
| 449 | if (isset($v['implicit']) && $v['implicit']) { |
||
| 450 | $vv['class'] = ASN1::CLASS_UNIVERSAL; |
||
| 451 | $vv['tag'] = $v['tag']; |
||
| 452 | if (isset($vv['value'])) { |
||
| 453 | $vv['value'] = $this->decodeContents($vv['tag'], true, $vv['value']); |
||
| 454 | } |
||
| 455 | } else { |
||
| 456 | $vv = $vv['children'][0] ?? null; |
||
| 457 | } |
||
| 458 | $result[$k] = $this->map($v, $vv); |
||
| 459 | unset($map['children'][$k]); |
||
| 460 | break; |
||
| 461 | } else { |
||
| 462 | if (!isset($v['optional']) || !$v['optional']) { |
||
| 463 | throw new ASN1Exception('Missing tagged type - ' . $k); |
||
| 464 | } |
||
| 465 | } |
||
| 466 | unset($map['children'][$k]); |
||
| 467 | continue; |
||
| 468 | } |
||
| 469 | if ($v['tag'] === $vv['tag'] || |
||
| 470 | in_array( |
||
| 471 | $v['tag'], |
||
| 472 | [ |
||
| 473 | ASN1::TYPE_ANY, |
||
| 474 | ASN1::TYPE_ANY_DER, |
||
| 475 | ASN1::TYPE_ANY_RAW, |
||
| 476 | ASN1::TYPE_ANY_SKIP, |
||
| 477 | ASN1::TYPE_CHOICE |
||
| 478 | ] |
||
| 479 | ) |
||
| 480 | ) { |
||
| 481 | try { |
||
| 482 | unset($map['children'][$k]); |
||
| 483 | $result[$k] = $this->map($v, $vv); |
||
| 484 | break; |
||
| 485 | } catch (ASN1Exception $e) { |
||
| 486 | // continue trying other children in case of failure |
||
| 487 | } |
||
| 488 | } |
||
| 489 | if (!isset($v['optional']) || !$v['optional']) { |
||
| 490 | throw new ASN1Exception('Missing type - ' . $k); |
||
| 491 | } else { |
||
| 492 | $result[$k] = null; |
||
| 493 | unset($map['children'][$k]); |
||
| 494 | } |
||
| 495 | } |
||
| 496 | } |
||
| 497 | return $result; |
||
| 498 | } |
||
| 499 | break; |
||
| 500 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
||
| 501 | $temp = isset($map['resolve']) && $map['resolve'] ? |
||
| 502 | ASN1::OIDtoText($skeleton['value']) : |
||
| 503 | $skeleton['value']; |
||
| 504 | return $temp; |
||
| 505 | case ASN1::TYPE_OCTET_STRING: |
||
| 506 | if (isset($map['der']) && $map['der']) { |
||
| 507 | $temp = static::fromString($skeleton['value']); |
||
| 508 | $temp = isset($map['map']) ? $temp->map($map['map']) : $temp->values(); |
||
| 509 | return $temp; |
||
| 510 | } else { |
||
| 511 | $temp = isset($map['raw']) && $map['raw'] ? |
||
| 512 | $skeleton['value'] : |
||
| 513 | base64_encode($skeleton['value']); |
||
| 514 | return $temp; |
||
| 515 | } |
||
| 516 | break; |
||
| 517 | case ASN1::TYPE_INTEGER: |
||
| 518 | $base = isset($map['base']) && (int)$map['base'] ? (int)$map['base'] : 10; |
||
| 519 | if ($base < 3) { |
||
| 520 | $result = $skeleton['value']; |
||
| 521 | } else { |
||
| 522 | if (strlen($skeleton['value']) > 53 && $base === 16) { |
||
| 523 | $hex = ''; |
||
| 524 | for ($i = strlen($skeleton['value']) - 4; $i >= 0; $i-=4) { |
||
| 525 | $hex .= dechex((int)bindec(substr($skeleton['value'], $i, 4))); |
||
| 526 | } |
||
| 527 | $result = strrev($hex); |
||
| 528 | } else { |
||
| 529 | $temp = base_convert($skeleton['value'], 2, $base); |
||
| 530 | if ($base === 10) { |
||
| 531 | $temp = (int)$temp; |
||
| 532 | } |
||
| 533 | $result = $temp; |
||
| 534 | } |
||
| 535 | } |
||
| 536 | if (isset($map['map']) && isset($map['map'][$result])) { |
||
| 537 | $result = $map['map'][$result]; |
||
| 538 | } |
||
| 539 | return $result; |
||
| 540 | case ASN1::TYPE_UTC_TIME: |
||
| 541 | case ASN1::TYPE_GENERALIZED_TIME: |
||
| 542 | return $skeleton['value']; |
||
| 543 | default: |
||
| 544 | $result = $skeleton['value']; |
||
| 545 | if (isset($map['map']) && isset($map['map'][$result])) { |
||
| 546 | $result = $map['map'][$result]; |
||
| 547 | } |
||
| 548 | return $result; |
||
| 549 | } |
||
| 553 |