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 | /** |
||
174 | * @var array |
||
175 | */ |
||
176 | private static $availableForegroundColors = array( |
||
177 | 'black' => 30, |
||
178 | 'red' => 31, |
||
179 | 'green' => 32, |
||
180 | 'yellow' => 33, |
||
181 | 'blue' => 34, |
||
182 | 'magenta' => 35, |
||
183 | 'cyan' => 36, |
||
184 | 'white' => 37, |
||
185 | 'default' => 39, |
||
186 | ); |
||
187 | |||
188 | /** |
||
189 | * @var array |
||
190 | */ |
||
191 | private static $availableBackgroundColors = array( |
||
192 | 'black' => 40, |
||
193 | 'red' => 41, |
||
194 | 'green' => 42, |
||
195 | 'yellow' => 43, |
||
196 | 'blue' => 44, |
||
197 | 'magenta' => 45, |
||
198 | 'cyan' => 46, |
||
199 | 'white' => 47, |
||
200 | 'default' => 49, |
||
201 | ); |
||
202 | |||
203 | /** |
||
204 | * @var array |
||
205 | */ |
||
206 | private static $availableOptions = array( |
||
207 | 'bold' => array('set' => 1, 'unset' => 22), |
||
208 | 'dim' => array('set' => 2, 'unset' => 22), |
||
209 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
210 | 'blink' => array('set' => 5, 'unset' => 25), |
||
211 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
212 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
213 | ); |
||
214 | |||
215 | /** |
||
216 | * Initialise style |
||
217 | */ |
||
218 | public function __construct(Terminal $terminal = null) |
||
242 | |||
243 | public function hasChangedFromDefaults() : bool |
||
267 | |||
268 | public function getDisabledItemText(string $text) : string |
||
277 | |||
278 | /** |
||
279 | * Generates the ansi escape sequence to set the colours |
||
280 | */ |
||
281 | private function generateColoursSetCode() : void |
||
297 | |||
298 | /** |
||
299 | * Get the colour code for Bg and Fg |
||
300 | */ |
||
301 | public function getColoursSetCode() : string |
||
305 | |||
306 | /** |
||
307 | * Get the inverted escape sequence (used for selected elements) |
||
308 | */ |
||
309 | public function getInvertedColoursSetCode() : string |
||
313 | |||
314 | /** |
||
315 | * Get the inverted escape sequence (used for selected elements) |
||
316 | */ |
||
317 | public function getInvertedColoursUnsetCode() : string |
||
321 | |||
322 | /** |
||
323 | * Get the escape sequence used to reset colours to default |
||
324 | */ |
||
325 | public function getColoursResetCode() : string |
||
329 | |||
330 | /** |
||
331 | * Calculate the contents width |
||
332 | */ |
||
333 | protected function calculateContentWidth() : void |
||
343 | |||
344 | public function getFg() |
||
348 | |||
349 | public function setFg(string $fg, string $fallback = null) : self |
||
360 | |||
361 | public function getBg() |
||
365 | |||
366 | public function setBg(string $bg, string $fallback = null) : self |
||
379 | |||
380 | public function getWidth() : int |
||
384 | |||
385 | public function setWidth(int $width) : self |
||
404 | |||
405 | public function getPaddingTopBottom() : int |
||
409 | |||
410 | public function getPaddingLeftRight() : int |
||
414 | |||
415 | private function generatePaddingTopBottomRows() : void |
||
439 | |||
440 | public function getPaddingTopBottomRows() : array |
||
444 | |||
445 | public function setPadding(int $topBottom, int $leftRight = null) : self |
||
459 | |||
460 | public function setPaddingTopBottom(int $topBottom) : self |
||
469 | |||
470 | public function setPaddingLeftRight(int $leftRight) : self |
||
480 | |||
481 | public function getMargin() : int |
||
485 | |||
486 | public function setMarginAuto() : self |
||
496 | |||
497 | public function setMargin(int $margin) : self |
||
509 | |||
510 | public function getContentWidth() : int |
||
514 | |||
515 | /** |
||
516 | * Get padding for right had side of content |
||
517 | */ |
||
518 | public function getRightHandPadding(int $contentLength) : int |
||
528 | |||
529 | public function getSelectedMarker() : string |
||
533 | |||
534 | public function setSelectedMarker(string $marker) : self |
||
540 | |||
541 | public function getUnselectedMarker() : string |
||
545 | |||
546 | public function setUnselectedMarker(string $marker) : self |
||
552 | |||
553 | /** |
||
554 | * Get the correct marker for the item |
||
555 | */ |
||
556 | public function getMarker(bool $selected) : string |
||
560 | |||
561 | public function setItemExtra(string $itemExtra) : self |
||
567 | |||
568 | public function getItemExtra() : string |
||
572 | |||
573 | public function getDisplaysExtra() : bool |
||
577 | |||
578 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
584 | |||
585 | public function getTitleSeparator() : string |
||
589 | |||
590 | public function setTitleSeparator(string $actionSeparator) : self |
||
596 | |||
597 | private function generateBorderRows() : void |
||
610 | |||
611 | public function getBorderTopRows() : array |
||
615 | |||
616 | public function getBorderBottomRows() : array |
||
620 | |||
621 | /** |
||
622 | * Shorthand function to set all borders values at once |
||
623 | */ |
||
624 | public function setBorder( |
||
660 | |||
661 | public function setBorderTopWidth(int $width) : self |
||
669 | |||
670 | public function setBorderRightWidth(int $width) : self |
||
679 | |||
680 | public function setBorderBottomWidth(int $width) : self |
||
688 | |||
689 | public function setBorderLeftWidth(int $width) : self |
||
698 | |||
699 | public function setBorderColour(string $colour, $fallback = null) : self |
||
712 | |||
713 | public function getBorderTopWidth() : int |
||
717 | |||
718 | public function getBorderRightWidth() : int |
||
722 | |||
723 | public function getBorderBottomWidth() : int |
||
727 | |||
728 | public function getBorderLeftWidth() : int |
||
732 | |||
733 | public function getBorderColour() : string |
||
737 | |||
738 | public function getBorderColourCode() : string |
||
748 | } |
||
749 |
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: