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 |
||
15 | class MenuStyle |
||
16 | { |
||
17 | /** |
||
18 | * @var Terminal |
||
19 | */ |
||
20 | protected $terminal; |
||
21 | |||
22 | /** |
||
23 | * @var int|string |
||
24 | */ |
||
25 | protected $fg; |
||
26 | |||
27 | /** |
||
28 | * @var int|string |
||
29 | */ |
||
30 | protected $bg; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $width; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $padding; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $margin; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $contentWidth; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $selectedMarker; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $unselectedMarker; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $itemExtra; |
||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $displaysExtra; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | private $titleSeparator; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | private $coloursSetCode; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | private $invertedColoursSetCode = "\033[7m"; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | private $invertedColoursUnsetCode = "\033[27m"; |
||
91 | |||
92 | /** |
||
93 | * @var string |
||
94 | */ |
||
95 | private $coloursResetCode = "\033[0m"; |
||
96 | |||
97 | /** |
||
98 | * Default Values |
||
99 | * |
||
100 | * @var array |
||
101 | */ |
||
102 | private static $defaultStyleValues = [ |
||
103 | 'fg' => 'white', |
||
104 | 'bg' => 'blue', |
||
105 | 'width' => 100, |
||
106 | 'padding' => 2, |
||
107 | 'margin' => 2, |
||
108 | 'selectedMarker' => '●', |
||
109 | 'unselectedMarker' => '○', |
||
110 | 'itemExtra' => '✔', |
||
111 | 'displaysExtra' => false, |
||
112 | 'titleSeparator' => '=', |
||
113 | ]; |
||
114 | |||
115 | public static function getDefaultStyleValues() : array |
||
119 | |||
120 | /** |
||
121 | * @var array |
||
122 | */ |
||
123 | private static $availableForegroundColors = array( |
||
124 | 'black' => 30, |
||
125 | 'red' => 31, |
||
126 | 'green' => 32, |
||
127 | 'yellow' => 33, |
||
128 | 'blue' => 34, |
||
129 | 'magenta' => 35, |
||
130 | 'cyan' => 36, |
||
131 | 'white' => 37, |
||
132 | 'default' => 39, |
||
133 | ); |
||
134 | |||
135 | /** |
||
136 | * @var array |
||
137 | */ |
||
138 | private static $availableBackgroundColors = array( |
||
139 | 'black' => 40, |
||
140 | 'red' => 41, |
||
141 | 'green' => 42, |
||
142 | 'yellow' => 43, |
||
143 | 'blue' => 44, |
||
144 | 'magenta' => 45, |
||
145 | 'cyan' => 46, |
||
146 | 'white' => 47, |
||
147 | 'default' => 49, |
||
148 | ); |
||
149 | |||
150 | /** |
||
151 | * @var array |
||
152 | */ |
||
153 | private static $availableOptions = array( |
||
154 | 'bold' => array('set' => 1, 'unset' => 22), |
||
155 | 'dim' => array('set' => 2, 'unset' => 22), |
||
156 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
157 | 'blink' => array('set' => 5, 'unset' => 25), |
||
158 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
159 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
160 | ); |
||
161 | |||
162 | /** |
||
163 | * Initialise style |
||
164 | */ |
||
165 | public function __construct(Terminal $terminal = null) |
||
180 | |||
181 | public function getDisabledItemText(string $text) : string |
||
190 | |||
191 | /** |
||
192 | * Generates the ansi escape sequence to set the colours |
||
193 | */ |
||
194 | private function generateColoursSetCode() : void |
||
210 | |||
211 | /** |
||
212 | * Get the colour code for Bg and Fg |
||
213 | */ |
||
214 | public function getColoursSetCode() : string |
||
218 | |||
219 | /** |
||
220 | * Get the inverted escape sequence (used for selected elements) |
||
221 | */ |
||
222 | public function getInvertedColoursSetCode() : string |
||
226 | |||
227 | /** |
||
228 | * Get the inverted escape sequence (used for selected elements) |
||
229 | */ |
||
230 | public function getInvertedColoursUnsetCode() : string |
||
234 | |||
235 | /** |
||
236 | * Get the escape sequence used to reset colours to default |
||
237 | */ |
||
238 | public function getColoursResetCode() : string |
||
242 | |||
243 | /** |
||
244 | * Calculate the contents width |
||
245 | */ |
||
246 | protected function calculateContentWidth() : void |
||
250 | |||
251 | public function getFg() |
||
255 | |||
256 | View Code Duplication | public function setFg($fg, string $fallback = null) : self |
|
267 | |||
268 | public function getBg() |
||
272 | |||
273 | View Code Duplication | public function setBg($bg, string $fallback = null) : self |
|
284 | |||
285 | public function getWidth() : int |
||
289 | |||
290 | public function setWidth(int $width) : self |
||
303 | |||
304 | public function getPadding() : int |
||
308 | |||
309 | public function setPadding(int $padding) : self |
||
317 | |||
318 | public function getMargin() : int |
||
322 | |||
323 | public function setMargin(int $margin) : self |
||
331 | |||
332 | public function getContentWidth() : int |
||
336 | |||
337 | /** |
||
338 | * Get padding for right had side of content |
||
339 | */ |
||
340 | public function getRightHandPadding(int $contentLength) : int |
||
344 | |||
345 | public function getSelectedMarker() : string |
||
349 | |||
350 | public function setSelectedMarker(string $marker) : self |
||
356 | |||
357 | public function getUnselectedMarker() : string |
||
361 | |||
362 | public function setUnselectedMarker(string $marker) : self |
||
368 | |||
369 | /** |
||
370 | * Get the correct marker for the item |
||
371 | */ |
||
372 | public function getMarker(bool $selected) : string |
||
376 | |||
377 | public function setItemExtra(string $itemExtra) : self |
||
383 | |||
384 | public function getItemExtra() : string |
||
388 | |||
389 | public function getDisplaysExtra() : bool |
||
393 | |||
394 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
400 | |||
401 | public function getTitleSeparator() : string |
||
405 | |||
406 | public function setTitleSeparator(string $actionSeparator) : self |
||
412 | } |
||
413 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: