Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Session 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 Session, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Session |
||
| 15 | { |
||
| 16 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 17 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var \PHPDaemon\Request\Generic |
||
| 21 | */ |
||
| 22 | public $route; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \PHPDaemon\Structures\StackCallbacks |
||
| 26 | */ |
||
| 27 | public $onWrite; |
||
| 28 | |||
| 29 | public $id; |
||
| 30 | |||
| 31 | public $appInstance; |
||
| 32 | |||
| 33 | public $addr; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | public $buffer = []; |
||
| 40 | |||
| 41 | public $framesBuffer = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var boolean |
||
| 45 | */ |
||
| 46 | public $finished = false; |
||
| 47 | |||
| 48 | protected $onFinishedCalled = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var boolean |
||
| 52 | */ |
||
| 53 | public $flushing = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var integer |
||
| 57 | */ |
||
| 58 | public $timeout = 60; |
||
| 59 | public $server; |
||
| 60 | public $get; |
||
| 61 | public $cookie; |
||
| 62 | public $post; |
||
| 63 | |||
| 64 | protected $pollMode; |
||
| 65 | |||
| 66 | protected $running = false; |
||
| 67 | |||
| 68 | protected $finishTimer; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * toJson |
||
| 72 | * @param string $m |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | protected function toJson($m) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * __construct |
||
| 82 | * @param Application $appInstance [@todo description] |
||
| 83 | * @param string $id [@todo description] |
||
| 84 | * @param array $server [@todo description] |
||
| 85 | * @return void |
||
|
|
|||
| 86 | */ |
||
| 87 | public function __construct($appInstance, $id, $server) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Uncaught exception handler |
||
| 116 | * @param object $e |
||
| 117 | * @return boolean|null Handled? |
||
| 118 | */ |
||
| 119 | public function handleException($e) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * onHandshake |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | public function onHandshake() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * c2s |
||
| 147 | * @param object $redis |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | public function c2s($redis) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * onFrame |
||
| 167 | * @param string $msg [@todo description] |
||
| 168 | * @param integer $type [@todo description] |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public function onFrame($msg, $type) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * poll |
||
| 190 | * @param object $redis |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function poll($redis) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @TODO DESCR |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | public function onWrite() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @TODO DESCR |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | public function finish() |
||
| 236 | |||
| 237 | /*public function __destruct() { |
||
| 238 | D('destructed session '.$this->id); |
||
| 239 | }*/ |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @TODO DESCR |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | public function onFinish() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Flushes buffered packets |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function flush() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * sendPacket |
||
| 329 | * @param object $pct [@todo description] |
||
| 330 | * @param callable $cb [@todo description] |
||
| 331 | * @callback $cb ( ) |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | public function sendPacket($pct, $cb = null) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Sends a frame. |
||
| 349 | * @param string $data Frame's data. |
||
| 350 | * @param integer $type Frame's type. See the constants. |
||
| 351 | * @param callback $cb Optional. Callback called when the frame is received by client. |
||
| 352 | * @callback $cb ( ) |
||
| 353 | * @return boolean Success. |
||
| 354 | */ |
||
| 355 | public function sendFrame($data, $type = 0x00, $cb = null) |
||
| 367 | } |
||
| 368 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.