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 |
||
16 | class Stdout extends AbstractAdapter |
||
17 | { |
||
18 | /** |
||
19 | * Bash fg colors |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | public $foreground_colors = [ |
||
24 | 'black' => '0;30', |
||
25 | 'dark_gray' => '1;30', |
||
26 | 'blue' => '0;34', |
||
27 | 'light_blue' => '1;34', |
||
28 | 'green' => '0;32', |
||
29 | 'light_green' => '1;32', |
||
30 | 'cyan' => '0;36', |
||
31 | 'light_cyan' => '1;36', |
||
32 | 'red' => '0;31', |
||
33 | 'light_red' => '1;31', |
||
34 | 'purple' => '0;35', |
||
35 | 'light_purple' => '1;35', |
||
36 | 'brown' => '0;33', |
||
37 | 'yellow' => '1;33', |
||
38 | 'light_gray' => '0;37', |
||
39 | 'white' => '1;37', |
||
40 | ]; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Bash bg colors |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | public $background_colors = [ |
||
49 | 'black' => '40', |
||
50 | 'red' => '41', |
||
51 | 'green' => '42', |
||
52 | 'yellow' => '43', |
||
53 | 'blue' => '44', |
||
54 | 'magenta' => '45', |
||
55 | 'cyan' => '46', |
||
56 | 'light_gray' => '47', |
||
57 | ]; |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Convert to bash color |
||
62 | * |
||
63 | * @param string $string |
||
64 | * @param string $foreground_color |
||
65 | * @param string $background_color |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getColoredString(string $string, string $foreground_color = null, string $background_color = null): string |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Log |
||
88 | * |
||
89 | * @param string $level |
||
90 | * @param string $message |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function log(string $level, string $message): bool |
||
132 | } |
||
133 |
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.