Conditions | 12 |
Total Lines | 87 |
Code Lines | 50 |
Lines | 87 |
Ratio | 100 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
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.
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 -*- |
||
35 | View Code Duplication | def create_override( |
|
|
|||
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 | |||
210 |