Complex classes like Bematech 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 Bematech, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Bematech extends DefaultPrinter implements PrinterInterface |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * List all available code pages. |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $aCodePage = array( |
||
31 | 'CP437' => array('conv' => '437', 'table' => '3', 'desc' => 'PC437: USA, Standard Europe'), |
||
32 | 'CP850' => array('conv' => '850', 'table' => '2', 'desc' => 'PC850: Multilingual'), |
||
33 | 'CP858' => array('conv' => '858', 'table' => '5', 'desc' => 'PC858: Multilingual'), |
||
34 | 'CP860' => array('conv' => '860', 'table' => '4', 'desc' => 'PC860: Portuguese'), |
||
35 | 'CP864' => array('conv' => '864', 'table' => '7', 'desc' => 'PC864: Arabic'), |
||
36 | 'CP866' => array('conv' => '866', 'table' => '6', 'desc' => 'PC866: Cyrillic'), |
||
37 | 'UTF8' => array('conv' => 'UTF8', 'table' => '8', 'desc' => 'UTF-8: Unicode') |
||
38 | ); |
||
39 | |||
40 | /** |
||
41 | * List all available region pages. |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $aRegion = array( |
||
45 | 'LATIN' |
||
46 | ); |
||
47 | |||
48 | /** |
||
49 | * Seleted printer mode. |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $printerMode = 'ESCBEMA'; |
||
53 | |||
54 | /** |
||
55 | * List all avaiable fonts |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $aFont = array(0 => 'C', 1 => 'D'); |
||
59 | |||
60 | /** |
||
61 | * Selected internal font. |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $font = 'C'; |
||
65 | |||
66 | /** |
||
67 | * Seleted code page |
||
68 | * Defined in printer class. |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $codepage = 'CP850'; |
||
72 | |||
73 | /** |
||
74 | * Acceptable barcodes list |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $barcode1Dlist = [ |
||
78 | 'UPC_A' => 65, |
||
79 | 'UPC_E' => 66, |
||
80 | 'EAN13' => 67, |
||
81 | 'EAN8' => 68, |
||
82 | 'CODE39' => 69, |
||
83 | 'I25' => 70, |
||
84 | 'CODABAR' => 71, |
||
85 | 'CODE93' => 72, |
||
86 | 'CODE128' => 73, |
||
87 | 'ISBN' => null, |
||
88 | 'MSI' => null |
||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * List of supported models |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $modelList = [ |
||
96 | '4200TH' |
||
97 | ]; |
||
98 | |||
99 | /** |
||
100 | * Selected model |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $printerModel = '4200TH'; |
||
104 | |||
105 | //public function __construct(); vide DefaultPrinter |
||
|
|||
106 | //public function defaultCodePage(); vide DefaultPrinter |
||
107 | //public function defaultRegionPage(); vide DefaultPrinter |
||
108 | //public function defaultFont(); vide DefaultPrinter |
||
109 | //public function defaultModel(); vide DefaultPrinter |
||
110 | //public function initialize(); vide DefaultPrinter |
||
111 | |||
112 | /** |
||
113 | * Select printer mode |
||
114 | * @param string $mode |
||
115 | */ |
||
116 | public function setPrintMode($mode = 'ESCBEMA') |
||
133 | |||
134 | /** |
||
135 | * Set a codepage table in printer. |
||
136 | * |
||
137 | * @param string $codepage |
||
138 | */ |
||
139 | public function setCodePage($codepage = null) |
||
148 | |||
149 | /** |
||
150 | * Set a region page. |
||
151 | * The numeric key of array $this->aRegion is the command parameter. |
||
152 | * |
||
153 | * @param string $region |
||
154 | */ |
||
155 | public function setRegionPage($region = null) |
||
159 | |||
160 | /** |
||
161 | * Set a printer font |
||
162 | * If send a valid font name will set the printer otherelse a default font is selected |
||
163 | * @param string $font |
||
164 | */ |
||
165 | public function setFont($font = null) |
||
171 | |||
172 | /** |
||
173 | * Set emphasys mode on or off. |
||
174 | */ |
||
175 | public function setBold() |
||
188 | |||
189 | /** |
||
190 | * Set Italic mode |
||
191 | */ |
||
192 | public function setItalic() |
||
204 | |||
205 | //public function setUnderlined(); vide DefaultPrinter |
||
206 | |||
207 | /** |
||
208 | * Set or unset condensed mode. |
||
209 | */ |
||
210 | public function setCondensed() |
||
222 | |||
223 | /** |
||
224 | * Set or unset expanded mode. |
||
225 | * |
||
226 | * @param integer $size not used |
||
227 | */ |
||
228 | public function setExpanded($size = null) |
||
240 | |||
241 | //public function setAlign(); vide DefaultPrinter |
||
242 | |||
243 | /** |
||
244 | * Turns white/black reverse print On or Off for characters. |
||
245 | */ |
||
246 | public function setReverseColors() |
||
252 | |||
253 | /** |
||
254 | * Set rotate 90 degrees. |
||
255 | */ |
||
256 | public function setRotate90() |
||
262 | |||
263 | /** |
||
264 | * Set horizontal and vertical motion units |
||
265 | * $horizontal => character spacing 1/x" |
||
266 | * $vertical => line spacing 1/y". |
||
267 | */ |
||
268 | public function setSpacing($horizontal = 30, $vertical = 30) |
||
272 | |||
273 | /** |
||
274 | * Set right-side character spacing |
||
275 | * 0 ≤ n ≤ 255 => 1/x". |
||
276 | * |
||
277 | * @param int $value |
||
278 | */ |
||
279 | public function setCharSpacing($value = 3) |
||
283 | |||
284 | //public function setParagraph(); vide DefaultPrinter |
||
285 | //public function text(); vide default |
||
286 | |||
287 | /** |
||
288 | * Prints data and feeds paper n lines |
||
289 | * ESC d n Prints data and feeds paper n lines. |
||
290 | * @param int|null $lines |
||
291 | */ |
||
292 | public function lineFeed($lines = 1) |
||
303 | |||
304 | //public function dotFeed(); vide default |
||
305 | |||
306 | /** |
||
307 | * Put a image |
||
308 | * GS v0 m xL xH yL yH d1 ... dk |
||
309 | * |
||
310 | * @param string $filename |
||
311 | * @param integer $width |
||
312 | * @param integer $height |
||
313 | * @param integer $size resolution relation |
||
314 | * @throws RuntimeException |
||
315 | */ |
||
316 | public function putImage($filename = '', $width = null, $height = null, $size = 0) |
||
331 | |||
332 | /** |
||
333 | * Generate a pulse, for opening a cash drawer if one is connected. |
||
334 | * |
||
335 | * |
||
336 | * @param int $pin 0 or 1, for pin 2 or pin 5 kick-out connector respectively. |
||
337 | * @param int $on_ms pulse ON time, in milliseconds. |
||
338 | * @param int $off_ms pulse OFF time, in milliseconds. |
||
339 | */ |
||
340 | public function pulse($pin = 0, $on_ms = 120, $off_ms = 240) |
||
353 | |||
354 | /** |
||
355 | * Cut the paper. |
||
356 | * |
||
357 | * @param int $mode FULL or PARTIAL. If not specified, FULL will be used. |
||
358 | * @param int $lines Number of lines to feed after cut |
||
359 | */ |
||
360 | public function cut($mode = 'PARTIAL', $lines = 3) |
||
373 | |||
374 | /** |
||
375 | * Implements barcodes 1D |
||
376 | * @param string $type Default CODE128 |
||
377 | * @param int $height |
||
378 | * @param int $lineWidth |
||
379 | * @param string $txtPosition |
||
380 | * @param string $txtFont |
||
381 | * @param string $data |
||
382 | */ |
||
383 | public function barcode( |
||
445 | |||
446 | /** |
||
447 | * Print PDF 417 2D barcode |
||
448 | * @param string $data |
||
449 | * @param integer $ecc |
||
450 | * @param integer $pheight |
||
451 | * @param integer $pwidth |
||
452 | * @param integer $colunms |
||
453 | * @return boolean |
||
454 | */ |
||
455 | public function barcodePDF417($data = '', $ecc = 5, $pheight = 2, $pwidth = 2, $colunms = 3) |
||
482 | |||
483 | |||
484 | /** |
||
485 | * Imprime o QR Code |
||
486 | * |
||
487 | * @param string $data Dados a serem inseridos no QRCode |
||
488 | * @param string $level Nivel de correção L,M,Q ou H |
||
489 | * @param int $modelo modelo de QRCode 0 QRCode ou 1 microQR |
||
490 | * @param int $wmod largura da barra 3 ~ 16 |
||
491 | */ |
||
492 | public function barcodeQRCode($data = '', $level = 'M', $modelo = 0, $wmod = 4) |
||
577 | } |
||
578 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.