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 |
||
14 | class Style |
||
15 | { |
||
16 | protected static $availableForegroundColors = [ |
||
17 | 'black' => ['set' => 30, 'unset' => 39], |
||
18 | 'red' => ['set' => 31, 'unset' => 39], |
||
19 | 'green' => ['set' => 32, 'unset' => 39], |
||
20 | 'yellow' => ['set' => 33, 'unset' => 39], |
||
21 | 'blue' => ['set' => 34, 'unset' => 39], |
||
22 | 'magenta' => ['set' => 35, 'unset' => 39], |
||
23 | 'cyan' => ['set' => 36, 'unset' => 39], |
||
24 | 'white' => ['set' => 37, 'unset' => 39], |
||
25 | ]; |
||
26 | |||
27 | protected static $availableBackgroundColors = [ |
||
28 | 'black' => ['set' => 40, 'unset' => 49], |
||
29 | 'red' => ['set' => 41, 'unset' => 49], |
||
30 | 'green' => ['set' => 42, 'unset' => 49], |
||
31 | 'yellow' => ['set' => 43, 'unset' => 49], |
||
32 | 'blue' => ['set' => 44, 'unset' => 49], |
||
33 | 'magenta' => ['set' => 45, 'unset' => 49], |
||
34 | 'cyan' => ['set' => 46, 'unset' => 49], |
||
35 | 'white' => ['set' => 47, 'unset' => 49], |
||
36 | ]; |
||
37 | |||
38 | protected static $availableOptions = [ |
||
39 | 'bold' => ['set' => 1, 'unset' => 22], |
||
40 | 'underscore' => ['set' => 4, 'unset' => 24], |
||
41 | 'blink' => ['set' => 5, 'unset' => 25], |
||
42 | 'reverse' => ['set' => 7, 'unset' => 27], |
||
43 | 'conceal' => ['set' => 8, 'unset' => 28], |
||
44 | ]; |
||
45 | |||
46 | private $foreground; |
||
47 | private $background; |
||
48 | private $options = []; |
||
49 | |||
50 | /** |
||
51 | * 初始化输出的样式 |
||
52 | * @param string|null $foreground 字体颜色 |
||
53 | * @param string|null $background 背景色 |
||
54 | * @param array $options 格式 |
||
55 | * @api |
||
56 | */ |
||
57 | public function __construct($foreground = null, $background = null, array $options = []) |
||
69 | |||
70 | /** |
||
71 | * 设置字体颜色 |
||
72 | * @param string|null $color 颜色名 |
||
73 | * @throws \InvalidArgumentException |
||
74 | * @api |
||
75 | */ |
||
76 | View Code Duplication | public function setForeground($color = null) |
|
90 | |||
91 | /** |
||
92 | * 设置背景色 |
||
93 | * @param string|null $color 颜色名 |
||
94 | * @throws \InvalidArgumentException |
||
95 | * @api |
||
96 | */ |
||
97 | View Code Duplication | public function setBackground($color = null) |
|
111 | |||
112 | /** |
||
113 | * 设置字体格式 |
||
114 | * @param string $option 格式名 |
||
115 | * @throws \InvalidArgumentException When the option name isn't defined |
||
116 | * @api |
||
117 | */ |
||
118 | public function setOption(string $option): void |
||
128 | |||
129 | /** |
||
130 | * 重置字体格式 |
||
131 | * @param string $option 格式名 |
||
132 | * @throws \InvalidArgumentException |
||
133 | */ |
||
134 | public function unsetOption(string $option): void |
||
145 | |||
146 | /** |
||
147 | * 批量设置字体格式 |
||
148 | * @param array $options |
||
149 | */ |
||
150 | public function setOptions(array $options) |
||
158 | |||
159 | /** |
||
160 | * 应用样式到文字 |
||
161 | * @param string $text 文字 |
||
162 | * @return string |
||
163 | */ |
||
164 | public function apply(string $text): string |
||
190 | } |
||
191 |
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.