| Conditions | 10 |
| Paths | 10 |
| Total Lines | 93 |
| Code Lines | 58 |
| 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 | { |
||
| 39 | $msg = (object) json_decode($msg); |
||
| 40 | if($msg->type == 'OpenChat initiated..!') |
||
| 41 | { |
||
| 42 | $initial = (object) array(); |
||
| 43 | $initial->initial = json_decode($this->onSidebar($from->userId)); |
||
| 44 | |||
| 45 | if($initial->initial != null) |
||
| 46 | { |
||
| 47 | $initial->conversation = json_decode( |
||
| 48 | $this->onConversation( |
||
| 49 | json_encode([ |
||
| 50 | "username" => $initial->initial[0]->login_id, |
||
| 51 | "load" => 10, |
||
| 52 | "userId" => $from->userId |
||
| 53 | ]), True |
||
| 54 | ) |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | $from->send(json_encode($initial)); |
||
| 58 | } |
||
| 59 | elseif ($msg->type == 'Load Sidebar') |
||
| 60 | { |
||
| 61 | $sidebar = (object) array(); |
||
| 62 | $sidebar->sidebar = json_decode($this->onSidebar($from->userId)); |
||
| 63 | $from->send(json_encode($sidebar)); |
||
| 64 | } |
||
| 65 | elseif ($msg->type == 'Initiated') |
||
| 66 | { |
||
| 67 | $msg->userId = $from->userId; |
||
| 68 | $result = (object) array(); |
||
| 69 | $result->conversation = json_decode($this->onConversation(json_encode($msg), False)); |
||
| 70 | $from->send(json_encode($result)); |
||
| 71 | } |
||
| 72 | elseif ($msg->type == 'Search') |
||
| 73 | { |
||
| 74 | $msg->userId = $from->userId; |
||
| 75 | $searchResult = $this->onSearch($msg); |
||
| 76 | $from->send($searchResult); |
||
| 77 | } |
||
| 78 | elseif ($msg->type == 'Compose') |
||
| 79 | { |
||
| 80 | $msg->userId = $from->userId; |
||
| 81 | $composeResult = $this->onCompose($msg); |
||
| 82 | $from->send($composeResult); |
||
| 83 | } |
||
| 84 | else |
||
| 85 | { |
||
| 86 | $msg->userId = $from->userId; |
||
| 87 | $getReturn = $this->onReply($msg); |
||
| 88 | echo $getReturn; |
||
| 89 | |||
| 90 | $receiveResult = (object) array(); |
||
| 91 | $sentResult = (object) array(); |
||
| 92 | foreach ($this->clients as $client) |
||
| 93 | { |
||
| 94 | if ($client->userId == $msg->name) |
||
| 95 | { |
||
| 96 | $receiveResult->sidebar = json_decode($this->onSidebar($client->userId)); |
||
| 97 | |||
| 98 | $receiveResult->reply = json_decode( |
||
| 99 | $this->onReceiver( |
||
| 100 | json_encode([ |
||
| 101 | "username" => $client->userId, |
||
| 102 | "load" => 10, |
||
| 103 | "userId" => $from->userId |
||
| 104 | ]), True |
||
| 105 | ) |
||
| 106 | ); |
||
| 107 | |||
| 108 | $client->send(json_encode($receiveResult)); |
||
| 109 | } |
||
| 110 | elseif($client == $from) |
||
| 111 | { |
||
| 112 | $sentResult->sidebar = json_decode($this->onSidebar($client->userId)); |
||
| 113 | |||
| 114 | $sentResult->conversation = json_decode( |
||
| 115 | $this->onConversation( |
||
| 116 | json_encode([ |
||
| 117 | "username" => $msg->name, |
||
| 118 | "load" => 10, |
||
| 119 | "userId" => $from->userId |
||
| 120 | ]), True |
||
| 121 | ) |
||
| 122 | ); |
||
| 123 | |||
| 124 | $client->send(json_encode($sentResult)); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 177 | } |
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: