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 |
||
14 | class JWK implements \Countable, \IteratorAggregate |
||
15 | { |
||
16 | use TypedJWK; |
||
17 | |||
18 | /** |
||
19 | * Parameters. |
||
20 | * |
||
21 | * @var JWKParameter[] $_parameters |
||
22 | */ |
||
23 | protected $_parameters; |
||
24 | |||
25 | /** |
||
26 | * Constructor |
||
27 | * |
||
28 | * @param JWKParameter ...$params |
||
29 | */ |
||
30 | 147 | public function __construct(JWKParameter ...$params) { |
|
36 | |||
37 | /** |
||
38 | * Initialize from an array representing a JSON object. |
||
39 | * |
||
40 | * @param array $members |
||
41 | * @return self |
||
42 | */ |
||
43 | 35 | public static function fromArray(array $members) { |
|
50 | |||
51 | /** |
||
52 | * Initialize from a JSON string. |
||
53 | * |
||
54 | * @param string $json |
||
55 | * @throws \UnexpectedValueException |
||
56 | * @return self |
||
57 | */ |
||
58 | 11 | View Code Duplication | public static function fromJSON($json) { |
65 | |||
66 | /** |
||
67 | * Initialize from another JWK. |
||
68 | * |
||
69 | * Allows casting to subclass by late static binding. |
||
70 | * |
||
71 | * @param JWK $jwk |
||
72 | * @return self |
||
73 | */ |
||
74 | 58 | public static function fromJWK(JWK $jwk) { |
|
77 | |||
78 | /** |
||
79 | * Get self with parameters added. |
||
80 | * |
||
81 | * @param JWKParameter ...$params |
||
82 | * @return self |
||
83 | */ |
||
84 | 10 | public function withParameters(JWKParameter ...$params) { |
|
91 | |||
92 | /** |
||
93 | * Get all parameters. |
||
94 | * |
||
95 | * @return JWKParameter[] |
||
96 | */ |
||
97 | 1 | public function parameters() { |
|
100 | |||
101 | /** |
||
102 | * Get self with given key ID added to parameters. |
||
103 | * |
||
104 | * @param string $id Key ID as a string |
||
105 | * @return self |
||
106 | */ |
||
107 | 4 | public function withKeyID($id) { |
|
110 | |||
111 | /** |
||
112 | * Whether parameters are present. |
||
113 | * |
||
114 | * Returns false if any of the given parameters is not set. |
||
115 | * |
||
116 | * @param string ...$names Parameter names |
||
117 | * @return bool |
||
118 | */ |
||
119 | 219 | View Code Duplication | public function has(...$names) { |
127 | |||
128 | /** |
||
129 | * Get a parameter. |
||
130 | * |
||
131 | * @param string $name Parameter name |
||
132 | * @throws \LogicException |
||
133 | * @return JWKParameter |
||
134 | */ |
||
135 | 186 | public function get($name) { |
|
141 | |||
142 | /** |
||
143 | * Convert to array. |
||
144 | * |
||
145 | * @return array Parameter values keyed by parameter names |
||
146 | */ |
||
147 | 4 | public function toArray() { |
|
154 | |||
155 | /** |
||
156 | * Convert to JSON. |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | 2 | public function toJSON() { |
|
167 | |||
168 | /** |
||
169 | * Get the number of parameters. |
||
170 | * |
||
171 | * @see Countable::count() |
||
172 | */ |
||
173 | 1 | public function count() { |
|
176 | |||
177 | /** |
||
178 | * Get iterator for the parameters. |
||
179 | * |
||
180 | * @see IteratorAggregate::getIterator() |
||
181 | * @return \ArrayIterator |
||
182 | */ |
||
183 | 1 | public function getIterator() { |
|
186 | } |
||
187 |
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.