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 |
||
14 | class MenuStyle |
||
15 | { |
||
16 | /** |
||
17 | * @var Terminal |
||
18 | */ |
||
19 | protected $terminal; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $fg; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $bg; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $width; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $padding; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $margin; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $contentWidth; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $selectedMarker; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $unselectedMarker; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $itemExtra; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $displaysExtra; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | private $titleSeparator; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | private $coloursSetCode; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | private $invertedColoursSetCode = "\033[7m"; |
||
85 | |||
86 | /** |
||
87 | * @var string |
||
88 | */ |
||
89 | private $coloursResetCode = "\033[0m"; |
||
90 | |||
91 | /** |
||
92 | * Default Values |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | private static $defaultStyleValues = [ |
||
97 | 'fg' => 'white', |
||
98 | 'bg' => 'blue', |
||
99 | 'width' => 100, |
||
100 | 'padding' => 2, |
||
101 | 'margin' => 2, |
||
102 | 'selectedMarker' => '●', |
||
103 | 'unselectedMarker' => '○', |
||
104 | 'itemExtra' => '✔', |
||
105 | 'displaysExtra' => false, |
||
106 | 'titleSeparator' => '=', |
||
107 | ]; |
||
108 | |||
109 | public static function getDefaultStyleValues() : array |
||
110 | { |
||
111 | return static::$defaultStyleValues; |
||
|
|||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @var array |
||
116 | */ |
||
117 | private static $availableForegroundColors = array( |
||
118 | 'black' => 30, |
||
119 | 'red' => 31, |
||
120 | 'green' => 32, |
||
121 | 'yellow' => 33, |
||
122 | 'blue' => 34, |
||
123 | 'magenta' => 35, |
||
124 | 'cyan' => 36, |
||
125 | 'white' => 37, |
||
126 | 'default' => 39, |
||
127 | ); |
||
128 | |||
129 | /** |
||
130 | * @var array |
||
131 | */ |
||
132 | private static $availableBackgroundColors = array( |
||
133 | 'black' => 40, |
||
134 | 'red' => 41, |
||
135 | 'green' => 42, |
||
136 | 'yellow' => 43, |
||
137 | 'blue' => 44, |
||
138 | 'magenta' => 45, |
||
139 | 'cyan' => 46, |
||
140 | 'white' => 47, |
||
141 | 'default' => 49, |
||
142 | ); |
||
143 | |||
144 | /** |
||
145 | * @var array |
||
146 | */ |
||
147 | private static $availableOptions = array( |
||
148 | 'bold' => array('set' => 1, 'unset' => 22), |
||
149 | 'dim' => array('set' => 2, 'unset' => 22), |
||
150 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
151 | 'blink' => array('set' => 5, 'unset' => 25), |
||
152 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
153 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
154 | ); |
||
155 | |||
156 | /** |
||
157 | * Initialise style |
||
158 | */ |
||
159 | public function __construct(Terminal $terminal = null) |
||
160 | { |
||
161 | $this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
||
162 | |||
163 | $this->setFg(static::$defaultStyleValues['fg']); |
||
164 | $this->setBg(static::$defaultStyleValues['bg']); |
||
165 | $this->setWidth(static::$defaultStyleValues['width']); |
||
166 | $this->setPadding(static::$defaultStyleValues['padding']); |
||
167 | $this->setMargin(static::$defaultStyleValues['margin']); |
||
168 | $this->setSelectedMarker(static::$defaultStyleValues['selectedMarker']); |
||
169 | $this->setUnselectedMarker(static::$defaultStyleValues['unselectedMarker']); |
||
170 | $this->setItemExtra(static::$defaultStyleValues['itemExtra']); |
||
171 | $this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']); |
||
172 | $this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']); |
||
173 | } |
||
174 | |||
175 | public static function getAvailableColours() : array |
||
176 | { |
||
177 | return array_keys(self::$availableBackgroundColors); |
||
178 | } |
||
179 | |||
180 | public function getDisabledItemText(string $text) : string |
||
189 | |||
190 | /** |
||
191 | * Generates the ansi escape sequence to set the colours |
||
192 | */ |
||
193 | private function generateColoursSetCode() : void |
||
209 | |||
210 | /** |
||
211 | * Get the colour code for Bg and Fg |
||
212 | */ |
||
213 | public function getColoursSetCode() : string |
||
217 | |||
218 | /** |
||
219 | * Get the inverted escape sequence (used for selected elements) |
||
220 | */ |
||
221 | public function getInvertedColoursSetCode() : string |
||
225 | |||
226 | /** |
||
227 | * Get the escape sequence used to reset colours to default |
||
228 | */ |
||
229 | public function getColoursResetCode() : string |
||
233 | |||
234 | /** |
||
235 | * Calculate the contents width |
||
236 | */ |
||
237 | protected function calculateContentWidth() : void |
||
241 | |||
242 | public function getFg() : string |
||
246 | |||
247 | View Code Duplication | public function setFg($fg) : self |
|
263 | |||
264 | public function getBg() : string |
||
268 | |||
269 | View Code Duplication | public function setBg($bg) : self |
|
285 | |||
286 | public function getWidth() : int |
||
290 | |||
291 | public function setWidth(int $width) : self |
||
304 | |||
305 | public function getPadding() : int |
||
309 | |||
310 | public function setPadding(int $padding) : self |
||
318 | |||
319 | public function getMargin() : int |
||
323 | |||
324 | public function setMargin(int $margin) : self |
||
332 | |||
333 | public function getContentWidth() : int |
||
337 | |||
338 | /** |
||
339 | * Get padding for right had side of content |
||
340 | */ |
||
341 | public function getRightHandPadding(int $contentLength) : int |
||
345 | |||
346 | public function getSelectedMarker() : string |
||
350 | |||
351 | public function setSelectedMarker(string $marker) : self |
||
357 | |||
358 | public function getUnselectedMarker() : string |
||
362 | |||
363 | public function setUnselectedMarker(string $marker) : self |
||
369 | |||
370 | /** |
||
371 | * Get the correct marker for the item |
||
372 | */ |
||
373 | public function getMarker(bool $selected) : string |
||
377 | |||
378 | public function setItemExtra(string $itemExtra) : self |
||
384 | |||
385 | public function getItemExtra() : string |
||
389 | |||
390 | public function getDisplaysExtra() : bool |
||
394 | |||
395 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
401 | |||
402 | public function getTitleSeparator() : string |
||
406 | |||
407 | public function setTitleSeparator(string $actionSeparator) : self |
||
413 | } |
||
414 |
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: