| Conditions | 10 |
| Paths | 10 |
| Total Lines | 90 |
| 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 |
||
| 98 | public function onMessage(ConnectionInterface $from, $msg) |
||
| 99 | { |
||
| 100 | $msg = (object)json_decode($msg); |
||
| 101 | if ($msg->type == 'OpenChat initiated..!') { |
||
| 102 | $initial = (object)array(); |
||
| 103 | $initial->initial = json_decode($this->onSidebar($from->userId)); |
||
| 104 | |||
| 105 | if ($initial->initial != null) { |
||
| 106 | $initial->conversation = json_decode( |
||
| 107 | $this->onConversation( |
||
| 108 | json_encode( |
||
| 109 | [ |
||
| 110 | "details" => $initial->initial[0]->login_id, |
||
| 111 | "load" => 20, |
||
| 112 | "userId" => $from->userId |
||
| 113 | ] |
||
| 114 | ), true |
||
| 115 | ) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | $from->send(json_encode($initial)); |
||
| 119 | } else if ($msg->type == 'Load Sidebar') { |
||
| 120 | $sidebar = (object)array(); |
||
| 121 | $sidebar->sidebar = json_decode($this->onSidebar($from->userId)); |
||
| 122 | $from->send(json_encode($sidebar)); |
||
| 123 | } else if ($msg->type == 'Initiated') { |
||
| 124 | $msg->userId = $from->userId; |
||
| 125 | $result = (object)array(); |
||
| 126 | $result->conversation = json_decode( |
||
| 127 | $this->onConversation(json_encode($msg), false) |
||
| 128 | ); |
||
| 129 | $from->send(json_encode($result)); |
||
| 130 | } else if ($msg->type == 'Search') { |
||
| 131 | $msg->userId = $from->userId; |
||
| 132 | $searchResult = $this->onSearch($msg); |
||
| 133 | $from->send($searchResult); |
||
| 134 | } else if ($msg->type == 'Compose') { |
||
| 135 | $msg->userId = $from->userId; |
||
| 136 | $composeResult = $this->onCompose($msg); |
||
| 137 | $from->send($composeResult); |
||
| 138 | } else { |
||
| 139 | $msg->userId = $from->userId; |
||
| 140 | $msg->name = convert_uudecode(hex2bin($msg->name)); |
||
| 141 | |||
| 142 | $getReturn = $this->onReply($msg); |
||
| 143 | echo $getReturn; |
||
| 144 | |||
| 145 | $receiveResult = (object)array(); |
||
| 146 | $sentResult = (object)array(); |
||
| 147 | foreach ($this->clients as $client) { |
||
| 148 | if ($client->userId == $msg->name) { |
||
| 149 | $receiveResult->sidebar = json_decode( |
||
| 150 | $this->onSidebar($client->userId) |
||
| 151 | ); |
||
| 152 | |||
| 153 | $receiveResult->reply = json_decode( |
||
| 154 | $this->onReceiver( |
||
| 155 | json_encode( |
||
| 156 | [ |
||
| 157 | "details" => $client->userId, |
||
| 158 | "load" => 20, |
||
| 159 | "userId" => $from->userId |
||
| 160 | ] |
||
| 161 | ), true |
||
| 162 | ) |
||
| 163 | ); |
||
| 164 | |||
| 165 | $client->send(json_encode($receiveResult)); |
||
| 166 | } else if ($client == $from) { |
||
| 167 | $sentResult->sidebar = json_decode( |
||
| 168 | $this->onSidebar($client->userId) |
||
| 169 | ); |
||
| 170 | |||
| 171 | $sentResult->conversation = json_decode( |
||
| 172 | $this->onConversation( |
||
| 173 | json_encode( |
||
| 174 | [ |
||
| 175 | "details" => bin2hex(convert_uuencode($msg->name)), |
||
| 176 | "load" => 20, |
||
| 177 | "userId" => $from->userId |
||
| 178 | ] |
||
| 179 | ), true |
||
| 180 | ) |
||
| 181 | ); |
||
| 182 | $client->send(json_encode($sentResult)); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 298 |
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.