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 | * linux is default |
||
| 74 | * |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | protected $ostype = 2; |
||
| 78 | /** |
||
| 79 | * Mode command to set up serial port |
||
| 80 | * formated device mode for especific OS use |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $mode = ''; |
||
| 85 | /** |
||
| 86 | * Status of port |
||
| 87 | * NoSet, Set or Open |
||
| 88 | * |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | protected $state = self::SERIAL_DEVICE_NOTSET; |
||
| 92 | /** |
||
| 93 | * Port name |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $port = '/dev/ttyS0'; |
||
| 98 | /** |
||
| 99 | * Data bits |
||
| 100 | * |
||
| 101 | * @var int |
||
| 102 | */ |
||
| 103 | protected $databits = 8; |
||
| 104 | /** |
||
| 105 | * Baud Rate |
||
| 106 | * |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | protected $baudrate = 9600; |
||
| 110 | /** |
||
| 111 | * Parity |
||
| 112 | * |
||
| 113 | * @var int |
||
| 114 | */ |
||
| 115 | protected $parity = self::PARITY_NONE; |
||
| 116 | /** |
||
| 117 | * Stop Bits |
||
| 118 | * |
||
| 119 | * @var float |
||
| 120 | */ |
||
| 121 | protected $stopbits = 1; |
||
| 122 | /** |
||
| 123 | * Flow Control |
||
| 124 | * |
||
| 125 | * @var int |
||
| 126 | */ |
||
| 127 | protected $flowcontrol = self::FLOW_NONE; |
||
| 128 | /** |
||
| 129 | * Formated device name command |
||
| 130 | * |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | protected $device = '/dev/ttyS0'; |
||
| 134 | /** |
||
| 135 | * Formated Data Bits command |
||
| 136 | * |
||
| 137 | * @var string |
||
| 138 | */ |
||
| 139 | protected $formatedDataBits = 'cs8'; |
||
| 140 | /** |
||
| 141 | * Formated Baud Rate command |
||
| 142 | * |
||
| 143 | * @var string |
||
| 144 | */ |
||
| 145 | protected $formatedBaudRate = '9600'; |
||
| 146 | /** |
||
| 147 | * Formated parity command |
||
| 148 | * |
||
| 149 | * @var string |
||
| 150 | */ |
||
| 151 | protected $formatedParity = '-parenb'; |
||
| 152 | /** |
||
| 153 | * Formated stop bits command |
||
| 154 | * |
||
| 155 | * @var string |
||
| 156 | */ |
||
| 157 | protected $formatedStopBits = '-cstopb'; |
||
| 158 | /** |
||
| 159 | * Formated flow control command |
||
| 160 | * |
||
| 161 | * @var string |
||
| 162 | */ |
||
| 163 | protected $formatedFlowControl = 'clocal -crtscts -ixon -ixoff'; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Parity data |
||
| 167 | * |
||
| 168 | * @var array |
||
| 169 | */ |
||
| 170 | private $parityargs = [ |
||
| 171 | "none" => [0, "-parenb"], |
||
| 172 | "odd" => [1, "parenb parodd"], |
||
| 173 | "even" => [2, "parenb -parodd"] |
||
| 174 | ]; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Basud Rate data |
||
| 178 | * |
||
| 179 | * @var array |
||
| 180 | */ |
||
| 181 | private $baudsargs = array ( |
||
| 182 | 110 => 11, |
||
| 183 | 150 => 15, |
||
| 184 | 300 => 30, |
||
| 185 | 600 => 60, |
||
| 186 | 1200 => 12, |
||
| 187 | 2400 => 24, |
||
| 188 | 4800 => 48, |
||
| 189 | 9600 => 96, |
||
| 190 | 19200 => 19, |
||
| 191 | 38400 => 38400, |
||
| 192 | 57600 => 57600, |
||
| 193 | 115200 => 115200 |
||
| 194 | ); |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Constructor |
||
| 198 | * Set ostype parameter |
||
| 199 | * |
||
| 200 | * @param int $forceOS |
||
| 201 | */ |
||
| 202 | 14 | public function __construct($forceOS = null) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Clear class params |
||
| 217 | * Used for testing proporses |
||
| 218 | */ |
||
| 219 | protected function clearParams() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Close port |
||
| 239 | */ |
||
| 240 | 14 | public function __destruct() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Open set port |
||
| 247 | * |
||
| 248 | * @return boolean |
||
| 249 | */ |
||
| 250 | 2 | public function open() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Close serial port |
||
| 271 | * |
||
| 272 | * @return boolean |
||
| 273 | */ |
||
| 274 | 14 | public function close() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Use class parameters to configure the serial port |
||
| 289 | */ |
||
| 290 | 2 | public function setUp() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Set automatic send massage to serial |
||
| 322 | * |
||
| 323 | * @param bool $auto |
||
| 324 | * @param float $waittime |
||
| 325 | */ |
||
| 326 | public function setAuto($auto, $waittime) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Returns automatic mode |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | public function getAuto() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Read serial port |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function read() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Write data to buffer or serial port |
||
| 360 | * depends of getAuto() |
||
| 361 | * if getAuto() == true this command writes directly to port |
||
| 362 | * if getAuto() == false this command writes to buffer (default) |
||
| 363 | * |
||
| 364 | * @param string $data |
||
| 365 | * @return boolean |
||
| 366 | */ |
||
| 367 | public function write($data) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Flushs imediatly data to serial port |
||
| 379 | * |
||
| 380 | * @return boolean |
||
| 381 | */ |
||
| 382 | public function flush() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set port name |
||
| 393 | * |
||
| 394 | * @param string $port |
||
| 395 | */ |
||
| 396 | 1 | public function setPort($port) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Returns port name |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | 1 | public function getPort() |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Returns device formated name |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | 1 | public function getDevice() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Sets the length of a character. |
||
| 441 | * length of a character (5 <= length <= 8) |
||
| 442 | * |
||
| 443 | * @param int $length |
||
| 444 | * @return boolean |
||
| 445 | */ |
||
| 446 | 1 | public function setDataBits($length) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Returns char length |
||
| 458 | * |
||
| 459 | * @return int |
||
| 460 | */ |
||
| 461 | 2 | public function getDataBits() |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Format data bits commands |
||
| 468 | * |
||
| 469 | * @param int $length |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | 1 | protected function zDataBits($length) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Set serial baud rate |
||
| 484 | * |
||
| 485 | * @param int $rate |
||
| 486 | * @return boolean |
||
| 487 | */ |
||
| 488 | 1 | public function setBaudRate($rate) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Return baud rate |
||
| 500 | * |
||
| 501 | * @return int |
||
| 502 | */ |
||
| 503 | 2 | public function getBaudRate() |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Format baud rate command |
||
| 510 | * |
||
| 511 | * @param int $rate |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | 1 | protected function zBaudRate($rate) |
|
| 523 | |||
| 524 | |||
| 525 | /** |
||
| 526 | * Sets parity mode |
||
| 527 | * |
||
| 528 | * @param string $parity odd, even, none |
||
| 529 | * @return boolean |
||
| 530 | */ |
||
| 531 | 1 | public function setParity($parity) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Get parity mode set |
||
| 543 | * |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | 2 | public function getParity() |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Format parity command |
||
| 560 | * |
||
| 561 | * @param string $parity |
||
| 562 | * @return string |
||
| 563 | */ |
||
| 564 | 1 | protected function zParity($parity) |
|
| 573 | |||
| 574 | |||
| 575 | /** |
||
| 576 | * Set length of stop bits |
||
| 577 | * the length of a stop bit. |
||
| 578 | * It must be either 1, 1.5 or 2. |
||
| 579 | * 1.5 is not supported under linux and on some computers. |
||
| 580 | * |
||
| 581 | * @param float $length |
||
| 582 | * @return boolean |
||
| 583 | */ |
||
| 584 | 1 | public function setStopBits($length) |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Return stop bits set |
||
| 596 | * |
||
| 597 | * @return float |
||
| 598 | */ |
||
| 599 | 2 | public function getStopBits() |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Format stop bit command |
||
| 606 | * |
||
| 607 | * @param float $length |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | 1 | public function zStopBits($length) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Set the flow control mode. |
||
| 621 | * Availible modes : |
||
| 622 | * "none" : no flow control |
||
| 623 | * "rts/cts" : use RTS/CTS handshaking |
||
| 624 | * "xon/xoff" : use XON/XOFF protocol |
||
| 625 | * |
||
| 626 | * @param string $flow |
||
| 627 | * @return boolean |
||
| 628 | */ |
||
| 629 | 1 | public function setFlowControl($flow) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Returns flow control set |
||
| 647 | * |
||
| 648 | * @return string |
||
| 649 | */ |
||
| 650 | 2 | public function getFlowControl() |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Return flow control command formated for OP type |
||
| 664 | * |
||
| 665 | * @param int $flow |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | 1 | protected function zFlowControl($flow) |
|
| 688 | |||
| 689 | /** |
||
| 690 | * Find OS type |
||
| 691 | * |
||
| 692 | * @return int |
||
| 693 | */ |
||
| 694 | 14 | protected function getOs() |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Exec command line in OS console |
||
| 719 | * |
||
| 720 | * @param type $cmd |
||
| 721 | * @param type $out |
||
| 722 | * @return type |
||
| 723 | */ |
||
| 724 | public function execCommand($cmd, &$out = null) |
||
| 741 | } |
||
| 742 |
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: