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 | 10 | public function __construct($critical, PolicyInformation ...$policies) { |
|
| 33 | 10 | parent::__construct(Extension::OID_CERTIFICATE_POLICIES, $critical); |
|
| 34 | 10 | $this->_policies = array(); |
|
| 35 | 10 | foreach ($policies as $policy) { |
|
| 36 | 9 | $this->_policies[$policy->oid()] = $policy; |
|
| 37 | 10 | } |
|
| 38 | 10 | } |
|
| 39 | |||
| 40 | 8 | View Code Duplication | protected static function _fromDER($data, $critical) { |
| 41 | 8 | $policies = array_map( |
|
| 42 | function (UnspecifiedType $el) { |
||
| 43 | 7 | return PolicyInformation::fromASN1($el->asSequence()); |
|
| 44 | 8 | }, Sequence::fromDER($data)->elements()); |
|
| 45 | 8 | if (!count($policies)) { |
|
| 46 | 1 | throw new \UnexpectedValueException( |
|
| 47 | "certificatePolicies must contain" . |
||
| 48 | 1 | " at least one PolicyInformation."); |
|
| 49 | } |
||
| 50 | 7 | return new self($critical, ...$policies); |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Check whether policy information by OID is present. |
||
| 55 | * |
||
| 56 | * @param string $oid |
||
| 57 | * @return boolean |
||
| 58 | */ |
||
| 59 | 3 | public function has($oid) { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Check whether anyPolicy is present. |
||
| 65 | * |
||
| 66 | * @return bool |
||
| 67 | */ |
||
| 68 | 1 | public function hasAnyPolicy() { |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Get policy information by OID. |
||
| 74 | * |
||
| 75 | * @param string $oid |
||
| 76 | * @throws \LogicException |
||
| 77 | * @return PolicyInformation |
||
| 78 | */ |
||
| 79 | 3 | public function get($oid) { |
|
| 80 | 3 | if (!$this->has($oid)) { |
|
| 81 | 1 | throw new \LogicException("Not certificate policy by OID $oid."); |
|
| 82 | } |
||
| 83 | 2 | return $this->_policies[$oid]; |
|
| 84 | } |
||
| 85 | |||
| 86 | 21 | View Code Duplication | protected function _valueASN1() { |
| 87 | 21 | if (!count($this->_policies)) { |
|
| 88 | 1 | throw new \LogicException("No policies."); |
|
| 89 | } |
||
| 90 | 20 | $elements = array_map( |
|
| 91 | 20 | function (PolicyInformation $pi) { |
|
| 92 | 20 | return $pi->toASN1(); |
|
| 93 | 20 | }, array_values($this->_policies)); |
|
| 94 | 20 | return new Sequence(...$elements); |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get the number of policies. |
||
| 99 | * |
||
| 100 | * @see Countable::count() |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | 1 | public function count() { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Get iterator for policy information terms. |
||
| 109 | * |
||
| 110 | * @see IteratorAggregate::getIterator() |
||
| 111 | * @return \ArrayIterator |
||
| 112 | */ |
||
| 113 | 1 | public function getIterator() { |
|
| 116 | } |
||
| 117 |