Complex classes like UnspecifiedType 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 UnspecifiedType, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 18 | class UnspecifiedType implements ElementBase | ||
| 19 | { | ||
| 20 | /** | ||
| 21 | * The wrapped element. | ||
| 22 | * | ||
| 23 | * @var Element | ||
| 24 | */ | ||
| 25 | private $_element; | ||
| 26 | |||
| 27 | /** | ||
| 28 | * Constructor. | ||
| 29 | * | ||
| 30 | * @param Element $el | ||
| 31 | */ | ||
| 32 | 96 | public function __construct(Element $el) | |
| 36 | |||
| 37 | /** | ||
| 38 | * Initialize from DER data. | ||
| 39 | * | ||
| 40 | * @param string $data DER encoded data | ||
| 41 | * @return self | ||
| 42 | */ | ||
| 43 | 3 | public static function fromDER(string $data): self | |
| 47 | |||
| 48 | /** | ||
| 49 | * Initialize from ElementBase interface. | ||
| 50 | * | ||
| 51 | * @param ElementBase $el | ||
| 52 | * @return self | ||
| 53 | */ | ||
| 54 | 2 | public static function fromElementBase(ElementBase $el): self | |
| 62 | |||
| 63 | /** | ||
| 64 | * Compatibility method to dispatch calls to the wrapped element. | ||
| 65 | * | ||
| 66 | * @deprecated Use <code>as*</code> accessor methods to ensure strict type | ||
| 67 | * @param string $mtd Method name | ||
| 68 | * @param array $args Arguments | ||
| 69 | * @return mixed | ||
| 70 | */ | ||
| 71 | 2 | public function __call($mtd, array $args) | |
| 75 | |||
| 76 | /** | ||
| 77 | * Get the wrapped element as a context specific tagged type. | ||
| 78 | * | ||
| 79 | * @throws \UnexpectedValueException If the element is not tagged | ||
| 80 | * @return TaggedType | ||
| 81 | */ | ||
| 82 | 2 | public function asTagged(): TaggedType | |
| 90 | |||
| 91 | /** | ||
| 92 | * Get the wrapped element as an application specific type. | ||
| 93 | * | ||
| 94 | * @throws \UnexpectedValueException If element is not application specific | ||
| 95 | * @return Tagged\ApplicationType | ||
| 96 | */ | ||
| 97 | 2 | public function asApplication(): Tagged\ApplicationType | |
| 106 | |||
| 107 | /** | ||
| 108 | * Get the wrapped element as a boolean type. | ||
| 109 | * | ||
| 110 | * @throws \UnexpectedValueException If the element is not a boolean | ||
| 111 | * @return Primitive\Boolean | ||
| 112 | */ | ||
| 113 | 4 | public function asBoolean(): Primitive\Boolean | |
| 121 | |||
| 122 | /** | ||
| 123 | * Get the wrapped element as an integer type. | ||
| 124 | * | ||
| 125 | * @throws \UnexpectedValueException If the element is not an integer | ||
| 126 | * @return Primitive\Integer | ||
| 127 | */ | ||
| 128 | 4 | public function asInteger(): Primitive\Integer | |
| 136 | |||
| 137 | /** | ||
| 138 | * Get the wrapped element as a bit string type. | ||
| 139 | * | ||
| 140 | * @throws \UnexpectedValueException If the element is not a bit string | ||
| 141 | * @return Primitive\BitString | ||
| 142 | */ | ||
| 143 | 2 | public function asBitString(): Primitive\BitString | |
| 151 | |||
| 152 | /** | ||
| 153 | * Get the wrapped element as an octet string type. | ||
| 154 | * | ||
| 155 | * @throws \UnexpectedValueException If the element is not an octet string | ||
| 156 | * @return Primitive\OctetString | ||
| 157 | */ | ||
| 158 | 2 | public function asOctetString(): Primitive\OctetString | |
| 166 | |||
| 167 | /** | ||
| 168 | * Get the wrapped element as a null type. | ||
| 169 | * | ||
| 170 | * @throws \UnexpectedValueException If the element is not a null | ||
| 171 | * @return Primitive\NullType | ||
| 172 | */ | ||
| 173 | 10 | public function asNull(): Primitive\NullType | |
| 181 | |||
| 182 | /** | ||
| 183 | * Get the wrapped element as an object identifier type. | ||
| 184 | * | ||
| 185 | * @throws \UnexpectedValueException If the element is not an object | ||
| 186 | * identifier | ||
| 187 | * @return Primitive\ObjectIdentifier | ||
| 188 | */ | ||
| 189 | 2 | public function asObjectIdentifier(): Primitive\ObjectIdentifier | |
| 198 | |||
| 199 | /** | ||
| 200 | * Get the wrapped element as an object descriptor type. | ||
| 201 | * | ||
| 202 | * @throws \UnexpectedValueException If the element is not an object | ||
| 203 | * descriptor | ||
| 204 | * @return Primitive\ObjectDescriptor | ||
| 205 | */ | ||
| 206 | 2 | public function asObjectDescriptor(): Primitive\ObjectDescriptor | |
| 215 | |||
| 216 | /** | ||
| 217 | * Get the wrapped element as a real type. | ||
| 218 | * | ||
| 219 | * @throws \UnexpectedValueException If the element is not a real | ||
| 220 | * @return Primitive\Real | ||
| 221 | */ | ||
| 222 | 2 | public function asReal(): Primitive\Real | |
| 230 | |||
| 231 | /** | ||
| 232 | * Get the wrapped element as an enumerated type. | ||
| 233 | * | ||
| 234 | * @throws \UnexpectedValueException If the element is not an enumerated | ||
| 235 | * @return Primitive\Enumerated | ||
| 236 | */ | ||
| 237 | 2 | public function asEnumerated(): Primitive\Enumerated | |
| 245 | |||
| 246 | /** | ||
| 247 | * Get the wrapped element as a UTF8 string type. | ||
| 248 | * | ||
| 249 | * @throws \UnexpectedValueException If the element is not a UTF8 string | ||
| 250 | * @return Primitive\UTF8String | ||
| 251 | */ | ||
| 252 | 2 | public function asUTF8String(): Primitive\UTF8String | |
| 260 | |||
| 261 | /** | ||
| 262 | * Get the wrapped element as a relative OID type. | ||
| 263 | * | ||
| 264 | * @throws \UnexpectedValueException If the element is not a relative OID | ||
| 265 | * @return Primitive\RelativeOID | ||
| 266 | */ | ||
| 267 | 2 | public function asRelativeOID(): Primitive\RelativeOID | |
| 275 | |||
| 276 | /** | ||
| 277 | * Get the wrapped element as a sequence type. | ||
| 278 | * | ||
| 279 | * @throws \UnexpectedValueException If the element is not a sequence | ||
| 280 | * @return Constructed\Sequence | ||
| 281 | */ | ||
| 282 | 2 | public function asSequence(): Constructed\Sequence | |
| 290 | |||
| 291 | /** | ||
| 292 | * Get the wrapped element as a set type. | ||
| 293 | * | ||
| 294 | * @throws \UnexpectedValueException If the element is not a set | ||
| 295 | * @return Constructed\Set | ||
| 296 | */ | ||
| 297 | 2 | public function asSet(): Constructed\Set | |
| 305 | |||
| 306 | /** | ||
| 307 | * Get the wrapped element as a numeric string type. | ||
| 308 | * | ||
| 309 | * @throws \UnexpectedValueException If the element is not a numeric string | ||
| 310 | * @return Primitive\NumericString | ||
| 311 | */ | ||
| 312 | 2 | public function asNumericString(): Primitive\NumericString | |
| 320 | |||
| 321 | /** | ||
| 322 | * Get the wrapped element as a printable string type. | ||
| 323 | * | ||
| 324 | * @throws \UnexpectedValueException If the element is not a printable | ||
| 325 | * string | ||
| 326 | * @return Primitive\PrintableString | ||
| 327 | */ | ||
| 328 | 2 | public function asPrintableString(): Primitive\PrintableString | |
| 336 | |||
| 337 | /** | ||
| 338 | * Get the wrapped element as a T61 string type. | ||
| 339 | * | ||
| 340 | * @throws \UnexpectedValueException If the element is not a T61 string | ||
| 341 | * @return Primitive\T61String | ||
| 342 | */ | ||
| 343 | 2 | public function asT61String(): Primitive\T61String | |
| 351 | |||
| 352 | /** | ||
| 353 | * Get the wrapped element as a videotex string type. | ||
| 354 | * | ||
| 355 | * @throws \UnexpectedValueException If the element is not a videotex string | ||
| 356 | * @return Primitive\VideotexString | ||
| 357 | */ | ||
| 358 | 2 | public function asVideotexString(): Primitive\VideotexString | |
| 366 | |||
| 367 | /** | ||
| 368 | * Get the wrapped element as a IA5 string type. | ||
| 369 | * | ||
| 370 | * @throws \UnexpectedValueException If the element is not a IA5 string | ||
| 371 | * @return Primitive\IA5String | ||
| 372 | */ | ||
| 373 | 2 | public function asIA5String(): Primitive\IA5String | |
| 381 | |||
| 382 | /** | ||
| 383 | * Get the wrapped element as an UTC time type. | ||
| 384 | * | ||
| 385 | * @throws \UnexpectedValueException If the element is not a UTC time | ||
| 386 | * @return Primitive\UTCTime | ||
| 387 | */ | ||
| 388 | 2 | public function asUTCTime(): Primitive\UTCTime | |
| 396 | |||
| 397 | /** | ||
| 398 | * Get the wrapped element as a generalized time type. | ||
| 399 | * | ||
| 400 | * @throws \UnexpectedValueException If the element is not a generalized | ||
| 401 | * time | ||
| 402 | * @return Primitive\GeneralizedTime | ||
| 403 | */ | ||
| 404 | 2 | public function asGeneralizedTime(): Primitive\GeneralizedTime | |
| 412 | |||
| 413 | /** | ||
| 414 | * Get the wrapped element as a graphic string type. | ||
| 415 | * | ||
| 416 | * @throws \UnexpectedValueException If the element is not a graphic string | ||
| 417 | * @return Primitive\GraphicString | ||
| 418 | */ | ||
| 419 | 2 | public function asGraphicString(): Primitive\GraphicString | |
| 427 | |||
| 428 | /** | ||
| 429 | * Get the wrapped element as a visible string type. | ||
| 430 | * | ||
| 431 | * @throws \UnexpectedValueException If the element is not a visible string | ||
| 432 | * @return Primitive\VisibleString | ||
| 433 | */ | ||
| 434 | 2 | public function asVisibleString(): Primitive\VisibleString | |
| 442 | |||
| 443 | /** | ||
| 444 | * Get the wrapped element as a general string type. | ||
| 445 | * | ||
| 446 | * @throws \UnexpectedValueException If the element is not general string | ||
| 447 | * @return Primitive\GeneralString | ||
| 448 | */ | ||
| 449 | 2 | public function asGeneralString(): Primitive\GeneralString | |
| 457 | |||
| 458 | /** | ||
| 459 | * Get the wrapped element as a universal string type. | ||
| 460 | * | ||
| 461 | * @throws \UnexpectedValueException If the element is not a universal | ||
| 462 | * string | ||
| 463 | * @return Primitive\UniversalString | ||
| 464 | */ | ||
| 465 | 2 | public function asUniversalString(): Primitive\UniversalString | |
| 473 | |||
| 474 | /** | ||
| 475 | * Get the wrapped element as a character string type. | ||
| 476 | * | ||
| 477 | * @throws \UnexpectedValueException If the element is not a character | ||
| 478 | * string | ||
| 479 | * @return Primitive\CharacterString | ||
| 480 | */ | ||
| 481 | 2 | public function asCharacterString(): Primitive\CharacterString | |
| 489 | |||
| 490 | /** | ||
| 491 | * Get the wrapped element as a BMP string type. | ||
| 492 | * | ||
| 493 | * @throws \UnexpectedValueException If the element is not a bmp string | ||
| 494 | * @return Primitive\BMPString | ||
| 495 | */ | ||
| 496 | 2 | public function asBMPString(): Primitive\BMPString | |
| 504 | |||
| 505 | /** | ||
| 506 | * Get the wrapped element as any string type. | ||
| 507 | * | ||
| 508 | * @throws \UnexpectedValueException If the element is not a string | ||
| 509 | * @return StringType | ||
| 510 | */ | ||
| 511 | 2 | public function asString(): StringType | |
| 519 | |||
| 520 | /** | ||
| 521 | * Get the wrapped element as any time type. | ||
| 522 | * | ||
| 523 | * @throws \UnexpectedValueException If the element is not a time | ||
| 524 | * @return TimeType | ||
| 525 | */ | ||
| 526 | 2 | public function asTime(): TimeType | |
| 534 | |||
| 535 | /** | ||
| 536 | * Generate message for exceptions thrown by <code>as*</code> methods. | ||
| 537 | * | ||
| 538 | * @param int $tag Type tag of the expected element | ||
| 539 | * @return string | ||
| 540 | */ | ||
| 541 | 29 | private function _generateExceptionMessage(int $tag): string | |
| 546 | |||
| 547 | /** | ||
| 548 | * Get textual description of the wrapped element for debugging purposes. | ||
| 549 | * | ||
| 550 | * @return string | ||
| 551 | */ | ||
| 552 | 31 | private function _typeDescriptorString(): string | |
| 561 | |||
| 562 | /** | ||
| 563 | * | ||
| 564 | * @see \ASN1\Feature\Encodable::toDER() | ||
| 565 | * @return string | ||
| 566 | */ | ||
| 567 | 1 | public function toDER(): string | |
| 571 | |||
| 572 | /** | ||
| 573 | * | ||
| 574 | * @see \ASN1\Feature\ElementBase::typeClass() | ||
| 575 | * @return int | ||
| 576 | */ | ||
| 577 | 2 | public function typeClass(): int | |
| 581 | |||
| 582 | /** | ||
| 583 | * | ||
| 584 | * @see \ASN1\Feature\ElementBase::isConstructed() | ||
| 585 | * @return bool | ||
| 586 | */ | ||
| 587 | 3 | public function isConstructed(): bool | |
| 591 | |||
| 592 | /** | ||
| 593 | * | ||
| 594 | * @see \ASN1\Feature\ElementBase::tag() | ||
| 595 | * @return int | ||
| 596 | */ | ||
| 597 | 9 | public function tag(): int | |
| 601 | |||
| 602 | /** | ||
| 603 | * | ||
| 604 |      * {@inheritdoc} | ||
| 605 | * @see \ASN1\Feature\ElementBase::isType() | ||
| 606 | * @return bool | ||
| 607 | */ | ||
| 608 | 1 | public function isType(int $tag): bool | |
| 612 | |||
| 613 | /** | ||
| 614 | * | ||
| 615 | * @deprecated Use any <code>as*</code> accessor method first to ensure | ||
| 616 | * type strictness. | ||
| 617 | * @see \ASN1\Feature\ElementBase::expectType() | ||
| 618 | * @return ElementBase | ||
| 619 | */ | ||
| 620 | 1 | public function expectType(int $tag): ElementBase | |
| 624 | |||
| 625 | /** | ||
| 626 | * | ||
| 627 | * @see \ASN1\Feature\ElementBase::isTagged() | ||
| 628 | * @return bool | ||
| 629 | */ | ||
| 630 | 1 | public function isTagged(): bool | |
| 634 | |||
| 635 | /** | ||
| 636 | * | ||
| 637 | * @deprecated Use any <code>as*</code> accessor method first to ensure | ||
| 638 | * type strictness. | ||
| 639 | * @see \ASN1\Feature\ElementBase::expectTagged() | ||
| 640 | * @return TaggedType | ||
| 641 | */ | ||
| 642 | 1 | public function expectTagged($tag = null): TaggedType | |
| 646 | |||
| 647 | /** | ||
| 648 | * | ||
| 649 | * @see \ASN1\Feature\ElementBase::asElement() | ||
| 650 | * @return Element | ||
| 651 | */ | ||
| 652 | 1 | public function asElement(): Element | |
| 656 | |||
| 657 | /** | ||
| 658 | * | ||
| 659 |      * {@inheritdoc} | ||
| 660 | * @return UnspecifiedType | ||
| 661 | */ | ||
| 662 | 1 | public function asUnspecified(): UnspecifiedType | |
| 666 | } | ||
| 667 |