ApertiumTrans::main()   C
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 6.2727
c 0
b 0
f 0
cc 9
eloc 25
nc 6
nop 0

How to fix   Long Method   

Long Method

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:

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