Passed
Pull Request — master (#457)
by Jaspar
02:22
created

OverridesMixin.create_override()   D

Complexity

Conditions 12

Size

Total Lines 87
Code Lines 50

Duplication

Lines 87
Ratio 100 %

Importance

Changes 0
Metric Value
cc 12
eloc 50
nop 13
dl 87
loc 87
rs 4.8
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like gvm.protocols.gmpv214.entities.overrides.OverridesMixin.create_override() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
# -*- coding: utf-8 -*-
2
# Copyright (C) 2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
# pylint:  disable=redefined-builtin
20
# MAYBE we should change filter to filter_string (everywhere)
21
22
23
from typing import Any, List, Optional
24
25
from gvm.errors import RequiredArgument
26
from gvm.protocols.gmpv208.entities.overrides import (
27
    OverridesMixin as Gmp208OverridesMixin,
28
)
29
from gvm.protocols.gmpv208.entities.severity import Severity
30
from gvm.utils import deprecation, to_comma_list
31
from gvm.xml import XmlCommand
32
33
34
class OverridesMixin(Gmp208OverridesMixin):
35 View Code Duplication
    def create_override(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
36
        self,
37
        text: str,
38
        nvt_oid: str,
39
        *,
40
        days_active: Optional[int] = None,
41
        hosts: Optional[List[str]] = None,
42
        port: Optional[int] = None,
43
        result_id: Optional[str] = None,
44
        severity: Optional[Severity] = None,
45
        new_severity: Optional[Severity] = None,
46
        task_id: Optional[str] = None,
47
        threat: Any = None,
48
        new_threat: Any = None,
49
    ) -> Any:
50
        """Create a new override
51
52
        Arguments:
53
            text: Text of the new override
54
            nvt_id: OID of the nvt to which override applies
55
            days_active: Days override will be active. -1 on always, 0 off
56
            hosts: A list of host addresses
57
            port: Port to which the override applies
58
            result_id: UUID of a result to which override applies
59
            severity: Severity to which override applies
60
            new_severity: New severity for result
61
            task_id: UUID of task to which override applies
62
            threat: deprecated
63
            new_threat: deprecated
64
65
        Returns:
66
            The response. See :py:meth:`send_command` for details.
67
        """
68
        if not text:
69
            raise RequiredArgument(
70
                function=self.create_override.__name__, argument='text'
71
            )
72
73
        if not nvt_oid:
74
            raise RequiredArgument(
75
                function=self.create_override.__name__, argument='nvt_oid'
76
            )
77
78
        cmd = XmlCommand("create_override")
79
        cmd.add_element("text", text)
80
        cmd.add_element("nvt", attrs={"oid": nvt_oid})
81
82
        if days_active is not None:
83
            cmd.add_element("active", str(days_active))
84
85
        if hosts:
86
            cmd.add_element("hosts", to_comma_list(hosts))
87
88
        if port:
89
            cmd.add_element("port", str(port))
90
91
        if result_id:
92
            cmd.add_element("result", attrs={"id": result_id})
93
94
        if severity:
95
            cmd.add_element("severity", str(severity))
96
97
        if new_severity:
98
            cmd.add_element("new_severity", str(new_severity))
99
100
        if task_id:
101
            cmd.add_element("task", attrs={"id": task_id})
102
103
        if threat is not None:
104
            deprecation(
105
                "The threat parameter has been removed in GMP"
106
                " version {}{}".format(
107
                    self.get_protocol_version()[0],
108
                    self.get_protocol_version()[1],
109
                )
110
            )
111
112
        if new_threat is not None:
113
            deprecation(
114
                "The new_threat parameter has been removed in GMP"
115
                " version {}{}".format(
116
                    self.get_protocol_version()[0],
117
                    self.get_protocol_version()[1],
118
                )
119
            )
120
121
        return self._send_xml_command(cmd)
122
123 View Code Duplication
    def modify_override(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
124
        self,
125
        override_id: str,
126
        text: str,
127
        *,
128
        days_active: Optional[int] = None,
129
        hosts: Optional[List[str]] = None,
130
        port: Optional[int] = None,
131
        result_id: Optional[str] = None,
132
        severity: Optional[Severity] = None,
133
        new_severity: Optional[Severity] = None,
134
        task_id: Optional[str] = None,
135
        threat: Any = None,
136
        new_threat: Any = None,
137
    ) -> Any:
138
        """Modifies an existing override.
139
140
        Arguments:
141
            override_id: UUID of override to modify.
142
            text: The text of the override.
143
            days_active: Days override will be active. -1 on always,
144
                0 off.
145
            hosts: A list of host addresses
146
            port: Port to which override applies.
147
            result_id: Result to which override applies.
148
            severity: Severity to which override applies.
149
            new_severity: New severity score for result.
150
            task_id: Task to which override applies.
151
            threat: deprecated
152
            new_threat: deprecated
153
154
        Returns:
155
            The response. See :py:meth:`send_command` for details.
156
        """
157
        if not override_id:
158
            raise RequiredArgument(
159
                function=self.modify_override.__name__, argument='override_id'
160
            )
161
        if not text:
162
            raise RequiredArgument(
163
                function=self.modify_override.__name__, argument='text'
164
            )
165
166
        cmd = XmlCommand("modify_override")
167
        cmd.set_attribute("override_id", override_id)
168
        cmd.add_element("text", text)
169
170
        if days_active is not None:
171
            cmd.add_element("active", str(days_active))
172
173
        if hosts:
174
            cmd.add_element("hosts", to_comma_list(hosts))
175
176
        if port:
177
            cmd.add_element("port", str(port))
178
179
        if result_id:
180
            cmd.add_element("result", attrs={"id": result_id})
181
182
        if severity:
183
            cmd.add_element("severity", str(severity))
184
185
        if new_severity:
186
            cmd.add_element("new_severity", str(new_severity))
187
188
        if task_id:
189
            cmd.add_element("task", attrs={"id": task_id})
190
191
        if threat is not None:
192
            deprecation(
193
                "The threat parameter has been removed in GMP"
194
                " version {}{}".format(
195
                    self.get_protocol_version()[0],
196
                    self.get_protocol_version()[1],
197
                )
198
            )
199
200
        if new_threat is not None:
201
            deprecation(
202
                "The new_threat parameter has been removed in GMP"
203
                " version {}{}".format(
204
                    self.get_protocol_version()[0],
205
                    self.get_protocol_version()[1],
206
                )
207
            )
208
209
        return self._send_xml_command(cmd)
210