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 |
||
15 | class StringBitSet implements BitSet |
||
16 | { |
||
17 | const BITS = 8; |
||
18 | |||
19 | const MASKS = [ |
||
20 | "\x01", |
||
21 | "\x02", |
||
22 | "\x04", |
||
23 | "\x08", |
||
24 | "\x10", |
||
25 | "\x20", |
||
26 | "\x40", |
||
27 | "\x80", |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | public $numBits; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | public $str; |
||
39 | |||
40 | /** |
||
41 | * StringBitSet constructor. |
||
42 | * |
||
43 | * @param int $numBits |
||
44 | */ |
||
45 | public function __construct(int $numBits) |
||
50 | |||
51 | /** |
||
52 | * @param int $i |
||
53 | * |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function testBit(int $i): bool |
||
62 | |||
63 | /** |
||
64 | * @param int $i |
||
65 | */ |
||
66 | View Code Duplication | public function setBit(int $i) |
|
73 | |||
74 | /** |
||
75 | * @param int $i |
||
76 | */ |
||
77 | View Code Duplication | public function clearBit(int $i) |
|
84 | |||
85 | /** |
||
86 | * @param BitSet $other |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function or(BitSet $other): bool |
||
106 | |||
107 | /** |
||
108 | * @return \Generator |
||
109 | */ |
||
110 | public function getIterator(): \Generator |
||
118 | } |
||
119 |
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.