Passed
Push — master ( b89d31...0815d7 )
by Thomas
02:46
created

EasyNmtHelper::translate()   C

Complexity

Conditions 12
Paths 73

Size

Total Lines 64
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 39
c 1
b 0
f 0
nc 73
nop 3
dl 0
loc 64
rs 6.9666

How to fix   Long Method    Complexity   

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 LeKoala\Multilingual;
4
5
/**
6
 * A simple helper to help translating strings
7
 * @link https://github.com/UKPLab/EasyNMT/tree/main/docker
8
 */
9
class EasyNmtHelper
10
{
11
    public static $baseUrl = 'http://localhost:24080/translate';
12
    public static $defaultLanguage = 'en';
13
14
    /**
15
     * Translate from provider
16
     *
17
     * @param string $sourceText
18
     * @param string $targetLang
19
     * @param string $sourceLang
20
     * @return string
21
     */
22
    public static function translate($sourceText, $targetLang = null, $sourceLang = null)
0 ignored issues
show
Unused Code introduced by
The parameter $sourceLang is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

22
    public static function translate($sourceText, $targetLang = null, /** @scrutinizer ignore-unused */ $sourceLang = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    {
24
        $src_lang = self::$defaultLanguage;
25
        if ($targetLang == $src_lang) {
26
            return $sourceText;
27
        }
28
29
        $orgText = $sourceText;
30
        $containsVar = str_contains($sourceText, '{');
31
32
        $extractedVars = [];
33
        if ($containsVar) {
34
            $matches = [];
35
            preg_match_all('/(\{[\w_]*?\})/', $sourceText, $matches);
36
            $extractedVars = $matches[0] ?? [];
37
        }
38
39
        // use en as intermediate language
40
        if ($targetLang != 'en') {
41
            $params2 = [
42
                'target_lang' => 'en',
43
                'text' => $sourceText
44
            ];
45
            $url = self::$baseUrl . '?' . http_build_query($params2);
46
            $result = @file_get_contents($url);
47
            if ($result) {
48
                $jsonData = json_decode($result, 1);
49
                if ($jsonData) {
50
                    $sourceText = $jsonData['translated'][0] ?? $sourceText;
51
                }
52
            }
53
            $src_lang = 'en';
54
        }
55
56
        $params = [
57
            'target_lang' => $targetLang,
58
            'text' => $sourceText,
59
            'source_lang' => $src_lang,
60
        ];
61
62
        $url = 'http://localhost:24080/translate?' . http_build_query($params);
63
        $result = @file_get_contents($url);
64
        if ($result) {
65
            $jsonData = json_decode($result, 1);
66
            if ($jsonData) {
67
                $sourceText = $jsonData['translated'][0] ?? $sourceText;
68
            }
69
        }
70
71
        // It has replaced all the {}, not good!
72
        if ($containsVar && !str_contains($sourceText, '{')) {
73
            return $orgText;
74
        }
75
76
        // Make sure we keep placeholders
77
        if ($containsVar) {
78
            preg_match_all('/(\{[\w_]*?\})/', $sourceText, $matches);
79
            $newVars = $matches[0] ?? [];
80
            foreach ($newVars as $idx => $v) {
81
                $sourceText = str_replace($v, $extractedVars[$idx], $sourceText);
82
            }
83
        }
84
85
        return $sourceText;
86
    }
87
}
88