Complex classes like Color 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 Color, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Color |
||
6 | { |
||
7 | const FORMAT_PATTERN = '#<([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)>(.*?)</\\1?>#s'; |
||
8 | /** @link http://www.php.net/manual/en/functions.user-defined.php */ |
||
9 | const STYLE_NAME_PATTERN = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'; |
||
10 | |||
11 | const ESC = "\033["; |
||
12 | const ESC_SEQ_PATTERN = "\033[%sm"; |
||
13 | |||
14 | protected $initial = ''; |
||
15 | protected $wrapped = ''; |
||
16 | // italic and blink may not work depending of your terminal |
||
17 | protected $styles = array( |
||
18 | 'reset' => '0', |
||
19 | 'bold' => '1', |
||
20 | 'dark' => '2', |
||
21 | 'italic' => '3', |
||
22 | 'underline' => '4', |
||
23 | 'blink' => '5', |
||
24 | 'reverse' => '7', |
||
25 | 'concealed' => '8', |
||
26 | |||
27 | 'default' => '39', |
||
28 | 'black' => '30', |
||
29 | 'red' => '31', |
||
30 | 'green' => '32', |
||
31 | 'yellow' => '33', |
||
32 | 'blue' => '34', |
||
33 | 'magenta' => '35', |
||
34 | 'cyan' => '36', |
||
35 | 'light_gray' => '37', |
||
36 | |||
37 | 'dark_gray' => '90', |
||
38 | 'light_red' => '91', |
||
39 | 'light_green' => '92', |
||
40 | 'light_yellow' => '93', |
||
41 | 'light_blue' => '94', |
||
42 | 'light_magenta' => '95', |
||
43 | 'light_cyan' => '96', |
||
44 | 'white' => '97', |
||
45 | |||
46 | 'bg_default' => '49', |
||
47 | 'bg_black' => '40', |
||
48 | 'bg_red' => '41', |
||
49 | 'bg_green' => '42', |
||
50 | 'bg_yellow' => '43', |
||
51 | 'bg_blue' => '44', |
||
52 | 'bg_magenta' => '45', |
||
53 | 'bg_cyan' => '46', |
||
54 | 'bg_light_gray' => '47', |
||
55 | |||
56 | 'bg_dark_gray' => '100', |
||
57 | 'bg_light_red' => '101', |
||
58 | 'bg_light_green' => '102', |
||
59 | 'bg_light_yellow' => '103', |
||
60 | 'bg_light_blue' => '104', |
||
61 | 'bg_light_magenta' => '105', |
||
62 | 'bg_light_cyan' => '106', |
||
63 | 'bg_white' => '107', |
||
64 | ); |
||
65 | protected $userStyles = array(); |
||
66 | protected $isStyleForced = false; |
||
67 | |||
68 | public function __construct($string = '') |
||
72 | |||
73 | public function __invoke($string) |
||
77 | |||
78 | public function __call($method, $args) |
||
86 | |||
87 | public function __get($name) |
||
91 | |||
92 | public function __toString() |
||
96 | |||
97 | public function setForceStyle($force) |
||
101 | |||
102 | public function isStyleForced() |
||
106 | |||
107 | /** |
||
108 | * Returns true if the stream supports colorization. |
||
109 | * |
||
110 | * Colorization is disabled if not supported by the stream: |
||
111 | * |
||
112 | * - Windows without Ansicon, ConEmu or Babun |
||
113 | * - non tty consoles |
||
114 | * |
||
115 | * @return bool true if the stream supports colorization, false otherwise |
||
116 | * |
||
117 | * @link https://github.com/symfony/Console/blob/master/Output/StreamOutput.php#L94 |
||
118 | * @codeCoverageIgnore |
||
119 | */ |
||
120 | public function isSupported() |
||
142 | |||
143 | /** |
||
144 | * @codeCoverageIgnore |
||
145 | */ |
||
146 | public function are256ColorsSupported() |
||
150 | |||
151 | protected function setInternalState($string) |
||
156 | |||
157 | protected function stylize($style, $text) |
||
180 | |||
181 | protected function shouldStylize() |
||
185 | |||
186 | protected function isStyleExists($style) |
||
190 | |||
191 | protected function applyStyle($style, $text) |
||
195 | |||
196 | protected function buildEscSeq($style) |
||
200 | |||
201 | protected function isUserStyleExists($style) |
||
205 | |||
206 | protected function applyUserStyle($userStyle, $text) |
||
216 | |||
217 | public function apply($style, $text = null) |
||
226 | |||
227 | public function fg($color, $text = null) |
||
231 | |||
232 | public function bg($color, $text = null) |
||
236 | |||
237 | public function highlight($color, $text = null) |
||
241 | |||
242 | public function reset() |
||
247 | |||
248 | public function center($width = 80, $text = null) |
||
264 | |||
265 | protected function stripColors($text) |
||
269 | |||
270 | public function clean($text = null) |
||
279 | |||
280 | public function strip($text = null) |
||
284 | |||
285 | public function isAValidStyleName($name) |
||
289 | |||
290 | /** |
||
291 | * @deprecated |
||
292 | * @codeCoverageIgnore |
||
293 | */ |
||
294 | public function setTheme(array $theme) |
||
298 | |||
299 | public function setUserStyles(array $userStyles) |
||
314 | |||
315 | protected function colorizeText($text) |
||
319 | |||
320 | public function colorize($text = null) |
||
329 | |||
330 | protected function replaceStyle($matches) |
||
334 | } |
||
335 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.