Nlp   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 88
Duplicated Lines 29.55 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 26
loc 88
ccs 34
cts 35
cp 0.9714
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A config() 0 9 1
A isValidConfigs() 0 15 4
A isBool() 8 8 3
A isString() 8 8 3
A isValidNBest() 0 6 5
A getAllowedConfigKeys() 10 10 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
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Api;
6
7
use Kerox\Messenger\Exception\InvalidKeyException;
8
use Kerox\Messenger\Exception\InvalidTypeException;
9
use Kerox\Messenger\NlpInterface;
10
use Kerox\Messenger\Request\NlpRequest;
11
use Kerox\Messenger\Response\NlpResponse;
12
13
class Nlp extends AbstractApi implements NlpInterface
14
{
15
    /**
16
     * @throws \Kerox\Messenger\Exception\MessengerException
17
     */
18 5
    public function config(array $configs = []): NlpResponse
19
    {
20 5
        $this->isValidConfigs($configs);
21
22 1
        $request = new NlpRequest($this->pageToken, $configs);
23 1
        $response = $this->client->post('me/nlp_configs', $request->build());
24
25 1
        return new NlpResponse($response);
26
    }
27
28
    /**
29
     * @throws \Kerox\Messenger\Exception\MessengerException
30
     */
31 5
    private function isValidConfigs(array $configs): void
32
    {
33 5
        $allowedConfigKeys = $this->getAllowedConfigKeys();
34 5
        if (!empty($configs)) {
35 5
            foreach ($configs as $key => $value) {
36 5
                if (!\in_array($key, $allowedConfigKeys, true)) {
37 1
                    throw new InvalidKeyException(sprintf('%s is not a valid key. configs must only contain "%s".', $key, implode(', ', $allowedConfigKeys)));
38
                }
39
40 4
                $this->isBool($key, $value);
41 3
                $this->isString($key, $value);
42 3
                $this->isValidNBest($key, $value);
43
            }
44
        }
45 1
    }
46
47
    /**
48
     * @param int|string $key
49
     * @param mixed      $value
50
     *
51
     * @throws \Kerox\Messenger\Exception\MessengerException
52
     */
53 4 View Code Duplication
    private function isBool($key, $value): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
54
    {
55 4
        if (!\is_bool($value) &&
56 4
            \in_array($key, [self::CONFIG_KEY_NLP_ENABLED, self::CONFIG_KEY_VERBOSE], true)
57
        ) {
58 2
            throw new InvalidTypeException(sprintf('%s must be a boolean.', $key));
59
        }
60 3
    }
61
62
    /**
63
     * @param int|string $key
64
     * @param mixed      $value
65
     *
66
     * @throws \Kerox\Messenger\Exception\MessengerException
67
     */
68 3 View Code Duplication
    private function isString($key, $value): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70 3
        if (!\is_string($value) &&
71 3
            \in_array($key, [self::CONFIG_KEY_CUSTOM_TOKEN, self::CONFIG_KEY_MODEL], true)
72
        ) {
73
            throw new InvalidTypeException(sprintf('%s must be a string.', $key));
74
        }
75 3
    }
76
77
    /**
78
     * @param int|string $key
79
     * @param mixed      $value
80
     *
81
     * @throws \Kerox\Messenger\Exception\MessengerException
82
     */
83 3
    private function isValidNBest($key, $value): void
84
    {
85 3
        if ($key === self::CONFIG_KEY_N_BEST && (!\is_int($value) || $value < 1 || $value > 8)) {
86 1
            throw new InvalidTypeException(sprintf('%s must be an integer between 1 and 8.', $key));
87
        }
88 3
    }
89
90 5 View Code Duplication
    private function getAllowedConfigKeys(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
91
    {
92
        return [
93 5
            self::CONFIG_KEY_NLP_ENABLED,
94 5
            self::CONFIG_KEY_MODEL,
95 5
            self::CONFIG_KEY_CUSTOM_TOKEN,
96 5
            self::CONFIG_KEY_VERBOSE,
97 5
            self::CONFIG_KEY_N_BEST,
98
        ];
99
    }
100
}
101