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 Set |
||
17 | { |
||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private $bit = 0; |
||
22 | |||
23 | /** |
||
24 | * @var mixed[][] |
||
25 | */ |
||
26 | private static $bits = []; |
||
27 | |||
28 | /** |
||
29 | * @var mixed[][] |
||
30 | */ |
||
31 | private static $keys = []; |
||
32 | |||
33 | /** |
||
34 | * @param array $values |
||
35 | */ |
||
36 | 23 | final public function __construct(array $values = []) |
|
42 | |||
43 | /** |
||
44 | * @return mixed[] |
||
45 | */ |
||
46 | 5 | final public function values() |
|
61 | |||
62 | /** |
||
63 | * Attach the given value. |
||
64 | * |
||
65 | * @param mixed $value |
||
66 | */ |
||
67 | 23 | final public function attach($value) |
|
73 | |||
74 | /** |
||
75 | * Detach the given value. |
||
76 | * |
||
77 | * @param mixed $value |
||
78 | */ |
||
79 | 2 | final public function detach($value) |
|
85 | |||
86 | /** |
||
87 | * Given value was attached. |
||
88 | * |
||
89 | * @param mixed $value |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | 2 | final public function contains($value) |
|
99 | |||
100 | /** |
||
101 | * Check if this Set is the same as other. |
||
102 | * |
||
103 | * @param Set $set |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | 3 | final public function equal(self $set) |
|
113 | |||
114 | /** |
||
115 | * Check if this Set is a subset of other. |
||
116 | * |
||
117 | * @param Set $set |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | 2 | final public function subset(self $set) |
|
127 | |||
128 | /** |
||
129 | * Check if this Set is a superset of other. |
||
130 | * |
||
131 | * @param Set $set |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | 2 | final public function superset(self $set) |
|
141 | |||
142 | /** |
||
143 | * Produce a new set with enum from both this and other (this | other). |
||
144 | * |
||
145 | * @param Set ...$set Other Set(s) of the same enum to produce the union |
||
146 | * |
||
147 | * @return Set |
||
148 | */ |
||
149 | 2 | View Code Duplication | final public function union(self $set) |
163 | |||
164 | /** |
||
165 | * Produce a new set with enum common to both this and other (this & other). |
||
166 | * |
||
167 | * @param Set ...$set Other Set(s) of the same enumeration to produce the union |
||
168 | * |
||
169 | * @return Set |
||
170 | */ |
||
171 | 2 | View Code Duplication | final public function intersect(self $set) |
185 | |||
186 | /** |
||
187 | * Produce a new set with enum in this but not in other (this - other). |
||
188 | * |
||
189 | * @param Set ...$set Other Set(s) of the same enumeration to produce the union |
||
190 | * |
||
191 | * @return Set |
||
192 | */ |
||
193 | 2 | View Code Duplication | final public function diff(self $set) |
207 | |||
208 | /** |
||
209 | * Produce a new set with enum in either this and other but not in both (this ^ (other | other)). |
||
210 | * |
||
211 | * @param Set ...$set Other Set(s) of the same enumeration to produce the union |
||
212 | * |
||
213 | * @return Set |
||
214 | */ |
||
215 | 2 | View Code Duplication | final public function symDiff(self $set) |
229 | |||
230 | /** |
||
231 | * Get choices for checkbox group. |
||
232 | * |
||
233 | * <code> |
||
234 | * { |
||
235 | * value1: 'Readable value 1', |
||
236 | * value2: 'Readable value 2', |
||
237 | * } |
||
238 | * </code> |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | 2 | final public static function choices() |
|
254 | |||
255 | /** |
||
256 | * @param mixed $value |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | 3 | public static function readable($value) |
|
266 | |||
267 | /** |
||
268 | * @param mixed $value |
||
269 | */ |
||
270 | 23 | private static function validateValue($value) |
|
276 | |||
277 | /** |
||
278 | * @param object $object |
||
279 | */ |
||
280 | 15 | private static function validateType($object) |
|
287 | |||
288 | /** |
||
289 | * @param string $class |
||
290 | */ |
||
291 | 23 | private static function detectConstants($class) |
|
305 | |||
306 | /** |
||
307 | * @param mixed $value |
||
308 | * |
||
309 | * @return int |
||
310 | */ |
||
311 | 23 | private function bit($value) |
|
315 | |||
316 | /** |
||
317 | * @return array |
||
318 | */ |
||
319 | 23 | private static function bits() |
|
326 | } |
||
327 |