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 | const SERIAL_DEVICE_NOTSET = 0; |
||
| 36 | const SERIAL_DEVICE_SET = 1; |
||
| 37 | const SERIAL_DEVICE_OPENED = 2; |
||
| 38 | const PARITY_NONE = 0; |
||
| 39 | const PARITY_ODD = 1; |
||
| 40 | const PARITY_EVEN = 2; |
||
| 41 | const FLOW_NONE = 0; //no flow control |
||
| 42 | const FLOW_RTSCTS = 1; // use RTS/CTS handshaking |
||
| 43 | const FLOW_XONXOFF = 2; //use XON/XOFF protocol |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Pointer for device |
||
| 47 | * @var resource|bool |
||
| 48 | */ |
||
| 49 | protected $handle = false; |
||
| 50 | /** |
||
| 51 | * Data buffer |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $buffer = ""; |
||
| 55 | /** |
||
| 56 | * This var says if buffer should be flushed by write (true) or |
||
| 57 | * manually (false) |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | protected $autoflush = false; |
||
| 61 | /** |
||
| 62 | * Wait time after send data to serial |
||
| 63 | * @var float |
||
| 64 | */ |
||
| 65 | protected $waittime = 0.1; |
||
| 66 | /** |
||
| 67 | * OS type where php is running |
||
| 68 | * linux is default |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $ostype = 2; |
||
| 72 | /** |
||
| 73 | * Mode command to set up serial port |
||
| 74 | * formated device mode for especific OS use |
||
| 75 | * @var string|null |
||
| 76 | */ |
||
| 77 | protected $mode = ''; |
||
| 78 | /** |
||
| 79 | * Status of port |
||
| 80 | * NoSet, Set or Open |
||
| 81 | * @var int|null |
||
| 82 | */ |
||
| 83 | protected $state = self::SERIAL_DEVICE_NOTSET; |
||
| 84 | /** |
||
| 85 | * Port name |
||
| 86 | * @var string|null |
||
| 87 | */ |
||
| 88 | protected $port = '/dev/ttyS0'; |
||
| 89 | /** |
||
| 90 | * Data bits |
||
| 91 | * @var int|null |
||
| 92 | */ |
||
| 93 | protected $databits = 8; |
||
| 94 | /** |
||
| 95 | * Baud Rate |
||
| 96 | * @var int|null |
||
| 97 | */ |
||
| 98 | protected $baudrate = 9600; |
||
| 99 | /** |
||
| 100 | * Parity |
||
| 101 | * @var int|null |
||
| 102 | */ |
||
| 103 | protected $parity = self::PARITY_NONE; |
||
| 104 | /** |
||
| 105 | * Stop Bits |
||
| 106 | * @var int|null |
||
| 107 | */ |
||
| 108 | protected $stopbits = 1; |
||
| 109 | /** |
||
| 110 | * Flow Control |
||
| 111 | * @var int|null |
||
| 112 | */ |
||
| 113 | protected $flowcontrol = self::FLOW_NONE; |
||
| 114 | /** |
||
| 115 | * Formated device name command |
||
| 116 | * @var string|null |
||
| 117 | */ |
||
| 118 | protected $device = '/dev/ttyS0'; |
||
| 119 | /** |
||
| 120 | * Formated Data Bits command |
||
| 121 | * |
||
| 122 | * @var string|null |
||
| 123 | */ |
||
| 124 | protected $formatedDataBits = 'cs8'; |
||
| 125 | /** |
||
| 126 | * Formated Baud Rate command |
||
| 127 | * @var string|null |
||
| 128 | */ |
||
| 129 | protected $formatedBaudRate = '9600'; |
||
| 130 | /** |
||
| 131 | * Formated parity command |
||
| 132 | * @var string|null |
||
| 133 | */ |
||
| 134 | protected $formatedParity = '-parenb'; |
||
| 135 | /** |
||
| 136 | * Formated stop bits command |
||
| 137 | * @var string|null |
||
| 138 | */ |
||
| 139 | protected $formatedStopBits = '-cstopb'; |
||
| 140 | /** |
||
| 141 | * Formated flow control command |
||
| 142 | * @var string|null |
||
| 143 | */ |
||
| 144 | protected $formatedFlowControl = 'clocal -crtscts -ixon -ixoff'; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Parity data |
||
| 148 | * |
||
| 149 | * @var array |
||
| 150 | */ |
||
| 151 | private $parityargs = [ |
||
| 152 | "none" => [0, "-parenb"], |
||
| 153 | "odd" => [1, "parenb parodd"], |
||
| 154 | "even" => [2, "parenb -parodd"] |
||
| 155 | ]; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Basud Rate data |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | private $baudsargs = array ( |
||
| 163 | 110 => 11, |
||
| 164 | 150 => 15, |
||
| 165 | 300 => 30, |
||
| 166 | 600 => 60, |
||
| 167 | 1200 => 12, |
||
| 168 | 2400 => 24, |
||
| 169 | 4800 => 48, |
||
| 170 | 9600 => 96, |
||
| 171 | 19200 => 19, |
||
| 172 | 38400 => 38400, |
||
| 173 | 57600 => 57600, |
||
| 174 | 115200 => 115200 |
||
| 175 | ); |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Constructor |
||
| 179 | * Set ostype parameter |
||
| 180 | * @param int $forceOS |
||
| 181 | */ |
||
| 182 | public function __construct($forceOS = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Clear class params |
||
| 197 | * Used for testing proporses |
||
| 198 | */ |
||
| 199 | protected function clearParams() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Close port |
||
| 219 | */ |
||
| 220 | public function __destruct() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Open set port |
||
| 227 | * @return boolean |
||
| 228 | */ |
||
| 229 | public function open() |
||
| 250 | |||
| 251 | /** |
||
| 252 | 2 | * Close serial port |
|
| 253 | * |
||
| 254 | 2 | * @return boolean |
|
| 255 | */ |
||
| 256 | public function close() |
||
| 268 | 1 | ||
| 269 | /** |
||
| 270 | 1 | * Returns the setup configuration for serial port |
|
| 271 | 1 | * this command will be exectuted in terminal |
|
| 272 | 1 | * @return string|null |
|
| 273 | */ |
||
| 274 | public function getSetUp() |
||
| 278 | |||
| 279 | /** |
||
| 280 | 15 | * Use class parameters to configure the serial port |
|
| 281 | * before the serial port is opened it must be configured, |
||
| 282 | 15 | * and in windows environment, all sets at a single time |
|
| 283 | 14 | * @return bool |
|
| 284 | */ |
||
| 285 | 1 | public function setUp() |
|
| 319 | |||
| 320 | 1 | /** |
|
| 321 | 1 | * Set automatic send massage to serial |
|
| 322 | 1 | * @param bool $auto |
|
| 323 | 1 | * @param float $waittime |
|
| 324 | 1 | */ |
|
| 325 | 1 | public function setAuto($auto, $waittime) |
|
| 336 | |||
| 337 | 1 | /** |
|
| 338 | 1 | * Returns automatic mode |
|
| 339 | 1 | * |
|
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public function getAuto() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Read serial port |
||
| 349 | * |
||
| 350 | * @param int $count Number of characters to be read (will stop before |
||
| 351 | * if less characters are in the buffer) |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function read($count = 0) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Write data to buffer or serial port |
||
| 382 | * depends of getAuto() |
||
| 383 | * if getAuto() == true this command writes directly to port |
||
| 384 | * if getAuto() == false this command writes to buffer (default) |
||
| 385 | * @param string $data |
||
| 386 | * @return boolean |
||
| 387 | */ |
||
| 388 | public function write($data) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Flushs imediatly data to serial port |
||
| 403 | * @return boolean |
||
| 404 | */ |
||
| 405 | public function flush() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Set port name |
||
| 419 | * |
||
| 420 | * @param string $port |
||
| 421 | */ |
||
| 422 | public function setPort($port) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Returns port name |
||
| 447 | * @return string|null |
||
| 448 | */ |
||
| 449 | public function getPort() |
||
| 453 | |||
| 454 | 1 | /** |
|
| 455 | * Returns device formated name |
||
| 456 | 1 | * @return string|null |
|
| 457 | 1 | */ |
|
| 458 | public function getDevice() |
||
| 462 | 1 | ||
| 463 | 1 | /** |
|
| 464 | * Sets the length of a character. |
||
| 465 | * length of a character (5 <= length <= 8) |
||
| 466 | * |
||
| 467 | * @param int $length |
||
| 468 | * @return boolean |
||
| 469 | */ |
||
| 470 | 1 | public function setDataBits($length) |
|
| 479 | 1 | ||
| 480 | /** |
||
| 481 | 1 | * Returns char length |
|
| 482 | * @return int|null |
||
| 483 | */ |
||
| 484 | public function getDataBits() |
||
| 488 | |||
| 489 | 1 | /** |
|
| 490 | * Format data bits commands |
||
| 491 | 1 | * |
|
| 492 | * @param int $length |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | protected function zDataBits($length) |
||
| 504 | |||
| 505 | /** |
||
| 506 | 1 | * Set serial baud rate |
|
| 507 | 1 | * |
|
| 508 | 1 | * @param int $rate |
|
| 509 | * @return boolean |
||
| 510 | */ |
||
| 511 | public function setBaudRate($rate) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Return baud rate |
||
| 523 | * @return int|null |
||
| 524 | */ |
||
| 525 | public function getBaudRate() |
||
| 529 | 1 | ||
| 530 | 1 | /** |
|
| 531 | * Format baud rate command |
||
| 532 | * @param int $rate |
||
| 533 | * @return string |
||
| 534 | 1 | */ |
|
| 535 | protected function zBaudRate($rate) |
||
| 544 | |||
| 545 | 1 | ||
| 546 | /** |
||
| 547 | * Sets parity mode |
||
| 548 | 1 | * |
|
| 549 | 1 | * @param string $parity odd, even, none |
|
| 550 | 1 | * @return boolean |
|
| 551 | */ |
||
| 552 | public function setParity($parity) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get parity mode set |
||
| 564 | * |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | public function getParity() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Format parity command |
||
| 581 | * |
||
| 582 | * @param string $parity |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | protected function zParity($parity) |
||
| 594 | |||
| 595 | |||
| 596 | /** |
||
| 597 | * Set length of stop bits |
||
| 598 | * the length of a stop bit. |
||
| 599 | * It must be either 1 or 2. |
||
| 600 | * 1.5 is not supported under linux and on some computers. |
||
| 601 | 2 | * @param int $length |
|
| 602 | * @return boolean |
||
| 603 | 2 | */ |
|
| 604 | 2 | public function setStopBits($length) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Return stop bits set |
||
| 616 | * @return int|null |
||
| 617 | */ |
||
| 618 | public function getStopBits() |
||
| 622 | 1 | ||
| 623 | /** |
||
| 624 | * Format stop bit command |
||
| 625 | * @param int $length |
||
| 626 | 1 | * @return string|null |
|
| 627 | */ |
||
| 628 | public function formatStopBits($length) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Set the flow control mode. |
||
| 639 | 1 | * Availible modes : |
|
| 640 | * "none" : no flow control |
||
| 641 | 1 | * "rts/cts" : use RTS/CTS handshaking |
|
| 642 | * "xon/xoff" : use XON/XOFF protocol |
||
| 643 | * @param string $flow |
||
| 644 | 1 | * @return boolean |
|
| 645 | 1 | */ |
|
| 646 | 1 | public function setFlowControl($flow) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Returns flow control set |
||
| 664 | * @return string |
||
| 665 | 1 | */ |
|
| 666 | public function getFlowControl() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Return flow control command formated for OP type |
||
| 680 | * @param int $flow |
||
| 681 | * @return string |
||
| 682 | */ |
||
| 683 | protected function zFlowControl($flow) |
||
| 731 | 1 | ||
| 732 | /** |
||
| 733 | 1 | * Find OS type |
|
| 734 | 1 | * @return int |
|
| 735 | 1 | */ |
|
| 736 | protected function getOs() |
||
| 758 | 1 | ||
| 759 | 1 | /** |
|
| 760 | * Exec command line in OS console |
||
| 761 | 1 | * @param string $cmd comand line to execute |
|
| 762 | * @param array|null $out return of this command in terminal |
||
| 763 | 1 | * @return int |
|
| 764 | 1 | */ |
|
| 765 | 1 | public function execCommand($cmd, &$out = null) |
|
| 782 | } |
||
| 783 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.