Completed
Push — master ( 52721c...da306e )
by Jaspar
18s queued 14s
created

NotesMixin.modify_note()   C

Complexity

Conditions 10

Size

Total Lines 71
Code Lines 40

Duplication

Lines 71
Ratio 100 %

Importance

Changes 0
Metric Value
cc 10
eloc 40
nop 11
dl 71
loc 71
rs 5.9999
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.notes.NotesMixin.modify_note() 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
from typing import Any, List, Optional
23
24
from gvm.errors import RequiredArgument
25
from gvm.protocols.gmpv208.entities.notes import NotesMixin as Gmp208NotesMixin
26
from gvm.protocols.gmpv208.entities.severity import Severity
27
from gvm.utils import deprecation, to_comma_list
28
from gvm.xml import XmlCommand
29
30
31
class NotesMixin(Gmp208NotesMixin):
32 View Code Duplication
    def create_note(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
33
        self,
34
        text: str,
35
        nvt_oid: str,
36
        *,
37
        days_active: Optional[int] = None,
38
        hosts: Optional[List[str]] = None,
39
        port: Optional[int] = None,
40
        result_id: Optional[str] = None,
41
        severity: Optional[Severity] = None,
42
        task_id: Optional[str] = None,
43
        threat: Any = None,
44
    ) -> Any:
45
        """Create a new note
46
47
        Arguments:
48
            text: Text of the new note
49
            nvt_id: OID of the nvt to which note applies
50
            days_active: Days note will be active. -1 on
51
                always, 0 off
52
            hosts: A list of hosts addresses
53
            port: Port to which the note applies
54
            result_id: UUID of a result to which note applies
55
            severity: Severity to which note applies
56
            task_id: UUID of task to which note applies
57
            threat: deprecated
58
59
        Returns:
60
            The response. See :py:meth:`send_command` for details.
61
        """
62
        if not text:
63
            raise RequiredArgument(
64
                function=self.create_note.__name__, argument='text'
65
            )
66
67
        if not nvt_oid:
68
            raise RequiredArgument(
69
                function=self.create_note.__name__, argument='nvt_oid'
70
            )
71
72
        cmd = XmlCommand("create_note")
73
        cmd.add_element("text", text)
74
        cmd.add_element("nvt", attrs={"oid": nvt_oid})
75
76
        if days_active is not None:
77
            cmd.add_element("active", str(days_active))
78
79
        if hosts:
80
            cmd.add_element("hosts", to_comma_list(hosts))
81
82
        if port:
83
            cmd.add_element("port", str(port))
84
85
        if result_id:
86
            cmd.add_element("result", attrs={"id": result_id})
87
88
        if severity:
89
            cmd.add_element("severity", str(severity))
90
91
        if task_id:
92
            cmd.add_element("task", attrs={"id": task_id})
93
94
        if threat is not None:
95
            deprecation(
96
                "The threat parameter has been removed in GMP"
97
                " version {}{}".format(
98
                    self.get_protocol_version()[0],
99
                    self.get_protocol_version()[1],
100
                )
101
            )
102
103
        return self._send_xml_command(cmd)
104
105 View Code Duplication
    def modify_note(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
106
        self,
107
        note_id: str,
108
        text: str,
109
        *,
110
        days_active: Optional[int] = None,
111
        hosts: Optional[List[str]] = None,
112
        port: Optional[int] = None,
113
        result_id: Optional[str] = None,
114
        severity: Optional[Severity] = None,
115
        task_id: Optional[str] = None,
116
        threat: Any = None,
117
    ) -> Any:
118
        """Modifies an existing note.
119
120
        Arguments:
121
            note_id: UUID of note to modify.
122
            text: The text of the note.
123
            days_active: Days note will be active. -1 on always, 0 off.
124
            hosts: A list of hosts addresses
125
            port: Port to which note applies.
126
            result_id: Result to which note applies.
127
            severity: Severity to which note applies.
128
            task_id: Task to which note applies.
129
            threat: deprecated
130
131
        Returns:
132
            The response. See :py:meth:`send_command` for details.
133
        """
134
        if not note_id:
135
            raise RequiredArgument(
136
                function=self.modify_note.__name__, argument='note_id'
137
            )
138
139
        if not text:
140
            raise RequiredArgument(
141
                function=self.modify_note.__name__, argument='text'
142
            )
143
144
        cmd = XmlCommand("modify_note")
145
        cmd.set_attribute("note_id", note_id)
146
        cmd.add_element("text", text)
147
148
        if days_active is not None:
149
            cmd.add_element("active", str(days_active))
150
151
        if hosts:
152
            cmd.add_element("hosts", to_comma_list(hosts))
153
154
        if port:
155
            cmd.add_element("port", str(port))
156
157
        if result_id:
158
            cmd.add_element("result", attrs={"id": result_id})
159
160
        if severity:
161
            cmd.add_element("severity", str(severity))
162
163
        if task_id:
164
            cmd.add_element("task", attrs={"id": task_id})
165
166
        if threat is not None:
167
            deprecation(
168
                "The threat parameter has been removed in GMP"
169
                " version {}{}".format(
170
                    self.get_protocol_version()[0],
171
                    self.get_protocol_version()[1],
172
                )
173
            )
174
175
        return self._send_xml_command(cmd)
176