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 |
||
13 | class JWKSet implements \Countable, \IteratorAggregate |
||
14 | { |
||
15 | /** |
||
16 | * JWK objects. |
||
17 | * |
||
18 | * @var JWK[] $_jwks |
||
19 | */ |
||
20 | protected $_jwks; |
||
21 | |||
22 | /** |
||
23 | * Additional members. |
||
24 | * |
||
25 | * @var array $_additional |
||
26 | */ |
||
27 | protected $_additional; |
||
28 | |||
29 | /** |
||
30 | * JWK mappings. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | private $_mappings = array(); |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param JWK ...$jwks |
||
40 | */ |
||
41 | 51 | public function __construct(JWK ...$jwks) { |
|
45 | |||
46 | /** |
||
47 | * Reset internal cache variables on clone. |
||
48 | */ |
||
49 | 1 | public function __clone() { |
|
52 | |||
53 | /** |
||
54 | * Initialize from an array representing a JSON object. |
||
55 | * |
||
56 | * @param array $members |
||
57 | * @throws \UnexpectedValueException |
||
58 | * @return self |
||
59 | */ |
||
60 | 3 | public static function fromArray(array $members) { |
|
74 | |||
75 | /** |
||
76 | * Initialize from a JSON string. |
||
77 | * |
||
78 | * @param string $json |
||
79 | * @throws \UnexpectedValueException |
||
80 | * @return self |
||
81 | */ |
||
82 | 3 | View Code Duplication | public static function fromJSON($json) { |
89 | |||
90 | /** |
||
91 | * Get self with keys added. |
||
92 | * |
||
93 | * @param JWK ...$keys JWK objects |
||
94 | * @return self |
||
95 | */ |
||
96 | 1 | public function withKeys(JWK ...$keys) { |
|
101 | |||
102 | /** |
||
103 | * Get all JWK's in a set. |
||
104 | * |
||
105 | * @return JWK[] |
||
106 | */ |
||
107 | 1 | public function keys() { |
|
110 | |||
111 | /** |
||
112 | * Get the first JWK in the set. |
||
113 | * |
||
114 | * @throws \LogicException |
||
115 | * @return JWK |
||
116 | */ |
||
117 | 5 | public function first() { |
|
123 | |||
124 | /** |
||
125 | * Get JWK by key ID. |
||
126 | * |
||
127 | * @param string $id |
||
128 | * @return JWK|null Null if not found |
||
129 | */ |
||
130 | 18 | protected function _getKeyByID($id) { |
|
134 | |||
135 | /** |
||
136 | * Check whether set has a JWK with a given key ID. |
||
137 | * |
||
138 | * @param string $id |
||
139 | * @return bool |
||
140 | */ |
||
141 | 13 | public function hasKeyID($id) { |
|
144 | |||
145 | /** |
||
146 | * Get a JWK by a key ID. |
||
147 | * |
||
148 | * @param string $id |
||
149 | * @throws \LogicException |
||
150 | * @return JWK |
||
151 | */ |
||
152 | 12 | public function keyByID($id) { |
|
159 | |||
160 | /** |
||
161 | * Get mapping from parameter values of given parameter name to JWK. |
||
162 | * |
||
163 | * Later duplicate value shall override earlier JWK. |
||
164 | * |
||
165 | * @param string $name Parameter name |
||
166 | * @return array |
||
167 | */ |
||
168 | 18 | protected function _getMapping($name) { |
|
181 | |||
182 | /** |
||
183 | * Convert to JSON. |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | 1 | public function toJSON() { |
|
192 | |||
193 | /** |
||
194 | * Get the number of keys. |
||
195 | * |
||
196 | * @see Countable::count() |
||
197 | */ |
||
198 | 10 | public function count() { |
|
201 | |||
202 | /** |
||
203 | * Get iterator for JWK objects. |
||
204 | * |
||
205 | * @see IteratorAggregate::getIterator() |
||
206 | * @return \ArrayIterator |
||
207 | */ |
||
208 | 1 | public function getIterator() { |
|
211 | } |
||
212 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.