Conditions | 14 |
Total Lines | 90 |
Code Lines | 52 |
Lines | 90 |
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.gmpv208.entities.overrides.OverridesMixin.modify_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 -*- |
||
215 | View Code Duplication | def modify_override( |
|
216 | self, |
||
217 | override_id: str, |
||
218 | text: str, |
||
219 | *, |
||
220 | days_active: Optional[int] = None, |
||
221 | hosts: Optional[List[str]] = None, |
||
222 | port: Optional[int] = None, |
||
223 | result_id: Optional[str] = None, |
||
224 | severity: Optional[Severity] = None, |
||
225 | new_severity: Optional[Severity] = None, |
||
226 | task_id: Optional[str] = None, |
||
227 | threat: Optional[SeverityLevel] = None, |
||
228 | new_threat: Optional[SeverityLevel] = None, |
||
229 | ) -> Any: |
||
230 | """Modifies an existing override. |
||
231 | |||
232 | Arguments: |
||
233 | override_id: UUID of override to modify. |
||
234 | text: The text of the override. |
||
235 | days_active: Days override will be active. -1 on always, |
||
236 | 0 off. |
||
237 | hosts: A list of host addresses |
||
238 | port: Port to which override applies. |
||
239 | result_id: Result to which override applies. |
||
240 | severity: Severity to which override applies. |
||
241 | new_severity: New severity score for result. |
||
242 | task_id: Task to which override applies. |
||
243 | threat: Threat level to which override applies. |
||
244 | Will be converted to severity. |
||
245 | new_threat: New threat level for results. Will be converted to |
||
246 | new_severity. |
||
247 | |||
248 | Returns: |
||
249 | The response. See :py:meth:`send_command` for details. |
||
250 | """ |
||
251 | if not override_id: |
||
252 | raise RequiredArgument( |
||
253 | function=self.modify_override.__name__, argument='override_id' |
||
254 | ) |
||
255 | if not text: |
||
256 | raise RequiredArgument( |
||
257 | function=self.modify_override.__name__, argument='text' |
||
258 | ) |
||
259 | |||
260 | cmd = XmlCommand("modify_override") |
||
261 | cmd.set_attribute("override_id", override_id) |
||
262 | cmd.add_element("text", text) |
||
263 | |||
264 | if days_active is not None: |
||
265 | cmd.add_element("active", str(days_active)) |
||
266 | |||
267 | if hosts: |
||
268 | cmd.add_element("hosts", to_comma_list(hosts)) |
||
269 | |||
270 | if port: |
||
271 | cmd.add_element("port", str(port)) |
||
272 | |||
273 | if result_id: |
||
274 | cmd.add_element("result", attrs={"id": result_id}) |
||
275 | |||
276 | if severity: |
||
277 | cmd.add_element("severity", str(severity)) |
||
278 | |||
279 | if new_severity: |
||
280 | cmd.add_element("new_severity", str(new_severity)) |
||
281 | |||
282 | if task_id: |
||
283 | cmd.add_element("task", attrs={"id": task_id}) |
||
284 | |||
285 | if threat is not None: |
||
286 | if not isinstance(threat, SeverityLevel): |
||
287 | raise InvalidArgumentType( |
||
288 | function=self.modify_override.__name__, |
||
289 | argument='threat', |
||
290 | arg_type=SeverityLevel.__name__, |
||
291 | ) |
||
292 | cmd.add_element("threat", threat.value) |
||
293 | |||
294 | if new_threat is not None: |
||
295 | if not isinstance(new_threat, SeverityLevel): |
||
296 | raise InvalidArgumentType( |
||
297 | function=self.modify_override.__name__, |
||
298 | argument='new_threat', |
||
299 | arg_type=SeverityLevel.__name__, |
||
300 | ) |
||
301 | |||
302 | cmd.add_element("new_threat", new_threat.value) |
||
303 | |||
304 | return self._send_xml_command(cmd) |
||
305 |