Complex classes like TBSCertificate 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 TBSCertificate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class TBSCertificate |
||
| 29 | { |
||
| 30 | // Certificate version enumerations |
||
| 31 | const VERSION_1 = 0; |
||
| 32 | const VERSION_2 = 1; |
||
| 33 | const VERSION_3 = 2; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Certificate version. |
||
| 37 | * |
||
| 38 | * @var int|null |
||
| 39 | */ |
||
| 40 | protected $_version; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Serial number. |
||
| 44 | * |
||
| 45 | * @var string|null |
||
| 46 | */ |
||
| 47 | protected $_serialNumber; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Signature algorithm. |
||
| 51 | * |
||
| 52 | * @var SignatureAlgorithmIdentifier|null |
||
| 53 | */ |
||
| 54 | protected $_signature; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Certificate issuer. |
||
| 58 | * |
||
| 59 | * @var Name $_issuer |
||
| 60 | */ |
||
| 61 | protected $_issuer; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Certificate validity period. |
||
| 65 | * |
||
| 66 | * @var Validity $_validity |
||
| 67 | */ |
||
| 68 | protected $_validity; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Certificate subject. |
||
| 72 | * |
||
| 73 | * @var Name $_subject |
||
| 74 | */ |
||
| 75 | protected $_subject; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Subject public key. |
||
| 79 | * |
||
| 80 | * @var PublicKeyInfo $_subjectPublicKeyInfo |
||
| 81 | */ |
||
| 82 | protected $_subjectPublicKeyInfo; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Issuer unique identifier. |
||
| 86 | * |
||
| 87 | * @var UniqueIdentifier|null $_issuerUniqueID |
||
| 88 | */ |
||
| 89 | protected $_issuerUniqueID; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Subject unique identifier. |
||
| 93 | * |
||
| 94 | * @var UniqueIdentifier|null $_subjectUniqueID |
||
| 95 | */ |
||
| 96 | protected $_subjectUniqueID; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Extensions. |
||
| 100 | * |
||
| 101 | * @var Extensions $_extensions |
||
| 102 | */ |
||
| 103 | protected $_extensions; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Constructor. |
||
| 107 | * |
||
| 108 | * @param Name $subject Certificate subject |
||
| 109 | * @param PublicKeyInfo $pki Subject public key |
||
| 110 | * @param Name $issuer Certificate issuer |
||
| 111 | * @param Validity $validity Validity period |
||
| 112 | */ |
||
| 113 | 29 | public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Initialize from ASN.1. |
||
| 125 | * |
||
| 126 | * @param Sequence $seq |
||
| 127 | * @return self |
||
| 128 | */ |
||
| 129 | 21 | public static function fromASN1(Sequence $seq): self |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Initialize from certification request. |
||
| 180 | * |
||
| 181 | * Note that signature is not verified and must be done by the caller. |
||
| 182 | * |
||
| 183 | * @param CertificationRequest $cr |
||
| 184 | * @return self |
||
| 185 | */ |
||
| 186 | 1 | public static function fromCSR(CertificationRequest $cr): self |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Get self with fields set from the issuer's certificate. |
||
| 207 | * |
||
| 208 | * Issuer shall be set to issuing certificate's subject. |
||
| 209 | * Authority key identifier extensions shall be added with a key identifier |
||
| 210 | * set to issuing certificate's public key identifier. |
||
| 211 | * |
||
| 212 | * @param Certificate $cert Issuing party's certificate |
||
| 213 | * @return self |
||
| 214 | */ |
||
| 215 | 1 | public function withIssuerCertificate(Certificate $cert): self |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Get self with given version. |
||
| 231 | * |
||
| 232 | * If version is not set, appropriate version is automatically |
||
| 233 | * determined during signing. |
||
| 234 | * |
||
| 235 | * @param int $version |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | 4 | public function withVersion(int $version): self |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get self with given serial number. |
||
| 247 | * |
||
| 248 | * @param int|string $serial Base 10 number |
||
| 249 | * @return self |
||
| 250 | */ |
||
| 251 | 5 | public function withSerialNumber($serial): self |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Get self with random positive serial number. |
||
| 260 | * |
||
| 261 | * @param int $size Number of random bytes |
||
| 262 | * @return self |
||
| 263 | */ |
||
| 264 | 1 | public function withRandomSerialNumber(int $size = 16): self |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Get self with given signature algorithm. |
||
| 277 | * |
||
| 278 | * @param SignatureAlgorithmIdentifier $algo |
||
| 279 | * @return self |
||
| 280 | */ |
||
| 281 | 4 | public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Get self with given issuer. |
||
| 290 | * |
||
| 291 | * @param Name $issuer |
||
| 292 | * @return self |
||
| 293 | */ |
||
| 294 | 1 | public function withIssuer(Name $issuer): self |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Get self with given validity. |
||
| 303 | * |
||
| 304 | * @param Validity $validity |
||
| 305 | * @return self |
||
| 306 | */ |
||
| 307 | 2 | public function withValidity(Validity $validity): self |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Get self with given subject. |
||
| 316 | * |
||
| 317 | * @param Name $subject |
||
| 318 | * @return self |
||
| 319 | */ |
||
| 320 | 2 | public function withSubject(Name $subject): self |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Get self with given subject public key info. |
||
| 329 | * |
||
| 330 | * @param PublicKeyInfo $pub_key_info |
||
| 331 | * @return self |
||
| 332 | */ |
||
| 333 | 1 | public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Get self with issuer unique ID. |
||
| 342 | * |
||
| 343 | * @param UniqueIdentifier $id |
||
| 344 | * @return self |
||
| 345 | */ |
||
| 346 | 6 | public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get self with subject unique ID. |
||
| 355 | * |
||
| 356 | * @param UniqueIdentifier $id |
||
| 357 | * @return self |
||
| 358 | */ |
||
| 359 | 4 | public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get self with given extensions. |
||
| 368 | * |
||
| 369 | * @param Extensions $extensions |
||
| 370 | * @return self |
||
| 371 | */ |
||
| 372 | 4 | public function withExtensions(Extensions $extensions): self |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Get self with extensions added. |
||
| 381 | * |
||
| 382 | * @param Extension ...$exts One or more Extension objects |
||
| 383 | * @return self |
||
| 384 | */ |
||
| 385 | 3 | public function withAdditionalExtensions(Extension ...$exts): self |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Check whether version is set. |
||
| 394 | * |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | 68 | public function hasVersion(): bool |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Get certificate version. |
||
| 404 | * |
||
| 405 | * @return int |
||
| 406 | */ |
||
| 407 | 68 | public function version(): int |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Check whether serial number is set. |
||
| 417 | * |
||
| 418 | * @return bool |
||
| 419 | */ |
||
| 420 | 91 | public function hasSerialNumber(): bool |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Get serial number. |
||
| 427 | * |
||
| 428 | * @return string Base 10 integer |
||
| 429 | */ |
||
| 430 | 91 | public function serialNumber(): string |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Check whether signature algorithm is set. |
||
| 440 | * |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | 68 | public function hasSignature(): bool |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Get signature algorithm. |
||
| 450 | * |
||
| 451 | * @return SignatureAlgorithmIdentifier |
||
| 452 | */ |
||
| 453 | 68 | public function signature(): SignatureAlgorithmIdentifier |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Get issuer. |
||
| 463 | * |
||
| 464 | * @return Name |
||
| 465 | */ |
||
| 466 | 68 | public function issuer(): Name |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get validity period. |
||
| 473 | * |
||
| 474 | * @return Validity |
||
| 475 | */ |
||
| 476 | 46 | public function validity(): Validity |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Get subject. |
||
| 483 | * |
||
| 484 | * @return Name |
||
| 485 | */ |
||
| 486 | 68 | public function subject(): Name |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Get subject public key. |
||
| 493 | * |
||
| 494 | * @return PublicKeyInfo |
||
| 495 | */ |
||
| 496 | 68 | public function subjectPublicKeyInfo(): PublicKeyInfo |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Whether issuer unique identifier is present. |
||
| 503 | * |
||
| 504 | * @return bool |
||
| 505 | */ |
||
| 506 | 7 | public function hasIssuerUniqueID(): bool |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Get issuerUniqueID. |
||
| 513 | * |
||
| 514 | * @return UniqueIdentifier |
||
| 515 | */ |
||
| 516 | 4 | public function issuerUniqueID(): UniqueIdentifier |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Whether subject unique identifier is present. |
||
| 526 | * |
||
| 527 | * @return bool |
||
| 528 | */ |
||
| 529 | 2 | public function hasSubjectUniqueID(): bool |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Get subjectUniqueID. |
||
| 536 | * |
||
| 537 | * @return UniqueIdentifier |
||
| 538 | */ |
||
| 539 | 2 | public function subjectUniqueID(): UniqueIdentifier |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Get extensions. |
||
| 549 | * |
||
| 550 | * @return Extensions |
||
| 551 | */ |
||
| 552 | 62 | public function extensions(): Extensions |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Generate ASN.1 structure. |
||
| 559 | * |
||
| 560 | * @return Sequence |
||
| 561 | */ |
||
| 562 | 65 | public function toASN1(): Sequence |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Create signed certificate. |
||
| 593 | * |
||
| 594 | * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
||
| 595 | * @param PrivateKeyInfo $privkey_info Private key used for signing |
||
| 596 | * @param Crypto|null $crypto Crypto engine, use default if not set |
||
| 597 | * @return Certificate |
||
| 598 | */ |
||
| 599 | 12 | public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Determine minimum version for the certificate. |
||
| 618 | * |
||
| 619 | * @return int |
||
| 620 | */ |
||
| 621 | 12 | protected function _determineVersion(): int |
|
| 633 | } |
||
| 634 |