Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 39 | */ |
||
| 40 | protected $_version; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Serial number. |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $_serialNumber; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Signature algorithm. |
||
| 51 | * |
||
| 52 | * @var AlgorithmIdentifierType |
||
| 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 | 18 | public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Initialize from ASN.1. |
||
| 124 | * |
||
| 125 | * @param Sequence $seq |
||
| 126 | * @return self |
||
| 127 | */ |
||
| 128 | 12 | public static function fromASN1(Sequence $seq) { |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Initialize from certification request. |
||
| 167 | * |
||
| 168 | * Note that signature is not verified and must be done by the caller. |
||
| 169 | * |
||
| 170 | * @param CertificationRequest $cr |
||
| 171 | * @return self |
||
| 172 | */ |
||
| 173 | 1 | public static function fromCSR(CertificationRequest $cr) { |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Get self with fields set from the issuer's certificate. |
||
| 196 | * |
||
| 197 | * Issuer shall be set to issuing certificate's subject. |
||
| 198 | * Authority key identifier extensions shall be added with a key identifier |
||
| 199 | * set to issuing certificate's public key identifier. |
||
| 200 | * |
||
| 201 | * @param Certificate $cert Issuing party's certificate |
||
| 202 | * @return self |
||
| 203 | */ |
||
| 204 | 1 | public function withIssuerCertificate(Certificate $cert) { |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Get self with given version. |
||
| 219 | * |
||
| 220 | * If version is not set, appropriate version is automatically |
||
| 221 | * determined during signing. |
||
| 222 | * |
||
| 223 | * @param int $version |
||
| 224 | * @return self |
||
| 225 | */ |
||
| 226 | 4 | public function withVersion($version) { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Get self with given serial number. |
||
| 234 | * |
||
| 235 | * @param int|string $serial Base 10 number |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | 5 | public function withSerialNumber($serial) { |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Get self with random positive serial number. |
||
| 246 | * |
||
| 247 | * @param int $size Number of random bytes |
||
| 248 | * @return self |
||
| 249 | */ |
||
| 250 | 1 | View Code Duplication | public function withRandomSerialNumber($size = 16) { |
| 259 | |||
| 260 | /** |
||
| 261 | * Get self with given signature algorithm. |
||
| 262 | * |
||
| 263 | * @param SignatureAlgorithmIdentifier $algo |
||
| 264 | * @return self |
||
| 265 | */ |
||
| 266 | 4 | public function withSignature(SignatureAlgorithmIdentifier $algo) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Get self with given issuer. |
||
| 274 | * |
||
| 275 | * @param Name $issuer |
||
| 276 | * @return self |
||
| 277 | */ |
||
| 278 | 1 | public function withIssuer(Name $issuer) { |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Get self with given validity. |
||
| 286 | * |
||
| 287 | * @param Validity $validity |
||
| 288 | * @return self |
||
| 289 | */ |
||
| 290 | 2 | public function withValidity(Validity $validity) { |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get self with given subject. |
||
| 298 | * |
||
| 299 | * @param Name $subject |
||
| 300 | * @return self |
||
| 301 | */ |
||
| 302 | 1 | public function withSubject(Name $subject) { |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Get self with given subject public key info. |
||
| 310 | * |
||
| 311 | * @param PublicKeyInfo $pub_key_info |
||
| 312 | * @return self |
||
| 313 | */ |
||
| 314 | 1 | public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info) { |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Get self with issuer unique ID. |
||
| 322 | * |
||
| 323 | * @param UniqueIdentifier $id |
||
| 324 | * @return self |
||
| 325 | */ |
||
| 326 | 4 | public function withIssuerUniqueID(UniqueIdentifier $id) { |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Get self with subject unique ID. |
||
| 334 | * |
||
| 335 | * @param UniqueIdentifier $id |
||
| 336 | * @return self |
||
| 337 | */ |
||
| 338 | 4 | public function withSubjectUniqueID(UniqueIdentifier $id) { |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Get self with given extensions. |
||
| 346 | * |
||
| 347 | * @param Extensions $extensions |
||
| 348 | * @return self |
||
| 349 | */ |
||
| 350 | 4 | public function withExtensions(Extensions $extensions) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Get self with extensions added. |
||
| 358 | * |
||
| 359 | * @param Extension ...$exts One or more Extension objects |
||
| 360 | * @return self |
||
| 361 | */ |
||
| 362 | 3 | public function withAdditionalExtensions(Extension ...$exts) { |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Check whether version is set. |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | 41 | public function hasVersion() { |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Get certificate version. |
||
| 379 | * |
||
| 380 | * @return int |
||
| 381 | */ |
||
| 382 | 41 | public function version() { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Check whether serial number is set. |
||
| 391 | * |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | 44 | public function hasSerialNumber() { |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Get serial number. |
||
| 400 | * |
||
| 401 | * @return int|string Base 10 integer |
||
| 402 | */ |
||
| 403 | 44 | public function serialNumber() { |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Check whether signature algorithm is set. |
||
| 412 | * |
||
| 413 | * @return bool |
||
| 414 | */ |
||
| 415 | 41 | public function hasSignature() { |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Get signature algorithm. |
||
| 421 | * |
||
| 422 | * @return AlgorithmIdentifierType |
||
| 423 | */ |
||
| 424 | 41 | public function signature() { |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Get issuer. |
||
| 433 | * |
||
| 434 | * @return Name |
||
| 435 | */ |
||
| 436 | 37 | public function issuer() { |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Get validity period. |
||
| 442 | * |
||
| 443 | * @return Validity |
||
| 444 | */ |
||
| 445 | 25 | public function validity() { |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Get subject. |
||
| 451 | * |
||
| 452 | * @return Name |
||
| 453 | */ |
||
| 454 | 36 | public function subject() { |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Get subject public key. |
||
| 460 | * |
||
| 461 | * @return PublicKeyInfo |
||
| 462 | */ |
||
| 463 | 31 | public function subjectPublicKeyInfo() { |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Whether issuer unique identifier is present. |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | 3 | public function hasIssuerUniqueID() { |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Get issuerUniqueID. |
||
| 478 | * |
||
| 479 | * @return UniqueIdentifier |
||
| 480 | */ |
||
| 481 | 2 | public function issuerUniqueID() { |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Whether subject unique identifier is present. |
||
| 490 | * |
||
| 491 | * @return bool |
||
| 492 | */ |
||
| 493 | 2 | public function hasSubjectUniqueID() { |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Get subjectUniqueID. |
||
| 499 | * |
||
| 500 | * @return UniqueIdentifier |
||
| 501 | */ |
||
| 502 | 2 | public function subjectUniqueID() { |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Get extensions. |
||
| 511 | * |
||
| 512 | * @return Extensions |
||
| 513 | */ |
||
| 514 | 35 | public function extensions() { |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Generate ASN.1 structure. |
||
| 520 | * |
||
| 521 | * @return Sequence |
||
| 522 | */ |
||
| 523 | 38 | public function toASN1() { |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Create signed certificate. |
||
| 553 | * |
||
| 554 | * @param Crypto $crypto Crypto engine |
||
| 555 | * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
||
| 556 | * @param PrivateKeyInfo $privkey_info Private key used for signing |
||
| 557 | * @return Certificate |
||
| 558 | */ |
||
| 559 | 9 | public function sign(Crypto $crypto, SignatureAlgorithmIdentifier $algo, |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Determine minimum version for the certificate. |
||
| 576 | * |
||
| 577 | * @return int |
||
| 578 | */ |
||
| 579 | 9 | protected function _determineVersion() { |
|
| 590 | } |
||
| 591 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: