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 |
||
| 16 | class PolicyMappingsExtension extends Extension implements |
||
| 17 | \Countable, |
||
|
|
|||
| 18 | \IteratorAggregate |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Policy mappings. |
||
| 22 | * |
||
| 23 | * @var PolicyMapping[] $_mappings |
||
| 24 | */ |
||
| 25 | protected $_mappings; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constructor |
||
| 29 | * |
||
| 30 | * @param bool $critical |
||
| 31 | * @param PolicyMapping ...$mappings One or more PolicyMapping objects |
||
| 32 | */ |
||
| 33 | 5 | public function __construct($critical, PolicyMapping ...$mappings) { |
|
| 34 | 5 | parent::__construct(self::OID_POLICY_MAPPINGS, $critical); |
|
| 35 | 5 | $this->_mappings = $mappings; |
|
| 36 | 5 | } |
|
| 37 | |||
| 38 | 2 | View Code Duplication | protected static function _fromDER($data, $critical) { |
| 39 | 2 | $mappings = array_map( |
|
| 40 | function (UnspecifiedType $el) { |
||
| 41 | 1 | return PolicyMapping::fromASN1($el->asSequence()); |
|
| 42 | 2 | }, Sequence::fromDER($data)->elements()); |
|
| 43 | 2 | if (!count($mappings)) { |
|
| 44 | 1 | throw new \UnexpectedValueException( |
|
| 45 | 1 | "PolicyMappings must have at least one mapping."); |
|
| 46 | } |
||
| 47 | 1 | return new self($critical, ...$mappings); |
|
| 48 | } |
||
| 49 | |||
| 50 | 6 | View Code Duplication | protected function _valueASN1() { |
| 51 | 6 | if (!count($this->_mappings)) { |
|
| 52 | 1 | throw new \LogicException("No mappings."); |
|
| 53 | } |
||
| 54 | 5 | $elements = array_map( |
|
| 55 | function (PolicyMapping $mapping) { |
||
| 56 | 5 | return $mapping->toASN1(); |
|
| 57 | 5 | }, $this->_mappings); |
|
| 58 | 5 | return new Sequence(...$elements); |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get all mappings. |
||
| 63 | * |
||
| 64 | * @return PolicyMapping[] |
||
| 65 | */ |
||
| 66 | 2 | public function mappings() { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Get mappings flattened into a single array of arrays of subject domains |
||
| 72 | * keyed by issuer domain. |
||
| 73 | * |
||
| 74 | * Eg. if policy mappings contains multiple mappings with the same issuer |
||
| 75 | * domain policy, their corresponding subject domain policies are placed |
||
| 76 | * under the same key. |
||
| 77 | * |
||
| 78 | * @return (string[])[] |
||
| 79 | */ |
||
| 80 | 2 | public function flattenedMappings() { |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Get all subject domain policy OIDs that are mapped to given issuer |
||
| 94 | * domain policy OID. |
||
| 95 | * |
||
| 96 | * @param string $oid Issuer domain policy |
||
| 97 | * @return string[] List of OIDs in dotted format |
||
| 98 | */ |
||
| 99 | 1 | public function issuerMappings($oid) { |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Get all mapped issuer domain policy OIDs. |
||
| 111 | * |
||
| 112 | * @return string[] |
||
| 113 | */ |
||
| 114 | 1 | public function issuerDomainPolicies() { |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Check whether policy mappings have anyPolicy mapped. |
||
| 124 | * |
||
| 125 | * RFC 5280 section 4.2.1.5 states that "Policies MUST NOT be mapped either |
||
| 126 | * to or from the special value anyPolicy". |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | 7 | public function hasAnyPolicyMapping() { |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Get the number of mappings. |
||
| 146 | * |
||
| 147 | * @see Countable::count() |
||
| 148 | * @return int |
||
| 149 | */ |
||
| 150 | 1 | public function count() { |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Get iterator for policy mappings. |
||
| 156 | * |
||
| 157 | * @see IteratorAggregate::getIterator() |
||
| 158 | * @return \ArrayIterator |
||
| 159 | */ |
||
| 160 | 1 | public function getIterator() { |
|
| 163 | } |
||
| 164 |