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) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Clear class params |
||
| 219 | * Used for testing proporses |
||
| 220 | */ |
||
| 221 | protected function clearParams() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Close port |
||
| 241 | */ |
||
| 242 | 15 | public function __destruct() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Open set port |
||
| 249 | * |
||
| 250 | * @return boolean |
||
| 251 | */ |
||
| 252 | 2 | public function open() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Close serial port |
||
| 277 | * |
||
| 278 | * @return boolean |
||
| 279 | */ |
||
| 280 | 15 | public function close() |
|
| 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() |
||
| 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() |
|
| 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) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns automatic mode |
||
| 366 | * |
||
| 367 | * @return bool |
||
| 368 | */ |
||
| 369 | public function getAuto() |
||
| 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) |
||
| 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) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Flushs imediatly data to serial port |
||
| 431 | * |
||
| 432 | * @return boolean |
||
| 433 | */ |
||
| 434 | public function flush() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Set port name |
||
| 448 | * |
||
| 449 | * @param string $port |
||
| 450 | */ |
||
| 451 | 1 | public function setPort($port) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Returns port name |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | 1 | public function getPort() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Returns device formated name |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | 1 | public function getDevice() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Sets the length of a character. |
||
| 496 | * length of a character (5 <= length <= 8) |
||
| 497 | * |
||
| 498 | * @param int $length |
||
| 499 | * @return boolean |
||
| 500 | */ |
||
| 501 | 1 | public function setDataBits($length) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Returns char length |
||
| 513 | * |
||
| 514 | * @return int |
||
| 515 | */ |
||
| 516 | 2 | public function getDataBits() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Format data bits commands |
||
| 523 | * |
||
| 524 | * @param int $length |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | 1 | protected function zDataBits($length) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Set serial baud rate |
||
| 539 | * |
||
| 540 | * @param int $rate |
||
| 541 | * @return boolean |
||
| 542 | */ |
||
| 543 | 1 | public function setBaudRate($rate) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Return baud rate |
||
| 555 | * |
||
| 556 | * @return int |
||
| 557 | */ |
||
| 558 | 2 | public function getBaudRate() |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Format baud rate command |
||
| 565 | * |
||
| 566 | * @param int $rate |
||
| 567 | * @return string |
||
| 568 | */ |
||
| 569 | 1 | protected function zBaudRate($rate) |
|
| 578 | |||
| 579 | |||
| 580 | /** |
||
| 581 | * Sets parity mode |
||
| 582 | * |
||
| 583 | * @param string $parity odd, even, none |
||
| 584 | * @return boolean |
||
| 585 | */ |
||
| 586 | 1 | public function setParity($parity) |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Get parity mode set |
||
| 598 | * |
||
| 599 | * @return string |
||
| 600 | */ |
||
| 601 | 2 | public function getParity() |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Format parity command |
||
| 615 | * |
||
| 616 | * @param string $parity |
||
| 617 | * @return string |
||
| 618 | */ |
||
| 619 | 1 | protected function zParity($parity) |
|
| 628 | |||
| 629 | |||
| 630 | /** |
||
| 631 | * Set length of stop bits |
||
| 632 | * the length of a stop bit. |
||
| 633 | * It must be either 1, 1.5 or 2. |
||
| 634 | * 1.5 is not supported under linux and on some computers. |
||
| 635 | * |
||
| 636 | * @param float $length |
||
| 637 | * @return boolean |
||
| 638 | */ |
||
| 639 | 1 | public function setStopBits($length) |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Return stop bits set |
||
| 651 | * |
||
| 652 | * @return float |
||
| 653 | */ |
||
| 654 | 2 | public function getStopBits() |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Format stop bit command |
||
| 661 | * |
||
| 662 | * @param float $length |
||
| 663 | * @return string |
||
| 664 | */ |
||
| 665 | 1 | public function zStopBits($length) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Set the flow control mode. |
||
| 676 | * Availible modes : |
||
| 677 | * "none" : no flow control |
||
| 678 | * "rts/cts" : use RTS/CTS handshaking |
||
| 679 | * "xon/xoff" : use XON/XOFF protocol |
||
| 680 | * |
||
| 681 | * @param string $flow |
||
| 682 | * @return boolean |
||
| 683 | */ |
||
| 684 | 1 | public function setFlowControl($flow) |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Returns flow control set |
||
| 702 | * |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | 2 | public function getFlowControl() |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Return flow control command formated for OP type |
||
| 719 | * |
||
| 720 | * @param int $flow |
||
| 721 | * @return string |
||
| 722 | */ |
||
| 723 | 1 | protected function zFlowControl($flow) |
|
| 771 | |||
| 772 | /** |
||
| 773 | * Find OS type |
||
| 774 | * |
||
| 775 | * @return int |
||
| 776 | */ |
||
| 777 | 15 | protected function getOs() |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Exec command line in OS console |
||
| 802 | * |
||
| 803 | * @param string $cmd comand line to execute |
||
| 804 | * @param array $out retorn of this command in terminal |
||
| 805 | * @return int |
||
| 806 | */ |
||
| 807 | 1 | public function execCommand($cmd, &$out = null) |
|
| 824 | } |
||
| 825 |
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: