Passed
Pull Request — master (#31)
by
unknown
02:34
created

DeepL::translate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 10
Bugs 1 Features 3
Metric Value
cc 2
eloc 19
c 10
b 1
f 3
nc 2
nop 11
dl 0
loc 38
ccs 12
cts 12
cp 1
crap 2
rs 9.6333

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace BabyMarkt\DeepL;
4
5
use InvalidArgumentException;
6
7
/**
8
 * DeepL API client library
9
 *
10
 * @package BabyMarkt\DeepL
11
 */
12
class DeepL
13
{
14
    /**
15
     * API URL: usage
16
     */
17
    const API_URL_RESOURCE_USAGE = 'usage';
18
19
    /**
20
     * API URL: languages
21
     */
22
    const API_URL_RESOURCE_LANGUAGES = 'languages';
23
24
    /**
25
     * @var ClientInterface
26
     */
27
    private $client;
28
29
    public function __construct($authKey, $apiVersion = 2, $host = 'api.deepl.com', ClientInterface $client = null)
30
    {
31
        $this->client = $client ?? new Client($authKey, $apiVersion, $host);
32
    }
33
34
    /**
35
     * Call languages-Endpoint and return Json-response as an Array
36
     *
37
     * @param string $type
38
     *
39
     * @return array
40
     *
41
     * @throws DeepLException
42
     */
43
    public function languages($type = null)
44
    {
45
        $url       = $this->client->buildBaseUrl(self::API_URL_RESOURCE_LANGUAGES);
46
        $body      = $this->client->buildQuery(array('type' => $type));
47
        $languages = $this->client->request($url, $body);
48
49
        return $languages;
50
    }
51
52
    /**
53
     * Translate the text string or array from source to destination language
54
     * For detailed info on Parameters see README.md
55
     *
56
     * @param string|string[] $text
57
     * @param string          $sourceLang
58
     * @param string          $targetLang
59
     * @param string          $tagHandling
60
     * @param array|null      $ignoreTags
61
     * @param string          $formality
62
     * @param null            $splitSentences
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $splitSentences is correct as it would always require null to be passed?
Loading history...
63
     * @param null            $preserveFormatting
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $preserveFormatting is correct as it would always require null to be passed?
Loading history...
64
     * @param array|null      $nonSplittingTags
65
     * @param null            $outlineDetection
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $outlineDetection is correct as it would always require null to be passed?
Loading history...
66
     * @param array|null      $splittingTags
67
     *
68
     * @return array
69
     *
70
     * @throws DeepLException
71
     *
72
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
73
     */
74
    public function translate(
75
        $text,
76
        $sourceLang = 'de',
77
        $targetLang = 'en',
78
        $tagHandling = null,
79
        array $ignoreTags = null,
80
        $formality = 'default',
81
        $splitSentences = null,
82
        $preserveFormatting = null,
83
        array $nonSplittingTags = null,
84
        $outlineDetection = null,
85 31
        array $splittingTags = null
86
    ) {
87 31
        if (is_array($tagHandling)) {
0 ignored issues
show
introduced by
The condition is_array($tagHandling) is always false.
Loading history...
88 31
            throw new InvalidArgumentException('$tagHandling must be of type String in V2 of DeepLLibrary');
89 31
        }
90 31
        $paramsArray = array(
91
            'text'                => $text,
92 31
            'source_lang'         => $sourceLang,
93 31
            'target_lang'         => $targetLang,
94
            'splitting_tags'      => $splittingTags,
95
            'non_splitting_tags'  => $nonSplittingTags,
96
            'ignore_tags'         => $ignoreTags,
97
            'tag_handling'        => $tagHandling,
98 31
            'formality'           => $formality,
99
            'split_sentences'     => $splitSentences,
100 31
            'preserve_formatting' => $preserveFormatting,
101 31
            'outline_detection'   => $outlineDetection,
102 31
        );
103 31
104
        $paramsArray = $this->removeEmptyParams($paramsArray);
105
        $url         = $this->client->buildBaseUrl();
106
        $body        = $this->client->buildQuery($paramsArray);
107
108
        // request the DeepL API
109
        $translationsArray = $this->client->request($url, $body);
110
111
        return $translationsArray['translations'];
112
    }
113 4
114
    /**
115 4
     * Calls the usage-Endpoint and return Json-response as an array
116 4
     *
117 4
     * @return array
118
     *
119 3
     * @throws DeepLException
120
     */
121
    public function usage()
122
    {
123
        $url   = $this->client->buildBaseUrl(self::API_URL_RESOURCE_USAGE);
124
        $usage = $this->client->request($url);
125
126
        return $usage;
127
    }
128
129
    /**
130
     * @param array $paramsArray
131
     *
132
     * @return array
133
     */
134
    private function removeEmptyParams($paramsArray)
135
    {
136
        foreach ($paramsArray as $key => $value) {
137
            if (true === empty($value)) {
138
                unset($paramsArray[$key]);
139
            }
140
            // Special Workaround for outline_detection which will be unset above
141
            // DeepL assumes outline_detection=1 if it is not send
142
            // in order to deactivate it, we need to send outline_detection=0 to the api
143
            if ('outline_detection' === $key) {
144
                if (1 === $value) {
145
                    unset($paramsArray[$key]);
146
                }
147
148 1
                if (0 === $value) {
149
                    $paramsArray[$key] = 0;
150 1
                }
151 1
            }
152
        }
153
154
        return $paramsArray;
155
    }
156
}
157