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 int |
||
78 | */ |
||
79 | private $borderTopWidth; |
||
80 | |||
81 | /** |
||
82 | * @var int |
||
83 | */ |
||
84 | private $borderRightWidth; |
||
85 | |||
86 | /** |
||
87 | * @var int |
||
88 | */ |
||
89 | private $borderBottomWidth; |
||
90 | |||
91 | /** |
||
92 | * @var int |
||
93 | */ |
||
94 | private $borderLeftWidth; |
||
95 | |||
96 | /** |
||
97 | * @var string |
||
98 | */ |
||
99 | private $borderColour; |
||
100 | |||
101 | /** |
||
102 | * Default Values |
||
103 | * |
||
104 | * @var array |
||
105 | */ |
||
106 | private static $defaultStyleValues = [ |
||
107 | 'fg' => 'white', |
||
108 | 'bg' => 'blue', |
||
109 | 'width' => 100, |
||
110 | 'padding' => 2, |
||
111 | 'margin' => 2, |
||
112 | 'selectedMarker' => '●', |
||
113 | 'unselectedMarker' => '○', |
||
114 | 'itemExtra' => '✔', |
||
115 | 'displaysExtra' => false, |
||
116 | 'titleSeparator' => '=', |
||
117 | 'borderTopWidth' => 0, |
||
118 | 'borderRightWidth' => 0, |
||
119 | 'borderBottomWidth' => 0, |
||
120 | 'borderLeftWidth' => 0, |
||
121 | 'borderColour' => 'white', |
||
122 | ]; |
||
123 | |||
124 | public static function getDefaultStyleValues() : array |
||
125 | { |
||
126 | return static::$defaultStyleValues; |
||
|
|||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @var array |
||
131 | */ |
||
132 | private static $availableForegroundColors = array( |
||
133 | 'black' => array('set' => 30, 'unset' => 39), |
||
134 | 'red' => array('set' => 31, 'unset' => 39), |
||
135 | 'green' => array('set' => 32, 'unset' => 39), |
||
136 | 'yellow' => array('set' => 33, 'unset' => 39), |
||
137 | 'blue' => array('set' => 34, 'unset' => 39), |
||
138 | 'magenta' => array('set' => 35, 'unset' => 39), |
||
139 | 'cyan' => array('set' => 36, 'unset' => 39), |
||
140 | 'white' => array('set' => 37, 'unset' => 39), |
||
141 | 'default' => array('set' => 39, 'unset' => 39), |
||
142 | ); |
||
143 | |||
144 | /** |
||
145 | * @var array |
||
146 | */ |
||
147 | private static $availableBackgroundColors = array( |
||
148 | 'black' => array('set' => 40, 'unset' => 49), |
||
149 | 'red' => array('set' => 41, 'unset' => 49), |
||
150 | 'green' => array('set' => 42, 'unset' => 49), |
||
151 | 'yellow' => array('set' => 43, 'unset' => 49), |
||
152 | 'blue' => array('set' => 44, 'unset' => 49), |
||
153 | 'magenta' => array('set' => 45, 'unset' => 49), |
||
154 | 'cyan' => array('set' => 46, 'unset' => 49), |
||
155 | 'white' => array('set' => 47, 'unset' => 49), |
||
156 | 'default' => array('set' => 49, 'unset' => 49), |
||
157 | ); |
||
158 | |||
159 | /** |
||
160 | * @var array |
||
161 | */ |
||
162 | private static $availableOptions = array( |
||
163 | 'bold' => array('set' => 1, 'unset' => 22), |
||
164 | 'dim' => array('set' => 2, 'unset' => 22), |
||
165 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
166 | 'blink' => array('set' => 5, 'unset' => 25), |
||
167 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
168 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
169 | ); |
||
170 | |||
171 | /** |
||
172 | * Initialise style |
||
173 | */ |
||
174 | public function __construct(Terminal $terminal = null) |
||
175 | { |
||
176 | $this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
||
177 | |||
178 | $this->setFg(static::$defaultStyleValues['fg']); |
||
179 | $this->setBg(static::$defaultStyleValues['bg']); |
||
180 | $this->setWidth(static::$defaultStyleValues['width']); |
||
181 | $this->setPadding(static::$defaultStyleValues['padding']); |
||
182 | $this->setMargin(static::$defaultStyleValues['margin']); |
||
183 | $this->setSelectedMarker(static::$defaultStyleValues['selectedMarker']); |
||
184 | $this->setUnselectedMarker(static::$defaultStyleValues['unselectedMarker']); |
||
185 | $this->setItemExtra(static::$defaultStyleValues['itemExtra']); |
||
186 | $this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']); |
||
187 | $this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']); |
||
188 | $this->setBorderTopWidth(static::$defaultStyleValues['borderTopWidth']); |
||
189 | $this->setBorderRightWidth(static::$defaultStyleValues['borderRightWidth']); |
||
190 | $this->setBorderBottomWidth(static::$defaultStyleValues['borderBottomWidth']); |
||
191 | $this->setBorderLeftWidth(static::$defaultStyleValues['borderLeftWidth']); |
||
192 | $this->setBorderColour(static::$defaultStyleValues['borderColour']); |
||
193 | |||
194 | } |
||
195 | |||
196 | public static function getAvailableColours() : array |
||
197 | { |
||
198 | return array_keys(self::$availableBackgroundColors); |
||
199 | } |
||
200 | |||
201 | public function getDisabledItemText(string $text) : string |
||
202 | { |
||
203 | return sprintf( |
||
204 | "\033[%sm%s\033[%sm", |
||
205 | self::$availableOptions['dim']['set'], |
||
206 | $text, |
||
207 | self::$availableOptions['dim']['unset'] |
||
208 | ); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Get the colour code set for Bg and Fg |
||
213 | */ |
||
214 | View Code Duplication | public function getSelectedSetCode() : string |
|
215 | { |
||
216 | return sprintf( |
||
217 | "\033[%sm", |
||
218 | implode(';', [ |
||
219 | self::$availableBackgroundColors[$this->getFg()]['set'], |
||
220 | self::$availableForegroundColors[$this->getBg()]['set'], |
||
221 | ]) |
||
222 | ); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Get the colour unset code for Bg and Fg |
||
227 | */ |
||
228 | View Code Duplication | public function getSelectedUnsetCode() : string |
|
229 | { |
||
230 | return sprintf( |
||
231 | "\033[%sm", |
||
232 | implode(';', [ |
||
233 | self::$availableBackgroundColors[$this->getBg()]['unset'], |
||
234 | self::$availableForegroundColors[$this->getFg()]['unset'], |
||
235 | ]) |
||
236 | ); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Get the inverted colour code |
||
241 | */ |
||
242 | View Code Duplication | public function getUnselectedSetCode() : string |
|
243 | { |
||
244 | return sprintf( |
||
245 | "\033[%sm", |
||
246 | implode(';', [ |
||
247 | self::$availableBackgroundColors[$this->getBg()]['set'], |
||
248 | self::$availableForegroundColors[$this->getFg()]['set'], |
||
249 | ]) |
||
250 | ); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Get the inverted colour unset code |
||
255 | */ |
||
256 | View Code Duplication | public function getUnselectedUnsetCode() : string |
|
257 | { |
||
258 | return sprintf( |
||
259 | "\033[%sm", |
||
260 | implode(';', [ |
||
261 | self::$availableBackgroundColors[$this->getBg()]['unset'], |
||
262 | self::$availableForegroundColors[$this->getFg()]['unset'], |
||
263 | ]) |
||
264 | ); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Calculate the contents width |
||
269 | */ |
||
270 | protected function calculateContentWidth() : void |
||
271 | { |
||
272 | $this->contentWidth = $this->width - ($this->padding*2) - ($this->margin*2) - ($this->borderRightWidth + $this->borderLeftWidth); |
||
273 | } |
||
274 | |||
275 | public function getFg() : string |
||
279 | |||
280 | public function setFg(string $fg) : self |
||
281 | { |
||
282 | $this->fg = $fg; |
||
283 | |||
284 | return $this; |
||
285 | } |
||
286 | |||
287 | public function getBg() : string |
||
288 | { |
||
289 | return $this->bg; |
||
291 | |||
292 | public function setBg(string $bg) : self |
||
298 | |||
299 | public function getWidth() : int |
||
303 | |||
304 | public function setWidth(int $width) : self |
||
317 | |||
318 | public function getPadding() : int |
||
322 | |||
323 | public function setPadding(int $padding) : self |
||
331 | |||
332 | public function getMargin() : int |
||
336 | |||
337 | public function setMargin(int $margin) : self |
||
345 | |||
346 | public function getContentWidth() : int |
||
350 | |||
351 | /** |
||
352 | * Get padding for right had side of content |
||
353 | */ |
||
354 | public function getRightHandPadding(int $contentLength) : int |
||
358 | |||
359 | public function getSelectedMarker() : string |
||
363 | |||
364 | public function setSelectedMarker(string $marker) : self |
||
370 | |||
371 | public function getUnselectedMarker() : string |
||
375 | |||
376 | public function setUnselectedMarker(string $marker) : self |
||
382 | |||
383 | /** |
||
384 | * Get the correct marker for the item |
||
385 | */ |
||
386 | public function getMarker(bool $selected) : string |
||
390 | |||
391 | public function setItemExtra(string $itemExtra) : self |
||
397 | |||
398 | public function getItemExtra() : string |
||
402 | |||
403 | public function getDisplaysExtra() : bool |
||
407 | |||
408 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
414 | |||
415 | public function getTitleSeparator() : string |
||
419 | |||
420 | public function setTitleSeparator(string $actionSeparator) : self |
||
426 | |||
427 | /** |
||
428 | * Shorthand function to set all borders values at once |
||
429 | */ |
||
430 | public function setBorder(int $topWidth, int $rightWidth = null, int $bottomWidth = null, int $leftWidth = null, string $colour = null) : self |
||
496 | } |
||
497 |
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: