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 | 92 | 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 | 1 | 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 a boolean type. |
||
| 93 | * |
||
| 94 | * @throws \UnexpectedValueException If the element is not a boolean |
||
| 95 | * @return Primitive\Boolean |
||
| 96 | */ |
||
| 97 | 4 | public function asBoolean(): Primitive\Boolean |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Get the wrapped element as an integer type. |
||
| 108 | * |
||
| 109 | * @throws \UnexpectedValueException If the element is not an integer |
||
| 110 | * @return Primitive\Integer |
||
| 111 | */ |
||
| 112 | 2 | public function asInteger(): Primitive\Integer |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get the wrapped element as a bit string type. |
||
| 123 | * |
||
| 124 | * @throws \UnexpectedValueException If the element is not a bit string |
||
| 125 | * @return Primitive\BitString |
||
| 126 | */ |
||
| 127 | 2 | public function asBitString(): Primitive\BitString |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get the wrapped element as an octet string type. |
||
| 138 | * |
||
| 139 | * @throws \UnexpectedValueException If the element is not an octet string |
||
| 140 | * @return Primitive\OctetString |
||
| 141 | */ |
||
| 142 | 2 | public function asOctetString(): Primitive\OctetString |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Get the wrapped element as a null type. |
||
| 153 | * |
||
| 154 | * @throws \UnexpectedValueException If the element is not a null |
||
| 155 | * @return Primitive\NullType |
||
| 156 | */ |
||
| 157 | 10 | public function asNull(): Primitive\NullType |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Get the wrapped element as an object identifier type. |
||
| 168 | * |
||
| 169 | * @throws \UnexpectedValueException If the element is not an object |
||
| 170 | * identifier |
||
| 171 | * @return Primitive\ObjectIdentifier |
||
| 172 | */ |
||
| 173 | 2 | public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Get the wrapped element as an object descriptor type. |
||
| 185 | * |
||
| 186 | * @throws \UnexpectedValueException If the element is not an object |
||
| 187 | * descriptor |
||
| 188 | * @return Primitive\ObjectDescriptor |
||
| 189 | */ |
||
| 190 | 2 | public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Get the wrapped element as a real type. |
||
| 202 | * |
||
| 203 | * @throws \UnexpectedValueException If the element is not a real |
||
| 204 | * @return Primitive\Real |
||
| 205 | */ |
||
| 206 | 2 | public function asReal(): Primitive\Real |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Get the wrapped element as an enumerated type. |
||
| 217 | * |
||
| 218 | * @throws \UnexpectedValueException If the element is not an enumerated |
||
| 219 | * @return Primitive\Enumerated |
||
| 220 | */ |
||
| 221 | 2 | public function asEnumerated(): Primitive\Enumerated |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Get the wrapped element as a UTF8 string type. |
||
| 232 | * |
||
| 233 | * @throws \UnexpectedValueException If the element is not a UTF8 string |
||
| 234 | * @return Primitive\UTF8String |
||
| 235 | */ |
||
| 236 | 2 | public function asUTF8String(): Primitive\UTF8String |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get the wrapped element as a relative OID type. |
||
| 247 | * |
||
| 248 | * @throws \UnexpectedValueException If the element is not a relative OID |
||
| 249 | * @return Primitive\RelativeOID |
||
| 250 | */ |
||
| 251 | 2 | public function asRelativeOID(): Primitive\RelativeOID |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Get the wrapped element as a sequence type. |
||
| 262 | * |
||
| 263 | * @throws \UnexpectedValueException If the element is not a sequence |
||
| 264 | * @return Constructed\Sequence |
||
| 265 | */ |
||
| 266 | 2 | public function asSequence(): Constructed\Sequence |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Get the wrapped element as a set type. |
||
| 277 | * |
||
| 278 | * @throws \UnexpectedValueException If the element is not a set |
||
| 279 | * @return Constructed\Set |
||
| 280 | */ |
||
| 281 | 2 | public function asSet(): Constructed\Set |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Get the wrapped element as a numeric string type. |
||
| 292 | * |
||
| 293 | * @throws \UnexpectedValueException If the element is not a numeric string |
||
| 294 | * @return Primitive\NumericString |
||
| 295 | */ |
||
| 296 | 2 | public function asNumericString(): Primitive\NumericString |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Get the wrapped element as a printable string type. |
||
| 307 | * |
||
| 308 | * @throws \UnexpectedValueException If the element is not a printable |
||
| 309 | * string |
||
| 310 | * @return Primitive\PrintableString |
||
| 311 | */ |
||
| 312 | 2 | public function asPrintableString(): Primitive\PrintableString |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get the wrapped element as a T61 string type. |
||
| 323 | * |
||
| 324 | * @throws \UnexpectedValueException If the element is not a T61 string |
||
| 325 | * @return Primitive\T61String |
||
| 326 | */ |
||
| 327 | 2 | public function asT61String(): Primitive\T61String |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Get the wrapped element as a videotex string type. |
||
| 338 | * |
||
| 339 | * @throws \UnexpectedValueException If the element is not a videotex string |
||
| 340 | * @return Primitive\VideotexString |
||
| 341 | */ |
||
| 342 | 2 | public function asVideotexString(): Primitive\VideotexString |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get the wrapped element as a IA5 string type. |
||
| 353 | * |
||
| 354 | * @throws \UnexpectedValueException If the element is not a IA5 string |
||
| 355 | * @return Primitive\IA5String |
||
| 356 | */ |
||
| 357 | 2 | public function asIA5String(): Primitive\IA5String |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get the wrapped element as an UTC time type. |
||
| 368 | * |
||
| 369 | * @throws \UnexpectedValueException If the element is not a UTC time |
||
| 370 | * @return Primitive\UTCTime |
||
| 371 | */ |
||
| 372 | 2 | public function asUTCTime(): Primitive\UTCTime |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Get the wrapped element as a generalized time type. |
||
| 383 | * |
||
| 384 | * @throws \UnexpectedValueException If the element is not a generalized |
||
| 385 | * time |
||
| 386 | * @return Primitive\GeneralizedTime |
||
| 387 | */ |
||
| 388 | 2 | public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Get the wrapped element as a graphic string type. |
||
| 399 | * |
||
| 400 | * @throws \UnexpectedValueException If the element is not a graphic string |
||
| 401 | * @return Primitive\GraphicString |
||
| 402 | */ |
||
| 403 | 2 | public function asGraphicString(): Primitive\GraphicString |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Get the wrapped element as a visible string type. |
||
| 414 | * |
||
| 415 | * @throws \UnexpectedValueException If the element is not a visible string |
||
| 416 | * @return Primitive\VisibleString |
||
| 417 | */ |
||
| 418 | 2 | public function asVisibleString(): Primitive\VisibleString |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Get the wrapped element as a general string type. |
||
| 429 | * |
||
| 430 | * @throws \UnexpectedValueException If the element is not general string |
||
| 431 | * @return Primitive\GeneralString |
||
| 432 | */ |
||
| 433 | 2 | public function asGeneralString(): Primitive\GeneralString |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Get the wrapped element as a universal string type. |
||
| 444 | * |
||
| 445 | * @throws \UnexpectedValueException If the element is not a universal |
||
| 446 | * string |
||
| 447 | * @return Primitive\UniversalString |
||
| 448 | */ |
||
| 449 | 2 | public function asUniversalString(): Primitive\UniversalString |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Get the wrapped element as a character string type. |
||
| 460 | * |
||
| 461 | * @throws \UnexpectedValueException If the element is not a character |
||
| 462 | * string |
||
| 463 | * @return Primitive\CharacterString |
||
| 464 | */ |
||
| 465 | 2 | public function asCharacterString(): Primitive\CharacterString |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Get the wrapped element as a BMP string type. |
||
| 476 | * |
||
| 477 | * @throws \UnexpectedValueException If the element is not a bmp string |
||
| 478 | * @return Primitive\BMPString |
||
| 479 | */ |
||
| 480 | 2 | public function asBMPString(): Primitive\BMPString |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Get the wrapped element as any string type. |
||
| 491 | * |
||
| 492 | * @throws \UnexpectedValueException If the element is not a string |
||
| 493 | * @return StringType |
||
| 494 | */ |
||
| 495 | 2 | public function asString(): StringType |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Get the wrapped element as any time type. |
||
| 506 | * |
||
| 507 | * @throws \UnexpectedValueException If the element is not a time |
||
| 508 | * @return TimeType |
||
| 509 | */ |
||
| 510 | 2 | public function asTime(): TimeType |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Generate message for exceptions thrown by <code>as*</code> methods. |
||
| 521 | * |
||
| 522 | * @param int $tag Type tag of the expected element |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | 29 | private function _generateExceptionMessage(int $tag): string |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Get textual description of the wrapped element for debugging purposes. |
||
| 533 | * |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | 30 | private function _typeDescriptorString(): string |
|
| 545 | |||
| 546 | /** |
||
| 547 | * |
||
| 548 | * @see \ASN1\Feature\Encodable::toDER() |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | 1 | public function toDER(): string |
|
| 555 | |||
| 556 | /** |
||
| 557 | * |
||
| 558 | * @see \ASN1\Feature\ElementBase::typeClass() |
||
| 559 | * @return int |
||
| 560 | */ |
||
| 561 | 2 | public function typeClass(): int |
|
| 565 | |||
| 566 | /** |
||
| 567 | * |
||
| 568 | * @see \ASN1\Feature\ElementBase::isConstructed() |
||
| 569 | * @return bool |
||
| 570 | */ |
||
| 571 | 3 | public function isConstructed(): bool |
|
| 575 | |||
| 576 | /** |
||
| 577 | * |
||
| 578 | * @see \ASN1\Feature\ElementBase::tag() |
||
| 579 | * @return int |
||
| 580 | */ |
||
| 581 | 9 | public function tag(): int |
|
| 585 | |||
| 586 | /** |
||
| 587 | * |
||
| 588 | * {@inheritdoc} |
||
| 589 | * @see \ASN1\Feature\ElementBase::isType() |
||
| 590 | * @return bool |
||
| 591 | */ |
||
| 592 | 1 | public function isType(int $tag): bool |
|
| 596 | |||
| 597 | /** |
||
| 598 | * |
||
| 599 | * @deprecated Use any <code>as*</code> accessor method first to ensure |
||
| 600 | * type strictness. |
||
| 601 | * @see \ASN1\Feature\ElementBase::expectType() |
||
| 602 | * @return ElementBase |
||
| 603 | */ |
||
| 604 | 1 | public function expectType(int $tag): ElementBase |
|
| 608 | |||
| 609 | /** |
||
| 610 | * |
||
| 611 | * @see \ASN1\Feature\ElementBase::isTagged() |
||
| 612 | * @return bool |
||
| 613 | */ |
||
| 614 | 1 | public function isTagged(): bool |
|
| 618 | |||
| 619 | /** |
||
| 620 | * |
||
| 621 | * @deprecated Use any <code>as*</code> accessor method first to ensure |
||
| 622 | * type strictness. |
||
| 623 | * @see \ASN1\Feature\ElementBase::expectTagged() |
||
| 624 | * @return TaggedType |
||
| 625 | */ |
||
| 626 | 1 | public function expectTagged($tag = null): TaggedType |
|
| 630 | |||
| 631 | /** |
||
| 632 | * |
||
| 633 | * @see \ASN1\Feature\ElementBase::asElement() |
||
| 634 | * @return Element |
||
| 635 | */ |
||
| 636 | 1 | public function asElement(): Element |
|
| 640 | |||
| 641 | /** |
||
| 642 | * |
||
| 643 | * {@inheritdoc} |
||
| 644 | * @return UnspecifiedType |
||
| 645 | */ |
||
| 646 | 1 | public function asUnspecified(): UnspecifiedType |
|
| 650 | } |
||
| 651 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.