Complex classes like PhpSerial 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 PhpSerial, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class PhpSerial |
||
26 | { |
||
27 | const OS_UNKNOWN = 0; |
||
28 | const OS_WIN = 1; //WINS32 WINNT Windows |
||
29 | const OS_LINUX = 2; |
||
30 | const OS_CYGWIN = 3; //Cygwin Windows Linux like commands |
||
31 | const OS_UNIX = 4; |
||
32 | const OS_BSD = 5; //FreeBSD or NetBSD or OpenBSD /dev/ttyu1 |
||
33 | const OS_OSX = 6; //Darwin MacOS |
||
34 | const OS_HPUX = 7; //tty1p0 |
||
35 | |||
36 | const SERIAL_DEVICE_NOTSET = 0; |
||
37 | const SERIAL_DEVICE_SET = 1; |
||
38 | const SERIAL_DEVICE_OPENED = 2; |
||
39 | |||
40 | const PARITY_NONE = 0; |
||
41 | const PARITY_ODD = 1; |
||
42 | const PARITY_EVEN = 2; |
||
43 | |||
44 | const FLOW_NONE = 0; //no flow control |
||
45 | const FLOW_RTSCTS = 1; // use RTS/CTS handshaking |
||
46 | const FLOW_XONXOFF = 2; //use XON/XOFF protocol |
||
47 | |||
48 | /** |
||
49 | * Pointer for device |
||
50 | * |
||
51 | * @var resource |
||
52 | */ |
||
53 | protected $handle = null; |
||
54 | /** |
||
55 | * Data buffer |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $buffer = ""; |
||
60 | /** |
||
61 | * This var says if buffer should be flushed by write (true) or |
||
62 | * manually (false) |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | protected $autoflush = false; |
||
67 | /** |
||
68 | * Wait time after send data to serial |
||
69 | * |
||
70 | * @var float |
||
71 | */ |
||
72 | protected $waittime = 0.1; |
||
73 | /** |
||
74 | * OS type where php is running |
||
75 | * linux is default |
||
76 | * |
||
77 | * @var int |
||
78 | */ |
||
79 | protected $ostype = 2; |
||
80 | /** |
||
81 | * Mode command to set up serial port |
||
82 | * formated device mode for especific OS use |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $mode = ''; |
||
87 | /** |
||
88 | * Status of port |
||
89 | * NoSet, Set or Open |
||
90 | * |
||
91 | * @var int |
||
92 | */ |
||
93 | protected $state = self::SERIAL_DEVICE_NOTSET; |
||
94 | /** |
||
95 | * Port name |
||
96 | * |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $port = '/dev/ttyS0'; |
||
100 | /** |
||
101 | * Data bits |
||
102 | * |
||
103 | * @var int |
||
104 | */ |
||
105 | protected $databits = 8; |
||
106 | /** |
||
107 | * Baud Rate |
||
108 | * |
||
109 | * @var int |
||
110 | */ |
||
111 | protected $baudrate = 9600; |
||
112 | /** |
||
113 | * Parity |
||
114 | * |
||
115 | * @var int |
||
116 | */ |
||
117 | protected $parity = self::PARITY_NONE; |
||
118 | /** |
||
119 | * Stop Bits |
||
120 | * |
||
121 | * @var float |
||
122 | */ |
||
123 | protected $stopbits = 1; |
||
124 | /** |
||
125 | * Flow Control |
||
126 | * |
||
127 | * @var int |
||
128 | */ |
||
129 | protected $flowcontrol = self::FLOW_NONE; |
||
130 | /** |
||
131 | * Formated device name command |
||
132 | * |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $device = '/dev/ttyS0'; |
||
136 | /** |
||
137 | * Formated Data Bits command |
||
138 | * |
||
139 | * @var string |
||
140 | */ |
||
141 | protected $formatedDataBits = 'cs8'; |
||
142 | /** |
||
143 | * Formated Baud Rate command |
||
144 | * |
||
145 | * @var string |
||
146 | */ |
||
147 | protected $formatedBaudRate = '9600'; |
||
148 | /** |
||
149 | * Formated parity command |
||
150 | * |
||
151 | * @var string |
||
152 | */ |
||
153 | protected $formatedParity = '-parenb'; |
||
154 | /** |
||
155 | * Formated stop bits command |
||
156 | * |
||
157 | * @var string |
||
158 | */ |
||
159 | protected $formatedStopBits = '-cstopb'; |
||
160 | /** |
||
161 | * Formated flow control command |
||
162 | * |
||
163 | * @var string |
||
164 | */ |
||
165 | protected $formatedFlowControl = 'clocal -crtscts -ixon -ixoff'; |
||
166 | |||
167 | /** |
||
168 | * Parity data |
||
169 | * |
||
170 | * @var array |
||
171 | */ |
||
172 | private $parityargs = [ |
||
173 | "none" => [0, "-parenb"], |
||
174 | "odd" => [1, "parenb parodd"], |
||
175 | "even" => [2, "parenb -parodd"] |
||
176 | ]; |
||
177 | |||
178 | /** |
||
179 | * Basud Rate data |
||
180 | * |
||
181 | * @var array |
||
182 | */ |
||
183 | private $baudsargs = array ( |
||
184 | 110 => 11, |
||
185 | 150 => 15, |
||
186 | 300 => 30, |
||
187 | 600 => 60, |
||
188 | 1200 => 12, |
||
189 | 2400 => 24, |
||
190 | 4800 => 48, |
||
191 | 9600 => 96, |
||
192 | 19200 => 19, |
||
193 | 38400 => 38400, |
||
194 | 57600 => 57600, |
||
195 | 115200 => 115200 |
||
196 | ); |
||
197 | |||
198 | /** |
||
199 | * Constructor |
||
200 | * Set ostype parameter |
||
201 | * |
||
202 | * @param int $forceOS |
||
203 | */ |
||
204 | 15 | public function __construct($forceOS = null) |
|
205 | { |
||
206 | 15 | if (! is_null($forceOS)) { |
|
207 | if ($this->ostype !== $forceOS && ($forceOS > 0 && $forceOS < 8)) { |
||
208 | $this->ostype = $forceOS; |
||
209 | //clear params |
||
210 | $this->clearParams(); |
||
211 | } |
||
212 | } else { |
||
213 | 15 | $this->ostype = $this->getOs(); |
|
214 | } |
||
215 | 15 | } |
|
216 | |||
217 | /** |
||
218 | * Clear class params |
||
219 | * Used for testing proporses |
||
220 | */ |
||
221 | protected function clearParams() |
||
222 | { |
||
223 | $this->mode = null; |
||
224 | $this->state = null; |
||
225 | $this->port = null; |
||
226 | $this->databits = null; |
||
227 | $this->baudrate = null; |
||
228 | $this->parity = null; |
||
229 | $this->stopbits = null; |
||
230 | $this->flowcontrol = null; |
||
231 | $this->device = null; |
||
232 | $this->formatedDataBits = null; |
||
233 | $this->formatedBaudRate = null; |
||
234 | $this->formatedParity = null; |
||
235 | $this->formatedStopBits = null; |
||
236 | $this->formatedFlowControl = null; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Close port |
||
241 | */ |
||
242 | 15 | public function __destruct() |
|
243 | { |
||
244 | 15 | $this->close(); |
|
245 | 15 | } |
|
246 | |||
247 | /** |
||
248 | * Open set port |
||
249 | * |
||
250 | * @return boolean |
||
251 | */ |
||
252 | 2 | public function open() |
|
253 | { |
||
254 | 2 | if ($this->state === self::SERIAL_DEVICE_OPENED && is_resource($this->handle)) { |
|
255 | return true; |
||
256 | } |
||
257 | 2 | $timeout = 10; //seconds |
|
258 | 2 | for ($i = 0; $i < $timeout; $i++) { |
|
259 | 2 | $this->handle = @fopen($this->device, 'r+bn'); |
|
260 | 2 | if ($this->handle) { |
|
261 | 1 | break; |
|
262 | } |
||
263 | 1 | sleep(1); |
|
264 | 1 | } |
|
265 | 2 | if ($this->handle === false) { |
|
266 | 1 | $this->handle = null; |
|
267 | 1 | $this->state = self::SERIAL_DEVICE_NOTSET; |
|
268 | 1 | throw new RuntimeException('Fail to open device. Check permissions.'); |
|
269 | } |
||
270 | 1 | stream_set_blocking($this->handle, false); |
|
271 | 1 | $this->state = self::SERIAL_DEVICE_OPENED; |
|
272 | 1 | return true; |
|
273 | } |
||
274 | |||
275 | /** |
||
276 | * Close serial port |
||
277 | * |
||
278 | * @return boolean |
||
279 | */ |
||
280 | 15 | public function close() |
|
281 | { |
||
282 | 15 | if ($this->state !== self::SERIAL_DEVICE_OPENED || ! is_resource($this->handle)) { |
|
283 | 14 | return true; |
|
284 | } |
||
285 | 1 | if (fclose($this->handle)) { |
|
286 | 1 | $this->handle = null; |
|
287 | 1 | $this->state = self::SERIAL_DEVICE_SET; |
|
288 | 1 | return true; |
|
289 | } |
||
290 | return false; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Returns the setup configuration for serial port |
||
295 | * this command will be exectuted in terminal |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getSetUp() |
||
300 | { |
||
301 | return $this->mode; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Use class parameters to configure the serial port |
||
306 | * before the serial port is opened it must be configured, |
||
307 | * and in windows environment, all sets at a single time |
||
308 | * |
||
309 | * @return bool |
||
310 | */ |
||
311 | 1 | public function setUp() |
|
312 | { |
||
313 | 1 | if ($this->state === self::SERIAL_DEVICE_SET) { |
|
314 | return true; |
||
315 | } |
||
316 | 1 | if ($this->ostype == 0) { |
|
317 | return false; |
||
318 | } |
||
319 | $modesos = [ |
||
320 | 1 | 1 => 'MODE', //windows mode com4: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off |
|
321 | 1 | 2 => "stty -F", //linux |
|
322 | 1 | 3 => "stty -F", //cygwin |
|
323 | 1 | 4 => 'stty -F', //unix |
|
324 | 1 | 5 => 'stty -F', //BSD |
|
325 | 1 | 6 => "stty -f", //MacOS |
|
326 | 7 => 'stty -F' //HPUX |
||
327 | 1 | ]; |
|
328 | 1 | $mode = $modesos[$this->ostype] |
|
329 | . " " |
||
330 | 1 | . "$this->device " |
|
331 | 1 | . "$this->formatedBaudRate " |
|
332 | 1 | . "$this->formatedParity " |
|
333 | 1 | . "$this->formatedDataBits " |
|
334 | 1 | . "$this->formatedStopBits " |
|
335 | 1 | . "$this->formatedFlowControl"; |
|
336 | |||
337 | 1 | $out = ''; |
|
338 | 1 | if ($this->execCommand($mode, $out) != 0) { |
|
|
|||
339 | 1 | throw new RuntimeException("SetUP fail with: ".$out[1]); |
|
340 | } |
||
341 | $this->mode = $mode; |
||
342 | $this->state = self::SERIAL_DEVICE_SET; |
||
343 | return true; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Set automatic send massage to serial |
||
348 | * |
||
349 | * @param bool $auto |
||
350 | * @param float $waittime |
||
351 | */ |
||
352 | public function setAuto($auto, $waittime) |
||
353 | { |
||
354 | if (! is_bool($auto)) { |
||
355 | $data = false; |
||
356 | } |
||
357 | if (! is_float($waittime)) { |
||
358 | $waittime = 0.1; |
||
359 | } |
||
360 | $this->waittime = $waittime; |
||
361 | $this->autoflush = $auto; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Returns automatic mode |
||
366 | * |
||
367 | * @return bool |
||
368 | */ |
||
369 | public function getAuto() |
||
370 | { |
||
371 | return $this->autoflush; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Read serial port |
||
376 | * |
||
377 | * @param int $count Number of characters to be read (will stop before |
||
378 | * if less characters are in the buffer) |
||
379 | * @return string |
||
380 | */ |
||
381 | public function read($count = 0) |
||
382 | { |
||
383 | if ($this->state !== self::SERIAL_DEVICE_OPENED) { |
||
384 | return ''; |
||
385 | } |
||
386 | $content = ""; |
||
387 | $i = 0; |
||
388 | // Windows port reading procedures still buggy |
||
389 | // Behavior in OSX isn't to wait for new data to recover, but just |
||
390 | // grabs what's there! Doesn't always work perfectly for me in OSX |
||
391 | if ($count !== 0) { |
||
392 | do { |
||
393 | if ($i > $count) { |
||
394 | $content .= fread($this->handle, ($count - $i)); |
||
395 | } else { |
||
396 | $content .= fread($this->handle, 128); |
||
397 | } |
||
398 | } while (($i += 128) === strlen($content)); |
||
399 | return $content; |
||
400 | } |
||
401 | do { |
||
402 | $content .= fread($this->handle, 128); |
||
403 | } while (($i += 128) === strlen($content)); |
||
404 | return $content; |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Write data to buffer or serial port |
||
409 | * depends of getAuto() |
||
410 | * if getAuto() == true this command writes directly to port |
||
411 | * if getAuto() == false this command writes to buffer (default) |
||
412 | * |
||
413 | * @param string $data |
||
414 | * @return boolean |
||
415 | */ |
||
416 | public function write($data) |
||
417 | { |
||
418 | $this->buffer .= $data; |
||
419 | if ($this->autoflush === true) { |
||
420 | $this->flush(); |
||
421 | usleep((int) ($this->waittime * 1000000)); |
||
422 | } |
||
423 | return true; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Flushs imediatly data to serial port |
||
428 | * |
||
429 | * @return boolean |
||
430 | */ |
||
431 | public function flush() |
||
432 | { |
||
433 | if (fwrite($this->handle, $this->buffer) !== false) { |
||
434 | $this->buffer = ""; |
||
435 | return true; |
||
436 | } |
||
437 | return false; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Set port name |
||
442 | * |
||
443 | * @param string $port |
||
444 | */ |
||
445 | 1 | public function setPort($port) |
|
446 | { |
||
447 | //identify input if $port like COM?? even in others OS |
||
448 | 1 | $flagWinMode = preg_match("@^COM(\d+):?$@i", $port, $matches); |
|
449 | //select port from OS type |
||
450 | 1 | switch ($this->ostype) { |
|
451 | 1 | case self::OS_WIN: |
|
452 | $this->device = ($flagWinMode) ? "COM$matches[1]:" : $port; |
||
453 | break; |
||
454 | 1 | case self::OS_LINUX: |
|
455 | 1 | case self::OS_CYGWIN: |
|
456 | 1 | $this->device = ($flagWinMode) ? "/dev/ttyS".($matches[1]-1) : $port; |
|
457 | 1 | break; |
|
458 | case self::OS_UNIX: |
||
459 | case self::OS_BSD: |
||
460 | case self::OS_OSX: |
||
461 | case self::OS_HPUX: |
||
462 | default: |
||
463 | $this->device = $port; |
||
464 | 1 | } |
|
465 | 1 | $this->port = $port; |
|
466 | 1 | } |
|
467 | |||
468 | /** |
||
469 | * Returns port name |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | 1 | public function getPort() |
|
477 | |||
478 | /** |
||
479 | * Returns device formated name |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | 1 | public function getDevice() |
|
484 | { |
||
485 | 1 | return $this->device; |
|
486 | } |
||
487 | |||
488 | /** |
||
489 | * Sets the length of a character. |
||
490 | * length of a character (5 <= length <= 8) |
||
491 | * |
||
492 | * @param int $length |
||
493 | * @return boolean |
||
494 | */ |
||
495 | 1 | public function setDataBits($length) |
|
496 | { |
||
497 | 1 | if ($length < 5 || $length > 8) { |
|
498 | $length = 8; |
||
499 | } |
||
500 | 1 | $this->databits = $length; |
|
501 | 1 | $this->formatedDataBits = $this->zDataBits($length); |
|
502 | 1 | return true; |
|
503 | } |
||
504 | |||
505 | /** |
||
506 | * Returns char length |
||
507 | * |
||
508 | * @return int |
||
509 | */ |
||
510 | 2 | public function getDataBits() |
|
511 | { |
||
512 | 2 | return $this->databits; |
|
513 | } |
||
514 | |||
515 | /** |
||
516 | * Format data bits commands |
||
517 | * |
||
518 | * @param int $length |
||
519 | * @return string |
||
520 | */ |
||
521 | 1 | protected function zDataBits($length) |
|
522 | { |
||
523 | 1 | $fdatabits = "cs$length"; |
|
524 | 1 | if ($this->ostype == self::OS_WIN) { |
|
525 | //windows |
||
526 | $fdatabits = "DATA=$length"; |
||
527 | } |
||
528 | 1 | return $fdatabits; |
|
529 | } |
||
530 | |||
531 | /** |
||
532 | * Set serial baud rate |
||
533 | * |
||
534 | * @param int $rate |
||
535 | * @return boolean |
||
536 | */ |
||
537 | 1 | public function setBaudRate($rate) |
|
538 | { |
||
539 | 1 | if (! isset($this->baudsargs[$rate])) { |
|
540 | $rate = 9600; |
||
541 | } |
||
542 | 1 | $this->baudrate = $rate; |
|
543 | 1 | $this->formatedBaudRate = $this->zBaudRate($rate); |
|
544 | 1 | return true; |
|
545 | } |
||
546 | |||
547 | /** |
||
548 | * Return baud rate |
||
549 | * |
||
550 | * @return int |
||
551 | */ |
||
552 | 2 | public function getBaudRate() |
|
553 | { |
||
554 | 2 | return $this->baudrate; |
|
555 | } |
||
556 | |||
557 | /** |
||
558 | * Format baud rate command |
||
559 | * |
||
560 | * @param int $rate |
||
561 | * @return string |
||
562 | */ |
||
563 | 1 | protected function zBaudRate($rate) |
|
572 | |||
573 | |||
574 | /** |
||
575 | * Sets parity mode |
||
576 | * |
||
577 | * @param string $parity odd, even, none |
||
578 | * @return boolean |
||
579 | */ |
||
580 | 1 | public function setParity($parity) |
|
581 | { |
||
582 | 1 | if (! isset($this->parityargs[$parity])) { |
|
583 | $parity = 'none'; |
||
584 | } |
||
585 | 1 | $this->parity = $this->parityargs[$parity][0]; |
|
586 | 1 | $this->formatedParity = $this->zParity($parity); |
|
587 | 1 | return true; |
|
588 | } |
||
589 | |||
590 | /** |
||
591 | * Get parity mode set |
||
592 | * |
||
593 | * @return string |
||
594 | */ |
||
595 | 2 | public function getParity() |
|
606 | |||
607 | /** |
||
608 | * Format parity command |
||
609 | * |
||
610 | * @param string $parity |
||
611 | * @return string |
||
612 | */ |
||
613 | 1 | protected function zParity($parity) |
|
622 | |||
623 | |||
624 | /** |
||
625 | * Set length of stop bits |
||
626 | * the length of a stop bit. |
||
627 | * It must be either 1, 1.5 or 2. |
||
628 | * 1.5 is not supported under linux and on some computers. |
||
629 | * |
||
630 | * @param float $length |
||
631 | * @return boolean |
||
632 | */ |
||
633 | 1 | public function setStopBits($length) |
|
642 | |||
643 | /** |
||
644 | * Return stop bits set |
||
645 | * |
||
646 | * @return float |
||
647 | */ |
||
648 | 2 | public function getStopBits() |
|
652 | |||
653 | /** |
||
654 | * Format stop bit command |
||
655 | * |
||
656 | * @param float $length |
||
657 | * @return string |
||
658 | */ |
||
659 | 1 | public function zStopBits($length) |
|
667 | |||
668 | /** |
||
669 | * Set the flow control mode. |
||
670 | * Availible modes : |
||
671 | * "none" : no flow control |
||
672 | * "rts/cts" : use RTS/CTS handshaking |
||
673 | * "xon/xoff" : use XON/XOFF protocol |
||
674 | * |
||
675 | * @param string $flow |
||
676 | * @return boolean |
||
677 | */ |
||
678 | 1 | public function setFlowControl($flow) |
|
693 | |||
694 | /** |
||
695 | * Returns flow control set |
||
696 | * |
||
697 | * @return string |
||
698 | */ |
||
699 | 2 | public function getFlowControl() |
|
710 | |||
711 | /** |
||
712 | * Return flow control command formated for OP type |
||
713 | * |
||
714 | * @param int $flow |
||
715 | * @return string |
||
716 | */ |
||
717 | 1 | protected function zFlowControl($flow) |
|
765 | |||
766 | /** |
||
767 | * Find OS type |
||
768 | * |
||
769 | * @return int |
||
770 | */ |
||
771 | 15 | protected function getOs() |
|
793 | |||
794 | /** |
||
795 | * Exec command line in OS console |
||
796 | * |
||
797 | * @param string $cmd comand line to execute |
||
798 | * @param array $out retorn of this command in terminal |
||
799 | * @return int |
||
800 | */ |
||
801 | 1 | public function execCommand($cmd, &$out = null) |
|
818 | } |
||
819 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: