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:
Complex classes like MenuStyle often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MenuStyle, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class MenuStyle |
||
17 | { |
||
18 | /** |
||
19 | * @var Terminal |
||
20 | */ |
||
21 | protected $terminal; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $fg; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $bg; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $width; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $margin; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $paddingTopBottom; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $paddingLeftRight; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $paddingTopBottomRows = []; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $contentWidth; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | private $selectedMarker; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | private $unselectedMarker; |
||
72 | |||
73 | /** |
||
74 | * @var string |
||
75 | */ |
||
76 | private $itemExtra; |
||
77 | |||
78 | /** |
||
79 | * @var bool |
||
80 | */ |
||
81 | private $displaysExtra; |
||
82 | |||
83 | /** |
||
84 | * @var string |
||
85 | */ |
||
86 | private $titleSeparator; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | private $coloursSetCode; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | private $invertedColoursSetCode = "\033[7m"; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | private $invertedColoursUnsetCode = "\033[27m"; |
||
102 | |||
103 | /** |
||
104 | * @var string |
||
105 | */ |
||
106 | private $coloursResetCode = "\033[0m"; |
||
107 | |||
108 | /** |
||
109 | * @var int |
||
110 | */ |
||
111 | private $borderTopWidth; |
||
112 | |||
113 | /** |
||
114 | * @var int |
||
115 | */ |
||
116 | private $borderRightWidth; |
||
117 | |||
118 | /** |
||
119 | * @var int |
||
120 | */ |
||
121 | private $borderBottomWidth; |
||
122 | |||
123 | /** |
||
124 | * @var int |
||
125 | */ |
||
126 | private $borderLeftWidth; |
||
127 | |||
128 | /** |
||
129 | * @var string |
||
130 | */ |
||
131 | private $borderColour = 'white'; |
||
132 | |||
133 | /** |
||
134 | * @var array |
||
135 | */ |
||
136 | private $borderTopRows = []; |
||
137 | |||
138 | /** |
||
139 | * @var array |
||
140 | */ |
||
141 | private $borderBottomRows = []; |
||
142 | |||
143 | /** |
||
144 | * @var bool |
||
145 | */ |
||
146 | private $marginAuto = false; |
||
147 | |||
148 | /** |
||
149 | * Default Values |
||
150 | * |
||
151 | * @var array |
||
152 | */ |
||
153 | private static $defaultStyleValues = [ |
||
154 | 'fg' => 'white', |
||
155 | 'bg' => 'blue', |
||
156 | 'width' => 100, |
||
157 | 'paddingTopBottom' => 1, |
||
158 | 'paddingLeftRight' => 2, |
||
159 | 'margin' => 2, |
||
160 | 'selectedMarker' => '●', |
||
161 | 'unselectedMarker' => '○', |
||
162 | 'itemExtra' => '✔', |
||
163 | 'displaysExtra' => false, |
||
164 | 'titleSeparator' => '=', |
||
165 | 'borderTopWidth' => 0, |
||
166 | 'borderRightWidth' => 0, |
||
167 | 'borderBottomWidth' => 0, |
||
168 | 'borderLeftWidth' => 0, |
||
169 | 'borderColour' => 'white', |
||
170 | 'marginAuto' => false, |
||
171 | ]; |
||
172 | |||
173 | public static function getDefaultStyleValues() : array |
||
177 | |||
178 | /** |
||
179 | * @var array |
||
180 | */ |
||
181 | private static $availableForegroundColors = array( |
||
182 | 'black' => 30, |
||
183 | 'red' => 31, |
||
184 | 'green' => 32, |
||
185 | 'yellow' => 33, |
||
186 | 'blue' => 34, |
||
187 | 'magenta' => 35, |
||
188 | 'cyan' => 36, |
||
189 | 'white' => 37, |
||
190 | 'default' => 39, |
||
191 | ); |
||
192 | |||
193 | /** |
||
194 | * @var array |
||
195 | */ |
||
196 | private static $availableBackgroundColors = array( |
||
197 | 'black' => 40, |
||
198 | 'red' => 41, |
||
199 | 'green' => 42, |
||
200 | 'yellow' => 43, |
||
201 | 'blue' => 44, |
||
202 | 'magenta' => 45, |
||
203 | 'cyan' => 46, |
||
204 | 'white' => 47, |
||
205 | 'default' => 49, |
||
206 | ); |
||
207 | |||
208 | /** |
||
209 | * @var array |
||
210 | */ |
||
211 | private static $availableOptions = array( |
||
212 | 'bold' => array('set' => 1, 'unset' => 22), |
||
213 | 'dim' => array('set' => 2, 'unset' => 22), |
||
214 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
215 | 'blink' => array('set' => 5, 'unset' => 25), |
||
216 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
217 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
218 | ); |
||
219 | |||
220 | /** |
||
221 | * Initialise style |
||
222 | */ |
||
223 | public function __construct(Terminal $terminal = null) |
||
247 | |||
248 | public function getDisabledItemText(string $text) : string |
||
257 | |||
258 | /** |
||
259 | * Generates the ansi escape sequence to set the colours |
||
260 | */ |
||
261 | private function generateColoursSetCode() : void |
||
277 | |||
278 | /** |
||
279 | * Get the colour code for Bg and Fg |
||
280 | */ |
||
281 | public function getColoursSetCode() : string |
||
285 | |||
286 | /** |
||
287 | * Get the inverted escape sequence (used for selected elements) |
||
288 | */ |
||
289 | public function getInvertedColoursSetCode() : string |
||
293 | |||
294 | /** |
||
295 | * Get the inverted escape sequence (used for selected elements) |
||
296 | */ |
||
297 | public function getInvertedColoursUnsetCode() : string |
||
301 | |||
302 | /** |
||
303 | * Get the escape sequence used to reset colours to default |
||
304 | */ |
||
305 | public function getColoursResetCode() : string |
||
309 | |||
310 | /** |
||
311 | * Calculate the contents width |
||
312 | */ |
||
313 | protected function calculateContentWidth() : void |
||
323 | |||
324 | public function getFg() |
||
328 | |||
329 | View Code Duplication | public function setFg(string $fg, string $fallback = null) : self |
|
340 | |||
341 | public function getBg() |
||
345 | |||
346 | public function setBg(string $bg, string $fallback = null) : self |
||
359 | |||
360 | public function getWidth() : int |
||
364 | |||
365 | public function setWidth(int $width) : self |
||
384 | |||
385 | public function getPaddingTopBottom() : int |
||
389 | |||
390 | public function getPaddingLeftRight() : int |
||
394 | |||
395 | private function generatePaddingTopBottomRows() : void |
||
419 | |||
420 | public function getPaddingTopBottomRows() : array |
||
424 | |||
425 | View Code Duplication | public function setPadding(int $topBottom, int $leftRight = null) : self |
|
439 | |||
440 | public function setPaddingTopBottom(int $topBottom) : self |
||
449 | |||
450 | public function setPaddingLeftRight(int $leftRight) : self |
||
460 | |||
461 | public function getMargin() : int |
||
465 | |||
466 | public function setMarginAuto() : self |
||
476 | |||
477 | public function setMargin(int $margin) : self |
||
489 | |||
490 | public function getContentWidth() : int |
||
494 | |||
495 | /** |
||
496 | * Get padding for right had side of content |
||
497 | */ |
||
498 | public function getRightHandPadding(int $contentLength) : int |
||
508 | |||
509 | public function getSelectedMarker() : string |
||
513 | |||
514 | public function setSelectedMarker(string $marker) : self |
||
520 | |||
521 | public function getUnselectedMarker() : string |
||
525 | |||
526 | public function setUnselectedMarker(string $marker) : self |
||
532 | |||
533 | /** |
||
534 | * Get the correct marker for the item |
||
535 | */ |
||
536 | public function getMarker(bool $selected) : string |
||
540 | |||
541 | public function setItemExtra(string $itemExtra) : self |
||
547 | |||
548 | public function getItemExtra() : string |
||
552 | |||
553 | public function getDisplaysExtra() : bool |
||
557 | |||
558 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
564 | |||
565 | public function getTitleSeparator() : string |
||
569 | |||
570 | public function setTitleSeparator(string $actionSeparator) : self |
||
576 | |||
577 | private function generateBorderRows() : void |
||
590 | |||
591 | public function getBorderTopRows() : array |
||
595 | |||
596 | public function getBorderBottomRows() : array |
||
600 | |||
601 | /** |
||
602 | * Shorthand function to set all borders values at once |
||
603 | */ |
||
604 | public function setBorder( |
||
640 | |||
641 | public function setBorderTopWidth(int $width) : self |
||
649 | |||
650 | public function setBorderRightWidth(int $width) : self |
||
659 | |||
660 | public function setBorderBottomWidth(int $width) : self |
||
668 | |||
669 | public function setBorderLeftWidth(int $width) : self |
||
678 | |||
679 | public function setBorderColour(string $colour, $fallback = null) : self |
||
692 | |||
693 | public function getBorderTopWidth() : int |
||
697 | |||
698 | public function getBorderRightWidth() : int |
||
702 | |||
703 | public function getBorderBottomWidth() : int |
||
707 | |||
708 | public function getBorderLeftWidth() : int |
||
712 | |||
713 | public function getBorderColour() : string |
||
717 | |||
718 | public function getBorderColourCode() : string |
||
728 | } |
||
729 |
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: