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:
| 1 | <?php |
||
| 8 | class SslChain |
||
| 9 | { |
||
| 10 | /** @var string */ |
||
| 11 | protected $name; |
||
| 12 | |||
| 13 | /** @var array */ |
||
| 14 | protected $subject; |
||
| 15 | |||
| 16 | /** @var string */ |
||
| 17 | protected $hash; |
||
| 18 | |||
| 19 | /** @var array */ |
||
| 20 | protected $issuer; |
||
| 21 | |||
| 22 | /** @var string */ |
||
| 23 | protected $version; |
||
| 24 | |||
| 25 | /** @var BigInteger */ |
||
| 26 | protected $serial; |
||
| 27 | |||
| 28 | /** @var Carbon */ |
||
| 29 | protected $validFrom; |
||
| 30 | |||
| 31 | /** @var Carbon */ |
||
| 32 | protected $validTo; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected $signatureType; |
||
| 36 | |||
| 37 | 41 | private static function setValidFromDate($utcInput): Carbon |
|
| 38 | { |
||
| 39 | 41 | return Carbon::createFromTimestampUTC($utcInput); |
|
| 40 | } |
||
| 41 | |||
| 42 | 41 | private static function setValidToDate($utcInput): Carbon |
|
| 43 | { |
||
| 44 | 41 | return Carbon::createFromTimestampUTC($utcInput); |
|
| 45 | } |
||
| 46 | |||
| 47 | 41 | public function __construct(array $chainInput) |
|
| 48 | { |
||
| 49 | 41 | $this->name = $chainInput['name']; |
|
| 50 | 41 | $this->subject = $chainInput['subject']; |
|
| 51 | 41 | $this->hash = $chainInput['hash']; |
|
| 52 | 41 | $this->issuer = $chainInput['issuer']; |
|
| 53 | 41 | $this->version = $chainInput['version']; |
|
| 54 | 41 | $this->serial = new BigInteger($chainInput['serialNumber']); |
|
| 55 | 41 | $this->validFrom = self::setValidFromDate($chainInput['validFrom_time_t']); |
|
| 56 | 41 | $this->validTo = self::setValidToDate($chainInput['validTo_time_t']); |
|
| 57 | 41 | $this->signatureType = $chainInput['signatureTypeSN']; |
|
| 58 | 41 | } |
|
| 59 | |||
| 60 | 1 | public function getLocationName(): string |
|
| 61 | { |
||
| 62 | 1 | return $this->subject['C'] ?? ''; |
|
| 63 | } |
||
| 64 | |||
| 65 | 1 | public function getOrganizationName(): string |
|
| 66 | { |
||
| 67 | 1 | return $this->subject['O'] ?? ''; |
|
| 68 | } |
||
| 69 | |||
| 70 | 1 | public function getOrganizationUnitName(): string |
|
| 71 | { |
||
| 72 | 1 | return $this->subject['OU'] ?? ''; |
|
| 73 | } |
||
| 74 | |||
| 75 | 1 | public function getCommonName(): string |
|
| 76 | { |
||
| 77 | 1 | return $this->subject['CN'] ?? ''; |
|
| 78 | } |
||
| 79 | |||
| 80 | 1 | public function getHash(): string |
|
| 81 | { |
||
| 82 | 1 | return $this->hash; |
|
| 83 | } |
||
| 84 | |||
| 85 | 1 | public function getIssuerLocationName(): string |
|
| 86 | { |
||
| 87 | 1 | return $this->issuer['C'] ?? ''; |
|
| 88 | } |
||
| 89 | |||
| 90 | 1 | public function getIssuerOrganizationName(): string |
|
| 91 | { |
||
| 92 | 1 | return $this->issuer['O'] ?? ''; |
|
| 93 | } |
||
| 94 | |||
| 95 | 2 | public function getIssuerOrganizationUnitName(): string |
|
| 96 | { |
||
| 97 | 2 | if (isset($this->issuer['OU']) === true) { |
|
| 98 | 2 | if (is_array($this->issuer['OU']) === true) { |
|
| 99 | 1 | return $this->issuer['OU'][0] ?? ''; |
|
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | 1 | return $this->issuer['OU'] ?? ''; |
|
| 104 | } |
||
| 105 | |||
| 106 | 1 | public function getIssuerCommonName(): string |
|
| 110 | |||
| 111 | 1 | public function getSerialNumber(): string |
|
| 115 | |||
| 116 | 2 | public function validFromDate(): Carbon |
|
| 120 | |||
| 121 | 4 | public function expirationDate(): Carbon |
|
| 125 | |||
| 126 | 1 | public function getSignatureAlgorithm(): string |
|
| 130 | |||
| 131 | 1 | public function isExpired(): bool |
|
| 135 | |||
| 136 | 1 | View Code Duplication | public function isValid() |
| 137 | { |
||
| 138 | 1 | if (Carbon::now()->between($this->validFromDate(), $this->expirationDate()) === false) { |
|
| 139 | return false; |
||
| 140 | } |
||
| 141 | |||
| 142 | 1 | return true; |
|
| 143 | } |
||
| 144 | |||
| 145 | 1 | public function isValidUntil(Carbon $carbon): bool |
|
| 153 | } |
||
| 154 |