Key   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 15.84 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 16
loc 101
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 16 16 4
B search() 0 32 8
A delete() 0 14 3

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
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license.  See the LICENSE file for details.
8
 */
9
10
namespace FAPI\PhraseApp\Api;
11
12
use FAPI\PhraseApp\Exception\DomainException;
13
use FAPI\PhraseApp\Model\Key\KeyCreated;
14
use FAPI\PhraseApp\Model\Key\KeySearchResults;
15
use Psr\Http\Client\ClientExceptionInterface;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * @author Sascha-Oliver Prolic <[email protected]>
20
 */
21
class Key extends HttpApi
22
{
23
    /**
24
     * Create a new key.
25
     *
26
     * @param string $projectKey
27
     * @param string $name
28
     * @param array  $params
29
     *
30
     * @throws DomainException
31
     * @throws ClientExceptionInterface
32
     *
33
     * @return KeyCreated|ResponseInterface
34
     */
35 View Code Duplication
    public function create(string $projectKey, string $name, array $params = [])
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...
36
    {
37
        $params['name'] = $name;
38
39
        $response = $this->httpPost(sprintf('/api/v2/projects/%s/keys', $projectKey), $params);
40
41
        if (!$this->hydrator) {
42
            return $response;
43
        }
44
45
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
46
            $this->handleErrors($response);
47
        }
48
49
        return $this->hydrator->hydrate($response, KeyCreated::class);
50
    }
51
52
    /**
53
     * Search keys.
54
     *
55
     * @param string $projectKey
56
     * @param array  $params
57
     *
58
     * @throws ClientExceptionInterface
59
     * @throws DomainException
60
     *
61
     * @return KeySearchResults|ResponseInterface
62
     */
63
    public function search(string $projectKey, array $params = [])
64
    {
65
        $q = '';
66
67
        if (isset($params['tags'])) {
68
            $q .= 'tags:'.$params['tags'].' ';
69
        }
70
71
        if (isset($params['name'])) {
72
            $q .= 'name:'.$params['name'].' ';
73
        }
74
75
        if (isset($params['ids'])) {
76
            $q .= 'ids:'.$params['ids'].' ';
77
        }
78
79
        if (!empty($q)) {
80
            $params['q'] = $q;
81
        }
82
83
        $response = $this->httpPost(sprintf('/api/v2/projects/%s/keys/search', $projectKey), $params);
84
85
        if (!$this->hydrator) {
86
            return $response;
87
        }
88
89
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
90
            $this->handleErrors($response);
91
        }
92
93
        return $this->hydrator->hydrate($response, KeySearchResults::class);
94
    }
95
96
    /**
97
     * Delete a key.
98
     *
99
     * @param string $projectKey
100
     * @param string $keyId
101
     *
102
     * @throws ClientExceptionInterface
103
     * @throws DomainException
104
     *
105
     * @return bool|ResponseInterface
106
     */
107
    public function delete(string $projectKey, string $keyId)
108
    {
109
        $response = $this->httpDelete(sprintf('/api/v2/projects/%s/keys/%s', $projectKey, $keyId));
110
111
        if (!$this->hydrator) {
112
            return $response;
113
        }
114
115
        if ($response->getStatusCode() !== 204) {
116
            $this->handleErrors($response);
117
        }
118
119
        return true;
120
    }
121
}
122