| Conditions | 9 |
| Paths | 9 |
| Total Lines | 84 |
| Code Lines | 49 |
| 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 |
||
| 41 | public function onMessage(ConnectionInterface $from, $msg) { |
||
| 42 | $sessionId = $from->WebSocket->request->getCookies()['PHPSESSID']; |
||
| 43 | if($msg == 'OpenChat initiated..!') |
||
| 44 | { |
||
| 45 | @$initial->initial = json_decode($this->onSidebar($from->userId)); |
||
| 46 | |||
| 47 | @$initial->conversation = json_decode( |
||
| 48 | $this->onConversation( |
||
| 49 | json_encode([ |
||
| 50 | "username" => $initial->initial[0]->login_id, |
||
| 51 | "load" => 10 |
||
| 52 | ]), True, $sessionId |
||
| 53 | ) |
||
| 54 | ); |
||
| 55 | |||
| 56 | @$initial->conversation[0]->login_status = $this->online; |
||
| 57 | $from->send(json_encode($initial)); |
||
| 58 | } |
||
| 59 | elseif ($msg == 'Load Sidebar') |
||
| 60 | { |
||
| 61 | @$initial->initial = json_decode($this->onSidebar($from->userId)); |
||
| 62 | $from->send(json_encode($initial)); |
||
| 63 | } |
||
| 64 | elseif (@json_decode($msg)->newConversation == 'Initiated') |
||
| 65 | { |
||
| 66 | @$result->conversation = json_decode($this->onConversation($msg, False, $sessionId)); |
||
| 67 | $from->send(json_encode($result)); |
||
| 68 | } |
||
| 69 | elseif (@json_decode($msg)->search == 'search') |
||
| 70 | { |
||
| 71 | $searchResult = $this->onSearch($msg, $sessionId); |
||
| 72 | $from->send($searchResult); |
||
| 73 | } |
||
| 74 | elseif (@json_decode($msg)->Compose == 'Compose') |
||
| 75 | { |
||
| 76 | $composeResult = $this->onCompose($msg, $sessionId); |
||
| 77 | $from->send($composeResult); |
||
| 78 | } |
||
| 79 | else |
||
| 80 | { |
||
| 81 | $this->onReply($msg, $sessionId); |
||
| 82 | |||
| 83 | $msg = json_decode($msg); |
||
| 84 | // $msg->from = $from->userId; |
||
| 85 | |||
| 86 | foreach ($this->clients as $client) |
||
| 87 | { |
||
| 88 | if ($client->userId == $msg->name) |
||
| 89 | { |
||
| 90 | @$result->sidebar = json_decode($this->onSidebar($client->userId)); |
||
| 91 | |||
| 92 | @$result->conversation = json_decode( |
||
| 93 | $this->onReceiver( |
||
| 94 | json_encode([ |
||
| 95 | "username" => $client->userId, |
||
| 96 | "load" => 10 |
||
| 97 | ]), True, $sessionId |
||
| 98 | ) |
||
| 99 | ); |
||
| 100 | |||
| 101 | $client->send(json_encode($result)); |
||
| 102 | $this->online = 1; |
||
| 103 | } |
||
| 104 | elseif($client == $from) |
||
| 105 | { |
||
| 106 | @$result->sidebar = json_decode($this->onSidebar($client->userId)); |
||
| 107 | |||
| 108 | @$result->conversation = json_decode( |
||
| 109 | $this->onConversation( |
||
| 110 | json_encode([ |
||
| 111 | "username" => $msg->name, |
||
| 112 | "load" => 10 |
||
| 113 | ]), True, $sessionId |
||
| 114 | ) |
||
| 115 | ); |
||
| 116 | |||
| 117 | $result->conversation[0]->login_status = $this->online; |
||
| 118 | $client->send(json_encode($result)); |
||
| 119 | $this->online = 0; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 173 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: