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 |
||
11 | class Debug |
||
12 | { |
||
13 | use \PHPDaemon\Traits\ClassWatchdog; |
||
14 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
15 | |||
16 | /** |
||
17 | * Export binary data |
||
18 | * @param string $str String |
||
19 | * @param boolean $all Whether to replace all of chars with escaped sequences |
||
20 | * @return string Escaped string |
||
21 | */ |
||
22 | public static function exportBytes($str, $all = false) |
||
40 | |||
41 | /** |
||
42 | * Returns pretty-printed JSON |
||
43 | * @param mixed $m Data |
||
44 | * @return string |
||
45 | */ |
||
46 | public static function prettyJson($m) |
||
50 | |||
51 | /** |
||
52 | * Returns JSON |
||
53 | * @param mixed $m Data |
||
54 | * @return string |
||
55 | */ |
||
56 | public static function json($m) |
||
60 | |||
61 | /** |
||
62 | * Returns a proxy callback function with logging for debugging purposes |
||
63 | * @param callable $cb Callback |
||
64 | * @param mixed $name Data |
||
65 | * @return callable |
||
66 | */ |
||
67 | public static function proxy($cb, $name = null) |
||
77 | |||
78 | /** |
||
79 | * Wrapper of var_dump |
||
80 | * @return string Result of var_dump() |
||
81 | */ |
||
82 | View Code Duplication | public static function dump() |
|
95 | |||
96 | /** |
||
97 | * Get refcount of the given variable |
||
98 | * @param mixed &$var Value |
||
99 | * @return integer |
||
100 | */ |
||
101 | public static function refcount(&$var) |
||
109 | |||
110 | /** |
||
111 | * Wrapper of debug_zval_dump |
||
112 | * @return string Result of debug_zval_dump() |
||
113 | */ |
||
114 | View Code Duplication | public static function zdump() |
|
127 | |||
128 | /** |
||
129 | * Returns textual backtrace |
||
130 | * @return string |
||
131 | */ |
||
132 | public static function backtrace($bool = false) |
||
154 | } |
||
155 |
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.