Completed
Push — master ( 0bc15a...82e2b9 )
by Aitor Riba
08:05
created

Translation   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 275
Duplicated Lines 12.36 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 0
dl 34
loc 275
rs 8.8
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 3
A setDebug() 0 6 1
A setFromLang() 0 6 1
A setToLang() 0 6 1
A setTranslator() 0 6 1
C run() 0 35 7
C mymemoryTrans() 17 65 11
C apertiumTrans() 17 61 10
A __toString() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Aitor24\Laralang;
4
5
use App;
6
7
class Translation
8
{
9
    /**
10
     * Setup public vars.
11
     */
12
    public $translator;
13
    public $translation;
14
    public $string;
15
    public $debug;
16
    public $from;
17
    public $to;
18
19
    /**
20
     * Setup default values.
21
     *
22
     * @param string $string
23
     * @param string $from
0 ignored issues
show
Bug introduced by
There is no parameter named $from. Was it maybe removed?

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 $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
24
     * @param string $to
0 ignored issues
show
Bug introduced by
There is no parameter named $to. Was it maybe removed?

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 $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
25
     */
26
    public function __construct($string)
27
    {
28
        $this->translator = config('laralang.default.translator');
29
        $this->debug = config('laralang.default.debug');
30
        $this->from = config('laralang.default.from_lang');
31
        $this->to = config('laralang.default.to_lang');
32
        $this->string = $string;
33
        $this->translation = $string;
34
35
36
        // Checking whether from_lang or to_lang are set as app_locale.
37
38
        if ($this->from == 'app_locale') {$this->from = App::getLocale();}
39
40
        if ($this->to == 'app_locale') {$this->to = App::getLocale();}
41
    }
42
43
44
    /**
45
     * Setup debug value
46
     *
47
     * @param boolean $debug
48
     */
49
50
    public function setDebug($debug)
51
    {
52
        $this->debug = $debug;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Setup fromLang value
59
     *
60
     * @param string $lang
61
     */
62
63
    public function setFromLang($lang)
64
    {
65
        $this->from = $lang;
66
67
        return $this;
68
    }
69
70
    /**
71
     * Setup tolang value
72
     *
73
     * @param string $lang
74
     */
75
76
    public function setToLang($lang)
77
    {
78
        $this->to = $lang;
79
80
        return $this;
81
    }
82
83
84
    /**
85
     * Setup translator
86
     *
87
     * @param string $translator
88
     */
89
90
    public function setTranslator($translator)
91
    {
92
        $this->translator = $translator;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Main function of the class
99
     *
100
     * Check what translator must select
101
     *
102
     */
103
104
    public function run()
105
    {
106
107
        // Return the value if the language is the same.
108
109
        if ($this->from == $this->to) {
110
            if ($this->debug == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
111
                $this->translation = "<font style='color:orange;'>Same in <> out languge</font>";
112
            }
113
114
            return;
115
        }
116
117
118
        $available_transoltors = ['apertium', 'mymemory'];
119
120
        // Checks available translators.
121
122
        if (in_array($this->translator, $available_transoltors) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
123
            if ($this->debug == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
124
                $this->translation = "<font style='color:red;'>Not suported translator: ".$this->translator."</font>";
125
            }
126
127
            return;
128
        }
129
130
        if ($this->translator == 'apertium') {
131
            return $this->apertiumTrans();
132
        }
133
134
        if ($this->translator == 'mymemory') {
135
            return $this->mymemoryTrans();
136
        }
137
138
    }
139
140
    /**
141
     * Get translation from mymemory API.
142
     */
143
     public function mymemoryTrans()
144
     {
145
         // Check if it can be translated from online sources.
146
147
         $host = 'api.mymemory.translated.net';
148
         if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
149
150
             // Host online
151
             $urlString = urlencode($this->string);
152
             $url = "http://$host/get?q=$urlString&langpair=$this->from%7C$this->to";
153
             $json = file_get_contents($url);
154
             $data = json_decode($json);
155
156
             // Checking response status
157
158
             if ($data->responseStatus != 200) {
159
                 if ($this->debug == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
160
                     $details = $data->responseDetails;
161
                     if ($data->responseStatus == 403) {
162
                         $details =($data->responseDetails);
163
                     }
164
                     $this->translation = "<font style='color:red;'>Error ".$data->responseStatus.": ".$details."</font>";
165
                 }
166
167
                 return;
168
             }
169
170
             $transObtained = $data->responseData->translatedText;
171
172
             $this->translation = ucfirst(strtolower(trim($transObtained)));
173
174
175
             // Checking debug setting to determinate how to output translation
176
177 View Code Duplication
             if ($this->debug == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
                 $errors = '';
179
                 $words = explode(' ', $transObtained);
180
                 foreach ($words as $word) {
181
                     if ($word != '') {
182
                         if ($word[0] == '*') {
183
                             $errors = $errors.substr($word, 1).', ';
184
                         }
185
                     }
186
                 }
187
188
                 if ($errors == '') {
189
                     $this->translation = "<font style='color:#00CC00;'>".$this->translation."</font>";
190
                 } else {
191
                     $this->translation = "<font style='color:orange;'>Unknoun words: ".substr($errors, 0, -2)."</font>";
192
                 }
193
             }
194
195
             fclose($socket);
196
             return;
197
198
         } else {
199
200
             // host offline
201
202
             if ($this->debug == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
203
                 $this->translation = "<font style='color:red;'>Mymeory host is down</font>";
204
             }
205
             return;
206
         }
207
     }
208
209
    /**
210
     * Get translation from apertium API.
211
     */
212
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) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
269
                $this->translation = "<font style='color:red;'>Apertium host is down</font>";
270
            }
271
            return;
272
        }
273
    }
274
275
    public function __toString()
276
    {
277
        $this->run();
278
279
        return $this->translation;
280
    }
281
}
282