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 |
||
25 | final class Uuid |
||
26 | { |
||
27 | /** |
||
28 | * 32-bit integer that identifies this host. |
||
29 | * |
||
30 | * @var integer |
||
31 | */ |
||
32 | private static $node = null; |
||
33 | |||
34 | /** |
||
35 | * Process identifier. |
||
36 | * |
||
37 | * @var integer |
||
38 | */ |
||
39 | private static $pid = null; |
||
40 | |||
41 | private static $hostIp, $hostName; |
||
42 | |||
43 | public static function setup($hostIp, $hostName) |
||
48 | |||
49 | /** |
||
50 | * Returns a 32-bit integer that identifies this host. |
||
51 | * |
||
52 | * The node identifier needs to be unique among nodes |
||
53 | * in a cluster for a given application in order to |
||
54 | * avoid collisions between generated identifiers. |
||
55 | * |
||
56 | * @return integer |
||
57 | */ |
||
58 | private static function getNodeId() |
||
59 | { |
||
60 | if (self::$hostIp !== null) { |
||
61 | return ip2long(self::$hostIp); |
||
62 | } |
||
63 | |||
64 | self::$hostIp = '127.0.0.1'; |
||
65 | |||
66 | if (self::$hostName !== null) { |
||
67 | self::$hostIp = crc32(self::$hostName); |
||
68 | } |
||
69 | |||
70 | View Code Duplication | if (true === function_exists('php_uname')) { |
|
|
|||
71 | self::$hostName = php_uname('n'); |
||
72 | self::$hostIp = gethostbyname(self::$hostName); |
||
73 | } |
||
74 | |||
75 | View Code Duplication | if (true === function_exists('gethostname')) { |
|
76 | self::$hostName = gethostname(); |
||
77 | self::$hostIp = gethostbyname(self::$hostName); |
||
78 | } |
||
79 | |||
80 | return ip2long(self::$hostIp); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Returns a process identifier. |
||
85 | * |
||
86 | * In multi-process servers, this should be the system process ID. |
||
87 | * In multi-threaded servers, this should be some unique ID to |
||
88 | * prevent two threads from generating precisely the same UUID |
||
89 | * at the same time. |
||
90 | * |
||
91 | * @return integer |
||
92 | */ |
||
93 | private static function getLockId() |
||
97 | |||
98 | /** |
||
99 | * Generate an RFC 4122 UUID. |
||
100 | * |
||
101 | * This is pseudo-random UUID influenced by the system clock, IP |
||
102 | * address and process ID. |
||
103 | * |
||
104 | * The intended use is to generate an identifier that can uniquely |
||
105 | * identify user generated posts, comments etc. made to a website. |
||
106 | * This generation process should be sufficient to avoid collisions |
||
107 | * between nodes in a cluster, and between apache children on the |
||
108 | * same host. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public static function generate() |
||
147 | } |
||
148 |
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.