Conditions | 15 |
Paths | 14 |
Total Lines | 56 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
119 | public function onMessage($data) |
||
120 | { |
||
121 | $response = $this->factory->response($data); |
||
122 | if (isset($this->deferred[$response->getId()])) { |
||
123 | $this->deferred[$response->getId()]->resolve($response); |
||
124 | } else { |
||
125 | $this->emit('message', [$response]); |
||
126 | |||
127 | if ($response instanceof Request) { |
||
128 | $params = $response->getParams(); |
||
129 | |||
130 | switch ($response->getMethod()) { |
||
131 | case ElectrumClient::HEADERS_SUBSCRIBE; |
||
|
|||
132 | if (!isset($params[0])) { |
||
133 | throw new \RuntimeException('Headers notification missing body'); |
||
134 | } |
||
135 | |||
136 | $header = $params[0]; |
||
137 | if (count($header) !== 8) { |
||
138 | throw new \RuntimeException('Headers notification missing parameter'); |
||
139 | } |
||
140 | |||
141 | $this->emit(ElectrumClient::HEADERS_SUBSCRIBE, [new HeadersNotification($header[0], $header[1], $header[2], $header[3], $header[4], $header[5], $header[6], $header[7])]); |
||
142 | break; |
||
143 | case ElectrumClient::ADDRESS_SUBSCRIBE; |
||
144 | if (!isset($params[0]) || !isset($params[1])) { |
||
145 | throw new \RuntimeException('Address notification missing address/txid'); |
||
146 | } |
||
147 | |||
148 | $this->emit(ElectrumClient::ADDRESS_SUBSCRIBE, [new AddressNotification($params[0], $params[1])]); |
||
149 | break; |
||
150 | case ElectrumClient::NUMBLOCKS_SUBSCRIBE; |
||
151 | if (!isset($params[0])) { |
||
152 | throw new \RuntimeException('Missing notification parameter: height'); |
||
153 | } |
||
154 | |||
155 | $this->emit(ElectrumClient::NUMBLOCKS_SUBSCRIBE, [new NumBlocksNotification($params[0])]); |
||
156 | break; |
||
157 | case MiningClient::SET_DIFFICULTY; |
||
158 | if (count($params) !== 1) { |
||
159 | throw new \RuntimeException('Missing mining difficulty notification parameter'); |
||
160 | } |
||
161 | |||
162 | $this->emit(MiningClient::SET_DIFFICULTY, [new SetDifficultyNotification($params[0])]); |
||
163 | break; |
||
164 | case MiningClient::NOTIFY; |
||
165 | if (count($params) !== 9) { |
||
166 | throw new \RuntimeException('Missing mining notification parameter'); |
||
167 | } |
||
168 | |||
169 | $this->emit(MiningClient::NOTIFY, [new MiningNotification($params[0], $params[1], $params[2], $params[3], $params[4], $params[5], $params[6], $params[7], $params[8])]); |
||
170 | break; |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.