| Conditions | 9 |
| Paths | 6 |
| Total Lines | 54 |
| Code Lines | 26 |
| 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 |
||
| 10 | public function main() |
||
| 11 | { |
||
| 12 | // Check if it can be translated from online sources. |
||
| 13 | |||
| 14 | $host = 'api.apertium.org'; |
||
| 15 | if ($this->checkHost($host)) { |
||
| 16 | // Host online |
||
| 17 | |||
| 18 | $urlString = urlencode($this->string); |
||
| 19 | $url = "http://$host/json/translate?q=$urlString&langpair=$this->from%7C$this->to"; |
||
| 20 | $json = file_get_contents($url); |
||
| 21 | $data = json_decode($json); |
||
| 22 | |||
| 23 | // Checking response status |
||
| 24 | |||
| 25 | if ($data->responseStatus != 200) { |
||
| 26 | if ($this->debug === true) { |
||
| 27 | $this->translation = "<font style='color:red;'>Error ".$data->responseStatus.': '.$data->responseDetails.'</font>'; |
||
| 28 | } |
||
| 29 | |||
| 30 | return; |
||
| 31 | } |
||
| 32 | |||
| 33 | $transObtained = $data->responseData->translatedText; |
||
| 34 | |||
| 35 | |||
| 36 | $this->translation = ucfirst(strtolower(trim(str_replace('*', ' ', $transObtained)))); |
||
| 37 | |||
| 38 | |||
| 39 | // Checking debug setting to determinate how to output translation |
||
| 40 | |||
| 41 | if ($this->debug === true) { |
||
| 42 | $errors = ''; |
||
| 43 | $words = explode(' ', $transObtained); |
||
| 44 | foreach ($words as $word) { |
||
| 45 | if ($word != '') { |
||
| 46 | if ($word[0] == '*') { |
||
| 47 | $errors = $errors.substr($word, 1).', '; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($errors == '') { |
||
| 53 | $this->translation = "<font style='color:#00CC00;'>".$this->translation.'</font>'; |
||
| 54 | } else { |
||
| 55 | $this->translation = "<font style='color:orange;'>Unknoun words: ".substr($errors, 0, -2).'</font>'; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | $this->checkSave(); |
||
| 60 | |||
| 61 | return; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 66 |