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 Sonyflake extends Snowflake |
||
14 | { |
||
15 | const MAX_TIMESTAMP_LENGTH = 39; |
||
16 | |||
17 | const MAX_MACHINEID_LENGTH = 16; |
||
18 | |||
19 | const MAX_SEQUENCE_LENGTH = 8; |
||
20 | |||
21 | /** |
||
22 | * The machine ID. |
||
23 | * |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $machineid; |
||
27 | |||
28 | /** |
||
29 | * Build Sonyflake Instance. |
||
30 | * |
||
31 | * @param int $machineid machine ID 0 ~ 65535 (2^16)-1 |
||
32 | */ |
||
33 | 8 | public function __construct(int $machineid = 0) |
|
42 | |||
43 | /** |
||
44 | * Get Sonyflake id. |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | 2 | public function id() |
|
74 | |||
75 | /** |
||
76 | * Set start time (millisecond). |
||
77 | */ |
||
78 | 2 | View Code Duplication | public function setStartTimeStamp(int $startTime) |
94 | |||
95 | /** |
||
96 | * Parse snowflake id. |
||
97 | */ |
||
98 | 1 | public function parseId(string $id, $transform = false): array |
|
113 | |||
114 | /** |
||
115 | * The Elapsed Time. |
||
116 | * |
||
117 | * @return int |
||
118 | */ |
||
119 | 2 | private function elapsedTime() |
|
123 | } |
||
124 |
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.