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 |
||
18 | class GeneralNames implements |
||
19 | \Countable, \IteratorAggregate |
||
20 | { |
||
21 | /** |
||
22 | * GeneralName objects. |
||
23 | * |
||
24 | * @var GeneralName[] $_names |
||
25 | */ |
||
26 | protected $_names; |
||
27 | |||
28 | /** |
||
29 | * Constructor |
||
30 | * |
||
31 | * @param GeneralName ...$names One or more GeneralName objects |
||
32 | */ |
||
33 | 48 | public function __construct(GeneralName ...$names) { |
|
36 | |||
37 | /** |
||
38 | * Initialize from ASN.1. |
||
39 | * |
||
40 | * @param Sequence $seq |
||
41 | * @throws \UnexpectedValueException |
||
42 | * @return self |
||
43 | */ |
||
44 | 30 | View Code Duplication | public static function fromASN1(Sequence $seq) { |
55 | |||
56 | /** |
||
57 | * Find first GeneralName by given tag. |
||
58 | * |
||
59 | * @param int $tag |
||
60 | * @return GeneralName|null |
||
61 | */ |
||
62 | 30 | protected function _findFirst($tag) { |
|
70 | |||
71 | /** |
||
72 | * Check whether GeneralNames contains a GeneralName of given type. |
||
73 | * |
||
74 | * @param int $tag One of <code>GeneralName::TAG_*</code> enumerations |
||
75 | * @return bool |
||
76 | */ |
||
77 | 2 | public function has($tag) { |
|
80 | |||
81 | /** |
||
82 | * Get first GeneralName of given type. |
||
83 | * |
||
84 | * @param int $tag One of <code>GeneralName::TAG_*</code> enumerations |
||
85 | * @throws \OutOfBoundsException |
||
86 | * @return GeneralName |
||
87 | */ |
||
88 | 28 | public function firstOf($tag) { |
|
95 | |||
96 | /** |
||
97 | * Get all GeneralName objects of given type. |
||
98 | * |
||
99 | * @param int $tag One of <code>GeneralName::TAG_*</code> enumerations |
||
100 | * @return GeneralName[] |
||
101 | */ |
||
102 | 3 | public function allOf($tag) { |
|
109 | |||
110 | /** |
||
111 | * Get value of the first 'dNSName' type. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | 2 | View Code Duplication | public function firstDNS() { |
116 | 2 | $gn = $this->firstOf(GeneralName::TAG_DNS_NAME); |
|
117 | 2 | if (!$gn instanceof DNSName) { |
|
118 | 1 | throw new \RuntimeException( |
|
119 | 1 | DNSName::class . " expected, got " . get_class($gn)); |
|
120 | } |
||
121 | 1 | return $gn->name(); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Get value of the first 'directoryName' type. |
||
126 | * |
||
127 | * @return Name |
||
128 | */ |
||
129 | 11 | View Code Duplication | public function firstDN() { |
130 | 11 | $gn = $this->firstOf(GeneralName::TAG_DIRECTORY_NAME); |
|
131 | 11 | if (!$gn instanceof DirectoryName) { |
|
132 | 1 | throw new \RuntimeException( |
|
133 | 1 | DirectoryName::class . " expected, got " . get_class($gn)); |
|
134 | } |
||
135 | 10 | return $gn->dn(); |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Get value of the first 'uniformResourceIdentifier' type. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | 3 | View Code Duplication | public function firstURI() { |
144 | 3 | $gn = $this->firstOf(GeneralName::TAG_URI); |
|
145 | 3 | if (!$gn instanceof UniformResourceIdentifier) { |
|
146 | 1 | throw new \RuntimeException( |
|
147 | 1 | UniformResourceIdentifier::class . " expected, got " . |
|
148 | 1 | get_class($gn)); |
|
149 | } |
||
150 | 2 | return $gn->uri(); |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Generate ASN.1 structure. |
||
155 | * |
||
156 | * @return Sequence |
||
157 | */ |
||
158 | 40 | View Code Duplication | public function toASN1() { |
169 | |||
170 | /** |
||
171 | * |
||
172 | * @see Countable::count() |
||
173 | * @return int |
||
174 | */ |
||
175 | 1 | public function count() { |
|
178 | |||
179 | /** |
||
180 | * Get iterator for GeneralName objects. |
||
181 | * |
||
182 | * @see IteratorAggregate::getIterator() |
||
183 | * @return \ArrayIterator |
||
184 | */ |
||
185 | 1 | public function getIterator() { |
|
188 | } |
||
189 |