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() |
|
47 | { |
||
48 | 5 | if (!$this->bit) { |
|
49 | 1 | return []; |
|
50 | } |
||
51 | |||
52 | 5 | $values = []; |
|
53 | 5 | foreach (self::$bits[get_called_class()] as $bit => $value) { |
|
54 | 5 | if ($this->bit & $bit) { |
|
55 | 5 | $values[] = $value; |
|
56 | 5 | } |
|
57 | 5 | } |
|
58 | |||
59 | 5 | return $values; |
|
60 | } |
||
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 | 23 | final public function equal(Set $set) |
|
113 | |||
114 | /** |
||
115 | * Check if this Set is a subset of other. |
||
116 | * |
||
117 | * @param Set $set |
||
118 | * |
||
119 | 3 | * @return bool |
|
120 | */ |
||
121 | 3 | final public function subset(Set $set) |
|
127 | |||
128 | /** |
||
129 | * Check if this Set is a superset of other. |
||
130 | * |
||
131 | * @param Set $set |
||
132 | * |
||
133 | 2 | * @return bool |
|
134 | */ |
||
135 | 2 | final public function superset(Set $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 | 2 | * @return Set |
|
148 | */ |
||
149 | 2 | View Code Duplication | final public function union(Set $set) |
163 | 2 | ||
164 | 2 | /** |
|
165 | 2 | * Produce a new set with enum common to both this and other (this & other). |
|
166 | * |
||
167 | 1 | * @param Set ...$set Other Set(s) of the same enumeration to produce the union |
|
168 | 1 | * |
|
169 | * @return Set |
||
170 | 1 | */ |
|
171 | 1 | View Code Duplication | final public function intersect(Set $set) |
185 | 2 | ||
186 | 2 | /** |
|
187 | 2 | * Produce a new set with enum in this but not in other (this - other). |
|
188 | * |
||
189 | 1 | * @param Set ...$set Other Set(s) of the same enumeration to produce the union |
|
190 | 1 | * |
|
191 | * @return Set |
||
192 | 1 | */ |
|
193 | 1 | View Code Duplication | final public function diff(Set $set) |
207 | 2 | ||
208 | 2 | /** |
|
209 | 2 | * Produce a new set with enum in either this and other but not in both (this ^ (other | other)). |
|
210 | * |
||
211 | 1 | * @param Set ...$set Other Set(s) of the same enumeration to produce the union |
|
212 | 1 | * |
|
213 | * @return Set |
||
214 | 1 | */ |
|
215 | 1 | View Code Duplication | final public function symDiff(Set $set) |
229 | 2 | ||
230 | 2 | /** |
|
231 | 2 | * Get choices for checkbox group. |
|
232 | * |
||
233 | 1 | * <code> |
|
234 | 1 | * { |
|
235 | * value1: 'Readable value 1', |
||
236 | 1 | * value2: 'Readable value 2', |
|
237 | 1 | * } |
|
238 | * </code> |
||
239 | 1 | * |
|
240 | * @return array |
||
241 | */ |
||
242 | final public static function choices() |
||
254 | 1 | ||
255 | /** |
||
256 | 1 | * @param mixed $value |
|
257 | 1 | * |
|
258 | * @return string |
||
259 | 1 | */ |
|
260 | 1 | public static function readable($value) |
|
266 | |||
267 | /** |
||
268 | * @param mixed $value |
||
269 | */ |
||
270 | private static function validateValue($value) |
||
276 | 2 | ||
277 | /** |
||
278 | * @param object $object |
||
279 | */ |
||
280 | private static function validateType($object) |
||
287 | 23 | ||
288 | /** |
||
289 | * @param string $class |
||
290 | */ |
||
291 | private static function detectConstants($class) |
||
305 | 23 | ||
306 | 1 | /** |
|
307 | 1 | * @param mixed $value |
|
308 | * |
||
309 | 1 | * @return int |
|
310 | 1 | */ |
|
311 | 1 | private function bit($value) |
|
315 | 1 | ||
316 | 23 | /** |
|
317 | * @return array |
||
318 | */ |
||
319 | private static function bits() |
||
326 | } |
||
327 |