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 |
||
| 15 | class CertificatePoliciesExtension extends Extension implements |
||
| 16 | \Countable, |
||
|
|
|||
| 17 | \IteratorAggregate |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Policy information terms. |
||
| 21 | * |
||
| 22 | * @var PolicyInformation[] $_policies |
||
| 23 | */ |
||
| 24 | protected $_policies; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Constructor |
||
| 28 | * |
||
| 29 | * @param bool $critical |
||
| 30 | * @param PolicyInformation ...$policies |
||
| 31 | */ |
||
| 32 | 13 | public function __construct($critical, PolicyInformation ...$policies) { |
|
| 33 | 13 | parent::__construct(Extension::OID_CERTIFICATE_POLICIES, $critical); |
|
| 34 | 13 | $this->_policies = array(); |
|
| 35 | 13 | foreach ($policies as $policy) { |
|
| 36 | 12 | $this->_policies[$policy->oid()] = $policy; |
|
| 37 | 13 | } |
|
| 38 | 13 | } |
|
| 39 | |||
| 40 | 10 | View Code Duplication | protected static function _fromDER($data, $critical) { |
| 41 | 10 | $policies = array_map( |
|
| 42 | function (UnspecifiedType $el) { |
||
| 43 | 9 | return PolicyInformation::fromASN1($el->asSequence()); |
|
| 44 | 10 | }, Sequence::fromDER($data)->elements()); |
|
| 45 | 10 | if (!count($policies)) { |
|
| 46 | 1 | throw new \UnexpectedValueException( |
|
| 47 | "certificatePolicies must contain" . |
||
| 48 | 1 | " at least one PolicyInformation."); |
|
| 49 | } |
||
| 50 | 9 | return new self($critical, ...$policies); |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Check whether policy information by OID is present. |
||
| 55 | * |
||
| 56 | * @param string $oid |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | 6 | public function has($oid) { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Get policy information by OID. |
||
| 65 | * |
||
| 66 | * @param string $oid |
||
| 67 | * @throws \LogicException |
||
| 68 | * @return PolicyInformation |
||
| 69 | */ |
||
| 70 | 4 | public function get($oid) { |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Check whether anyPolicy is present. |
||
| 79 | * |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | 3 | public function hasAnyPolicy() { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get anyPolicy information. |
||
| 88 | * |
||
| 89 | * @throws \LogicException If anyPolicy is not present. |
||
| 90 | * @return PolicyInformation |
||
| 91 | */ |
||
| 92 | 2 | public function anyPolicy() { |
|
| 98 | |||
| 99 | 30 | View Code Duplication | protected function _valueASN1() { |
| 109 | |||
| 110 | /** |
||
| 111 | * Get the number of policies. |
||
| 112 | * |
||
| 113 | * @see Countable::count() |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | 1 | public function count() { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Get iterator for policy information terms. |
||
| 122 | * |
||
| 123 | * @see IteratorAggregate::getIterator() |
||
| 124 | * @return \ArrayIterator |
||
| 125 | */ |
||
| 126 | 15 | public function getIterator() { |
|
| 129 | } |
||
| 130 |