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 |
||
| 26 | class PhpSerial |
||
| 27 | { |
||
| 28 | const OS_UNKNOWN = 0; |
||
| 29 | const OS_WIN = 1; //WINS32 WINNT Windows |
||
| 30 | const OS_LINUX = 2; |
||
| 31 | const OS_CYGWIN = 3; //Cygwin Windows Linux like commands |
||
| 32 | const OS_UNIX = 4; |
||
| 33 | const OS_BSD = 5; //FreeBSD or NetBSD or OpenBSD /dev/ttyu1 |
||
| 34 | const OS_OSX = 6; //Darwin MacOS |
||
| 35 | const OS_HPUX = 7; //tty1p0 |
||
| 36 | const SERIAL_DEVICE_NOTSET = 0; |
||
| 37 | const SERIAL_DEVICE_SET = 1; |
||
| 38 | const SERIAL_DEVICE_OPENED = 2; |
||
| 39 | const PARITY_NONE = 0; |
||
| 40 | const PARITY_ODD = 1; |
||
| 41 | const PARITY_EVEN = 2; |
||
| 42 | const FLOW_NONE = 0; //no flow control |
||
| 43 | const FLOW_RTSCTS = 1; // use RTS/CTS handshaking |
||
| 44 | const FLOW_XONXOFF = 2; //use XON/XOFF protocol |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Pointer for device |
||
| 48 | * |
||
| 49 | * @var resource |
||
| 50 | */ |
||
| 51 | protected $handle = null; |
||
| 52 | /** |
||
| 53 | * Data buffer |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $buffer = ""; |
||
| 58 | /** |
||
| 59 | * This var says if buffer should be flushed by write (true) or |
||
| 60 | * manually (false) |
||
| 61 | * |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $autoflush = false; |
||
| 65 | /** |
||
| 66 | * Wait time after send data to serial |
||
| 67 | * |
||
| 68 | * @var float |
||
| 69 | */ |
||
| 70 | protected $waittime = 0.1; |
||
| 71 | /** |
||
| 72 | * OS type where php is running |
||
| 73 | * |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | protected $ostype = 2; //linux is default |
||
| 77 | /** |
||
| 78 | * Mode command to set up serial port |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $mode = ''; //formated device mode for especific OS use |
||
| 83 | /** |
||
| 84 | * Status of port |
||
| 85 | * NoSet, Set or Open |
||
| 86 | * |
||
| 87 | * @var int |
||
| 88 | */ |
||
| 89 | protected $state = self::SERIAL_DEVICE_NOTSET; |
||
| 90 | /** |
||
| 91 | * Port name |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | protected $port = '/dev/ttyS0'; |
||
| 96 | /** |
||
| 97 | * Data bits |
||
| 98 | * |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | protected $databits = 8; |
||
| 102 | /** |
||
| 103 | * Baud Rate |
||
| 104 | * |
||
| 105 | * @var int |
||
| 106 | */ |
||
| 107 | protected $baudrate = 9600; |
||
| 108 | /** |
||
| 109 | * Parity |
||
| 110 | * |
||
| 111 | * @var int |
||
| 112 | */ |
||
| 113 | protected $parity = self::PARITY_NONE; |
||
| 114 | /** |
||
| 115 | * Stop Bits |
||
| 116 | * |
||
| 117 | * @var float |
||
| 118 | */ |
||
| 119 | protected $stopbits = 1; |
||
| 120 | /** |
||
| 121 | * Flow Control |
||
| 122 | * |
||
| 123 | * @var int |
||
| 124 | */ |
||
| 125 | protected $flowcontrol = self::FLOW_NONE; |
||
| 126 | /** |
||
| 127 | * Formated device name command |
||
| 128 | * |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | protected $device = '/dev/ttyS0'; |
||
| 132 | /** |
||
| 133 | * Formated Data Bits command |
||
| 134 | * |
||
| 135 | * @var string |
||
| 136 | */ |
||
| 137 | protected $formatedDataBits = 'cs8'; |
||
| 138 | /** |
||
| 139 | * Formated Baud Rate command |
||
| 140 | * |
||
| 141 | * @var string |
||
| 142 | */ |
||
| 143 | protected $formatedBaudRate = '9600'; |
||
| 144 | /** |
||
| 145 | * Formated parity command |
||
| 146 | * |
||
| 147 | * @var string |
||
| 148 | */ |
||
| 149 | protected $formatedParity = '-parenb'; |
||
| 150 | /** |
||
| 151 | * Formated stop bits command |
||
| 152 | * |
||
| 153 | * @var string |
||
| 154 | */ |
||
| 155 | protected $formatedStopBits = '-cstopb'; |
||
| 156 | /** |
||
| 157 | * Formated flow control command |
||
| 158 | * |
||
| 159 | * @var string |
||
| 160 | */ |
||
| 161 | protected $formatedFlowControl = 'clocal -crtscts -ixon -ixoff'; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Parity data |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | private $parityargs = [ |
||
| 169 | "none" => [0, "-parenb"], |
||
| 170 | "odd" => [1, "parenb parodd"], |
||
| 171 | "even" => [2, "parenb -parodd"] |
||
| 172 | ]; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Basud Rate data |
||
| 176 | * |
||
| 177 | * @var array |
||
| 178 | */ |
||
| 179 | private $baudsargs = array ( |
||
| 180 | 110 => 11, |
||
| 181 | 150 => 15, |
||
| 182 | 300 => 30, |
||
| 183 | 600 => 60, |
||
| 184 | 1200 => 12, |
||
| 185 | 2400 => 24, |
||
| 186 | 4800 => 48, |
||
| 187 | 9600 => 96, |
||
| 188 | 19200 => 19, |
||
| 189 | 38400 => 38400, |
||
| 190 | 57600 => 57600, |
||
| 191 | 115200 => 115200 |
||
| 192 | ); |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Constructor |
||
| 196 | * Set ostype parameter |
||
| 197 | * |
||
| 198 | * @param int $forceOS |
||
| 199 | */ |
||
| 200 | 14 | public function __construct($forceOS = null) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Clear class params |
||
| 215 | * Used for testing proporses |
||
| 216 | */ |
||
| 217 | protected function clearParams() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Close port |
||
| 237 | */ |
||
| 238 | 14 | public function __destruct() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Open set port |
||
| 245 | * |
||
| 246 | * @return boolean |
||
| 247 | */ |
||
| 248 | 2 | public function open() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Close serial port |
||
| 269 | * |
||
| 270 | * @return boolean |
||
| 271 | */ |
||
| 272 | 14 | public function close() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Use class parameters to configure the serial port |
||
| 287 | */ |
||
| 288 | 2 | public function config() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Set automatic send massage to serial |
||
| 320 | * |
||
| 321 | * @param bool $auto |
||
| 322 | * @param float $time |
||
| 323 | */ |
||
| 324 | public function setAuto($auto, $waittime) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Returns automatic mode |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public function getAuto() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Read serial port |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function read() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Write data to buffer or serial port |
||
| 358 | * depends of getAuto() |
||
| 359 | * if getAuto() == true this command writes directly to port |
||
| 360 | * if getAuto() == false this command writes to buffer (default) |
||
| 361 | * |
||
| 362 | * @param string $data |
||
| 363 | * @return boolean |
||
| 364 | */ |
||
| 365 | public function write($data) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Flushs imediatly data to serial port |
||
| 377 | * |
||
| 378 | * @return boolean |
||
| 379 | */ |
||
| 380 | public function flush() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Set port name |
||
| 391 | * |
||
| 392 | * @param string $port |
||
| 393 | */ |
||
| 394 | 1 | public function setPort($port) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Returns port name |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | 1 | public function getPort() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Returns device formated name |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | 1 | public function getDevice() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Sets the length of a character. |
||
| 439 | * length of a character (5 <= length <= 8) |
||
| 440 | * |
||
| 441 | * @param int $length |
||
| 442 | * @return boolean |
||
| 443 | */ |
||
| 444 | 1 | public function setDataBits($length) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Returns char length |
||
| 456 | * |
||
| 457 | * @return int |
||
| 458 | */ |
||
| 459 | 2 | public function getDataBits() |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Format data bits commands |
||
| 466 | * |
||
| 467 | * @param int $length |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | 1 | protected function zDataBits($length) |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Set serial baud rate |
||
| 482 | * |
||
| 483 | * @param int $rate |
||
| 484 | * @return boolean |
||
| 485 | */ |
||
| 486 | 1 | public function setBaudRate($rate) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Return baud rate |
||
| 498 | * |
||
| 499 | * @return int |
||
| 500 | */ |
||
| 501 | 2 | public function getBaudRate() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Format baud rate command |
||
| 508 | * |
||
| 509 | * @param int $rate |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | 1 | protected function zBaudRate($rate) |
|
| 521 | |||
| 522 | |||
| 523 | /** |
||
| 524 | * Sets parity mode |
||
| 525 | * |
||
| 526 | * @param string $parity odd, even, none |
||
| 527 | * @return boolean |
||
| 528 | */ |
||
| 529 | 1 | public function setParity($parity) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Get parity mode set |
||
| 541 | * |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | 2 | public function getParity() |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Format parity command |
||
| 558 | * |
||
| 559 | * @param string $parity |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | 1 | protected function zParity($parity) |
|
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * Set length of stop bits |
||
| 575 | * the length of a stop bit. |
||
| 576 | * It must be either 1, 1.5 or 2. |
||
| 577 | * 1.5 is not supported under linux and on some computers. |
||
| 578 | * |
||
| 579 | * @param float $length |
||
| 580 | * @return boolean |
||
| 581 | */ |
||
| 582 | 1 | public function setStopBits($length) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Return stop bits set |
||
| 594 | * |
||
| 595 | * @return float |
||
| 596 | */ |
||
| 597 | 2 | public function getStopBits() |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Format stop bit command |
||
| 604 | * |
||
| 605 | * @param float $length |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | 1 | public function zStopBits($length) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Set the flow control mode. |
||
| 619 | * Availible modes : |
||
| 620 | * "none" : no flow control |
||
| 621 | * "rts/cts" : use RTS/CTS handshaking |
||
| 622 | * "xon/xoff" : use XON/XOFF protocol |
||
| 623 | * |
||
| 624 | * @param string $flow |
||
| 625 | * @return boolean |
||
| 626 | */ |
||
| 627 | 1 | public function setFlowControl($flow) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Returns flow control set |
||
| 645 | * |
||
| 646 | * @return string |
||
| 647 | */ |
||
| 648 | 2 | public function getFlowControl() |
|
| 659 | |||
| 660 | /** |
||
| 661 | * Return flow control command formated for OP type |
||
| 662 | * |
||
| 663 | * @param int $flow |
||
| 664 | * @return string |
||
| 665 | */ |
||
| 666 | 1 | protected function zFlowControl($flow) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Find OS type |
||
| 689 | * |
||
| 690 | * @return int |
||
| 691 | */ |
||
| 692 | 14 | protected function getOs() |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Exec command line in OS console |
||
| 716 | * |
||
| 717 | * @param type $cmd |
||
| 718 | * @param type $out |
||
| 719 | * @return type |
||
| 720 | */ |
||
| 721 | protected function execCommand($cmd, &$out = null) |
||
| 738 | } |
||
| 739 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: