Completed
Push — master ( 39109f...3d39f4 )
by Tobias
10:10
created

Asset   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 14.41 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 17
loc 118
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 24 4
A tag() 17 17 3
C patch() 0 34 8

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
/*
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\Localise\Api;
11
12
use FAPI\Localise\Model\Asset\Asset as AssetModel;
13
use Psr\Http\Message\ResponseInterface;
14
use FAPI\Localise\Exception;
15
16
/**
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
class Asset extends HttpApi
20
{
21
    /**
22
     * Create an asset.
23
     * {@link https://localise.biz/api/docs/assets/createasset}.
24
     *
25
     * @param string $projectKey
26
     * @param string $id
27
     *
28
     * @return AssetModel|ResponseInterface
29
     *
30
     * @throws Exception
31
     */
32
    public function create(string $projectKey, string $id)
33
    {
34
        $param = [
35
            'name' => $id,
36
            'id' => $id,
37
            'type' => 'text',
38
            'default' => 'untranslated',
39
        ];
40
41
        $response = $this->httpPost(sprintf('/api/assets?key=%s', $projectKey), $param);
42
        if (!$this->hydrator) {
43
            return $response;
44
        }
45
46
        if (409 === $response->getStatusCode()) {
47
            throw Exception\Domain\AssetConflictException::create($id);
48
        }
49
50
        if (201 !== $response->getStatusCode()) {
51
            $this->handleErrors($response);
52
        }
53
54
        return $this->hydrator->hydrate($response, AssetModel::class);
55
    }
56
57
    /**
58
     * Tag an asset.
59
     * {@link https://localise.biz/api/docs/assets/tagasset}.
60
     *
61
     * @param string $projectKey
62
     * @param string $id
63
     * @param string $tag
64
     *
65
     * @return AssetModel|ResponseInterface
66
     *
67
     * @throws Exception\DomainException
68
     */
69 View Code Duplication
    public function tag(string $projectKey, string $id, string $tag)
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...
70
    {
71
        $param = [
72
            'name' => $tag,
73
        ];
74
75
        $response = $this->httpPost(sprintf('/api/assets/%s/tags?key=%s', $id, $projectKey), $param);
76
        if (!$this->hydrator) {
77
            return $response;
78
        }
79
80
        if ($response->getStatusCode() >= 400) {
81
            $this->handleErrors($response);
82
        }
83
84
        return $this->hydrator->hydrate($response, AssetModel::class);
85
    }
86
87
    /**
88
     * Patch an asset.
89
     * {@link https://localise.biz/api/docs/assets/patchasset}.
90
     *
91
     * @param string $projectKey
92
     * @param string $id
93
     * @param string $type
94
     * @param string $name
95
     * @param string $context
96
     * @param string $notes
97
     *
98
     * @return AssetModel|ResponseInterface
99
     *
100
     * @throws Exception
101
     */
102
    public function patch(string $projectKey, string $id, $type = null, $name = null, $context = null, $notes = null)
103
    {
104
        $param = [
105
            'id' => $id,
106
        ];
107
108
        if ($type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
109
            $param['type'] = $type;
110
        }
111
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
112
            $param['name'] = $name;
113
        }
114
        if ($context) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
115
            $param['context'] = $context;
116
        }
117
        if ($notes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $notes of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
118
            $param['notes'] = $notes;
119
        }
120
121
        $response = $this->httpPatch(sprintf('/api/assets/%s.json?key=%s', $id, $projectKey), $param);
122
        if (!$this->hydrator) {
123
            return $response;
124
        }
125
126
        if (409 === $response->getStatusCode()) {
127
            throw Exception\Domain\AssetConflictException::create($id);
128
        }
129
130
        if ($response->getStatusCode() >= 400) {
131
            $this->handleErrors($response);
132
        }
133
134
        return $this->hydrator->hydrate($response, AssetModel::class);
135
    }
136
}
137