SecretKey   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 22.89 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 19
loc 83
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 17 3
A delete() 0 4 1
A get() 0 6 1
A getAll() 0 4 1
A _get() 19 19 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
// Copyright 1999-2021. Plesk International GmbH.
3
4
namespace PleskX\Api\Operator;
5
6
use PleskX\Api\Struct\SecretKey as Struct;
7
8
class SecretKey extends \PleskX\Api\Operator
9
{
10
    protected $_wrapperTag = 'secret_key';
11
12
    /**
13
     * @param string $ipAddress
14
     * @param string $description
15
     *
16
     * @return string
17
     */
18
    public function create($ipAddress = '', $description = '')
19
    {
20
        $packet = $this->_client->getPacket();
21
        $createTag = $packet->addChild($this->_wrapperTag)->addChild('create');
22
23
        if ('' !== $ipAddress) {
24
            $createTag->addChild('ip_address', $ipAddress);
25
        }
26
27
        if ('' !== $description) {
28
            $createTag->addChild('description', $description);
29
        }
30
31
        $response = $this->_client->request($packet);
32
33
        return (string) $response->key;
34
    }
35
36
    /**
37
     * @param string $keyId
38
     *
39
     * @return bool
40
     */
41
    public function delete($keyId)
42
    {
43
        return $this->_delete('key', $keyId, 'delete');
44
    }
45
46
    /**
47
     * @param string $keyId
48
     *
49
     * @return Struct\Info
50
     */
51
    public function get($keyId)
52
    {
53
        $items = $this->_get($keyId);
54
55
        return reset($items);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($items); of type PleskX\Api\Struct\SecretKey\Info|false adds false to the return on line 55 which is incompatible with the return type documented by PleskX\Api\Operator\SecretKey::get of type PleskX\Api\Struct\SecretKey\Info. It seems like you forgot to handle an error condition.
Loading history...
56
    }
57
58
    /**
59
     * @return Struct\Info[]
60
     */
61
    public function getAll()
62
    {
63
        return $this->_get();
64
    }
65
66
    /**
67
     * @param string|null $keyId
68
     *
69
     * @return Struct\Info[]
70
     */
71 View Code Duplication
    public function _get($keyId = null)
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...
72
    {
73
        $packet = $this->_client->getPacket();
74
        $getTag = $packet->addChild($this->_wrapperTag)->addChild('get_info');
75
76
        $filterTag = $getTag->addChild('filter');
77
        if (!is_null($keyId)) {
78
            $filterTag->addChild('key', $keyId);
79
        }
80
81
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
82
83
        $items = [];
84
        foreach ($response->xpath('//result/key_info') as $keyInfo) {
85
            $items[] = new Struct\Info($keyInfo);
86
        }
87
88
        return $items;
89
    }
90
}
91