| Conditions | 10 |
| Paths | 10 |
| Total Lines | 95 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 37 | public function onMessage(ConnectionInterface $from, $msg) { |
||
| 38 | $sessionId = $from->WebSocket->request->getCookies()['PHPSESSID']; |
||
| 39 | if($msg == 'OpenChat initiated..!') |
||
| 40 | { |
||
| 41 | $initial = (object) array(); |
||
| 42 | $initial->initial = json_decode($this->onSidebar($from->userId)); |
||
| 43 | |||
| 44 | if($initial->initial != null) { |
||
| 45 | $initial->conversation = json_decode( |
||
| 46 | $this->onConversation( |
||
| 47 | json_encode([ |
||
| 48 | "username" => $initial->initial[0]->login_id, |
||
| 49 | "load" => 10, |
||
| 50 | "userId" => $from->userId |
||
| 51 | ]), True |
||
| 52 | ) |
||
| 53 | ); |
||
| 54 | } |
||
| 55 | $from->send(json_encode($initial)); |
||
| 56 | } |
||
| 57 | elseif ($msg == 'Load Sidebar') |
||
| 58 | { |
||
| 59 | $sidebar = (object) array(); |
||
| 60 | $sidebar->sidebar = json_decode($this->onSidebar($from->userId)); |
||
| 61 | $from->send(json_encode($sidebar)); |
||
| 62 | } |
||
| 63 | elseif (@json_decode($msg)->newConversation == 'Initiated') |
||
| 64 | { |
||
| 65 | $msg = json_decode($msg); |
||
| 66 | $msg->userId = $from->userId; |
||
| 67 | $result = (object) array(); |
||
| 68 | $result->conversation = json_decode($this->onConversation(json_encode($msg), False)); |
||
| 69 | $from->send(json_encode($result)); |
||
| 70 | } |
||
| 71 | elseif (@json_decode($msg)->search == 'search') |
||
| 72 | { |
||
| 73 | $msg = json_decode($msg); |
||
| 74 | $msg->userId = $from->userId; |
||
| 75 | $searchResult = $this->onSearch($msg); |
||
| 76 | $from->send($searchResult); |
||
| 77 | } |
||
| 78 | elseif (@json_decode($msg)->Compose == 'Compose') |
||
| 79 | { |
||
| 80 | $msg = json_decode($msg); |
||
| 81 | $msg->userId = $from->userId; |
||
| 82 | $composeResult = $this->onCompose($msg); |
||
| 83 | $from->send($composeResult); |
||
| 84 | } |
||
| 85 | else |
||
| 86 | { |
||
| 87 | $msg = (object) json_decode($msg); |
||
| 88 | $msg->userId = $from->userId; |
||
| 89 | $getReturn = $this->onReply($msg); |
||
| 90 | echo $getReturn; |
||
| 91 | |||
| 92 | $receiveResult = (object) array(); |
||
| 93 | $sentResult = (object) array(); |
||
| 94 | foreach ($this->clients as $client) |
||
| 95 | { |
||
| 96 | if ($client->userId == $msg->name) |
||
| 97 | { |
||
| 98 | $receiveResult->sidebar = json_decode($this->onSidebar($client->userId)); |
||
| 99 | |||
| 100 | $receiveResult->reply = json_decode( |
||
| 101 | $this->onReceiver( |
||
| 102 | json_encode([ |
||
| 103 | "username" => $client->userId, |
||
| 104 | "load" => 10, |
||
| 105 | "userId" => $from->userId |
||
| 106 | ]), True |
||
| 107 | ) |
||
| 108 | ); |
||
| 109 | |||
| 110 | $client->send(json_encode($receiveResult)); |
||
| 111 | } |
||
| 112 | elseif($client == $from) |
||
| 113 | { |
||
| 114 | $sentResult->sidebar = json_decode($this->onSidebar($client->userId)); |
||
| 115 | |||
| 116 | $sentResult->conversation = json_decode( |
||
| 117 | $this->onConversation( |
||
| 118 | json_encode([ |
||
| 119 | "username" => $msg->name, |
||
| 120 | "load" => 10, |
||
| 121 | "userId" => $from->userId |
||
| 122 | ]), True |
||
| 123 | ) |
||
| 124 | ); |
||
| 125 | |||
| 126 | $client->send(json_encode($sentResult)); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 179 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: