| Conditions | 10 |
| Paths | 7 |
| Total Lines | 61 |
| Code Lines | 30 |
| Lines | 17 |
| Ratio | 27.87 % |
| 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 |
||
| 213 | public function apertiumTrans() |
||
| 214 | { |
||
| 215 | // Check if it can be translated from online sources. |
||
| 216 | |||
| 217 | $host = 'api.apertium.org'; |
||
| 218 | if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) { |
||
| 219 | |||
| 220 | // Host online |
||
| 221 | |||
| 222 | $urlString = urlencode($this->string); |
||
| 223 | $url = "http://$host/json/translate?q=$urlString&langpair=$this->from%7C$this->to"; |
||
| 224 | $json = file_get_contents($url); |
||
| 225 | $data = json_decode($json); |
||
| 226 | |||
| 227 | // Checking response status |
||
| 228 | |||
| 229 | if ($data->responseStatus != 200) { |
||
| 230 | if ($this->debug == true) { |
||
| 231 | $this->translation = "<font style='color:red;'>Error ".$data->responseStatus.': '.$data->responseDetails.'</font>'; |
||
| 232 | } |
||
| 233 | |||
| 234 | return; |
||
| 235 | } |
||
| 236 | |||
| 237 | $transObtained = $data->responseData->translatedText; |
||
| 238 | |||
| 239 | |||
| 240 | $this->translation = ucfirst(strtolower(trim(str_replace('*', ' ', $transObtained)))); |
||
| 241 | |||
| 242 | |||
| 243 | // Checking debug setting to determinate how to output translation |
||
| 244 | |||
| 245 | View Code Duplication | if ($this->debug == true) { |
|
| 246 | $errors = ''; |
||
| 247 | $words = explode(' ', $transObtained); |
||
| 248 | foreach ($words as $word) { |
||
| 249 | if ($word != '') { |
||
| 250 | if ($word[0] == '*') { |
||
| 251 | $errors = $errors.substr($word, 1).', '; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | if ($errors == '') { |
||
| 257 | $this->translation = "<font style='color:#00CC00;'>".$this->translation.'</font>'; |
||
| 258 | } else { |
||
| 259 | $this->translation = "<font style='color:orange;'>Unknoun words: ".substr($errors, 0, -2).'</font>'; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | fclose($socket); |
||
| 264 | return; |
||
| 265 | |||
| 266 | } else { |
||
| 267 | //host offline |
||
| 268 | if ($this->debug == true) { |
||
| 269 | $this->translation = "<font style='color:red;'>Apertium host is down</font>"; |
||
| 270 | } |
||
| 271 | return; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 282 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.