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 Common { |
||
14 | /** |
||
15 | * output a string, wrap at 100 chars |
||
16 | * |
||
17 | * @param string $string string to output |
||
18 | * @param int $indent indent |
||
19 | */ |
||
20 | public static function l($string = '', $indent = 0) { |
||
24 | |||
25 | /** |
||
26 | * output a line |
||
27 | * |
||
28 | * @param string $string string to output |
||
29 | */ |
||
30 | public static function ln($string = '') { |
||
33 | |||
34 | /** |
||
35 | * output a error message to the stderr |
||
36 | * |
||
37 | * @param string $msg |
||
38 | */ |
||
39 | public static function e($msg) { |
||
44 | |||
45 | /** |
||
46 | * check arguments for null, throws an exception on null or strlen == 0 |
||
47 | * |
||
48 | * @params ...$things |
||
49 | * @throws \Threema\Core\Exception |
||
50 | */ |
||
51 | public static function required() { |
||
60 | |||
61 | /** |
||
62 | * Append the prefix to the the PublicKey @key |
||
63 | * |
||
64 | * @param string $key PublicKey in hex |
||
65 | * @return null|string |
||
66 | */ |
||
67 | public static function convertPublicKey($key) { |
||
73 | |||
74 | /** |
||
75 | * Extract the PublicKey |
||
76 | * |
||
77 | * @param string $stringWithPrefix PublicKey in hex with the key-prefix |
||
78 | * @return null|string |
||
79 | */ |
||
80 | public static function getPublicKey($stringWithPrefix = null) { |
||
86 | |||
87 | /** |
||
88 | * Append the prefix to the the PrivateKey @key |
||
89 | * |
||
90 | * @param string $key PrivateKey in hex |
||
91 | * @return null|string |
||
92 | */ |
||
93 | public static function convertPrivateKey($key) { |
||
99 | |||
100 | /** |
||
101 | * Extract the PrivateKey |
||
102 | * |
||
103 | * @param string $stringWithPrefix PrivateKey in hex with the key-prefix (@Constants::PRIVATE_KEY_PREFIX) |
||
104 | * @return null|string |
||
105 | */ |
||
106 | public static function getPrivateKey($stringWithPrefix = null) { |
||
112 | } |
||
113 |
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.