Complex classes like Profile 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 Profile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class Profile |
||
13 | { |
||
14 | /** |
||
15 | * Column count |
||
16 | * |
||
17 | * @var int |
||
18 | */ |
||
19 | private $columns; |
||
20 | |||
21 | /** |
||
22 | * Model capabilities |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $capabilities; |
||
27 | |||
28 | /** |
||
29 | * Connection or output buffer |
||
30 | * |
||
31 | * @var \Thermal\Connection\Connection |
||
32 | */ |
||
33 | private $connection; |
||
34 | |||
35 | /** |
||
36 | * Font name A, C or C |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $font; |
||
41 | |||
42 | /** |
||
43 | * Printer default font name |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $default_font; |
||
48 | |||
49 | /** |
||
50 | * Encoding |
||
51 | * |
||
52 | * @var \Thermal\Buffer\Encoding |
||
53 | */ |
||
54 | private $encoding; |
||
55 | |||
56 | /** |
||
57 | * Profile constructor |
||
58 | * |
||
59 | * @param array $capabilities |
||
60 | */ |
||
61 | 52 | public function __construct($capabilities) |
|
70 | |||
71 | 13 | public function getName() |
|
76 | |||
77 | /** |
||
78 | * Printer encoding |
||
79 | * |
||
80 | * @return \Thermal\Buffer\Encoding |
||
81 | */ |
||
82 | 19 | public function getEncoding() |
|
86 | |||
87 | /** |
||
88 | * Default column count |
||
89 | * |
||
90 | * @return int |
||
91 | */ |
||
92 | 52 | public function getDefaultColumns() |
|
96 | |||
97 | /** |
||
98 | * Column count for printing |
||
99 | * |
||
100 | * @return int |
||
101 | */ |
||
102 | 4 | public function getColumns() |
|
106 | |||
107 | 6 | public function setColumns($columns) |
|
121 | |||
122 | 52 | public function getDefaultFont() |
|
126 | |||
127 | 52 | private function findDefaultFont() |
|
143 | |||
144 | 3 | public function getCodePages() |
|
148 | |||
149 | 52 | public function getDefaultCodePage() |
|
153 | |||
154 | 14 | protected function checkCodePage($codepage) |
|
155 | { |
||
156 | 14 | if (!isset($this->capabilities['codepages'][$codepage])) { |
|
157 | 1 | throw new \Exception( |
|
158 | 1 | sprintf( |
|
159 | 1 | 'Codepage "%s" not supported for printer "%s"', |
|
160 | $codepage, |
||
161 | 1 | $this->getName() |
|
162 | ), |
||
163 | 1 | 401 |
|
164 | ); |
||
165 | } |
||
166 | 14 | return $this; |
|
167 | } |
||
168 | |||
169 | 14 | public function setCodePage($codepage) |
|
175 | |||
176 | 6 | public function getFont() |
|
180 | |||
181 | 8 | public function setFont($font) |
|
208 | |||
209 | 14 | protected function refresh() |
|
215 | |||
216 | 6 | protected function fontChanged($new_font, $old_font) |
|
219 | |||
220 | /** |
||
221 | * Current connection |
||
222 | * |
||
223 | * @return \Thermal\Connection\Connection |
||
224 | */ |
||
225 | 39 | public function getConnection() |
|
232 | |||
233 | /** |
||
234 | * Set current connection |
||
235 | * |
||
236 | * @param \Thermal\Connection\Connection $connection |
||
237 | * @return self |
||
238 | */ |
||
239 | 47 | public function setConnection($connection) |
|
244 | |||
245 | 12 | public function initialize() |
|
253 | |||
254 | 1 | public function finalize() |
|
257 | |||
258 | 7 | public function write($text, $styles) |
|
273 | |||
274 | 3 | public function writeln($text, $styles, $align) |
|
289 | |||
290 | 3 | protected function getBitmapCmd() |
|
294 | |||
295 | 4 | public function draw($image) |
|
308 | |||
309 | /** |
||
310 | * Draw QR Code |
||
311 | * |
||
312 | * @param string $data data to encode |
||
313 | * @param int $size size of QR Code |
||
314 | * @return void |
||
315 | */ |
||
316 | abstract public function qrcode($data, $size); |
||
317 | |||
318 | /** |
||
319 | * Draw QR Code generating an image and sending to printer |
||
320 | * |
||
321 | * @param string $data data to encode |
||
322 | * @param int $size size of QR Code |
||
323 | * @return void |
||
324 | */ |
||
325 | 2 | protected function drawQrcode($data, $size) |
|
338 | |||
339 | abstract public function feed($lines); |
||
342 | |||
343 | /** |
||
344 | * @param int $number drawer id |
||
345 | * @param int $on_time time in milliseconds that activate the drawer |
||
346 | * @param int $off_time time in milliseconds that deactivate the drawer |
||
347 | */ |
||
348 | abstract public function drawer($number, $on_time, $off_time); |
||
349 | |||
350 | abstract public function setAlignment($align); |
||
353 | } |
||
354 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.