Conditions | 23 |
Total Lines | 126 |
Code Lines | 78 |
Lines | 126 |
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.audits.AuditsMixin.modify_audit() 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 -*- |
||
266 | View Code Duplication | def modify_audit( |
|
267 | self, |
||
268 | audit_id: str, |
||
269 | *, |
||
270 | name: Optional[str] = None, |
||
271 | policy_id: Optional[str] = None, |
||
272 | target_id: Optional[str] = None, |
||
273 | scanner_id: Optional[str] = None, |
||
274 | alterable: Optional[bool] = None, |
||
275 | hosts_ordering: Optional[HostsOrdering] = None, |
||
276 | schedule_id: Optional[str] = None, |
||
277 | schedule_periods: Optional[int] = None, |
||
278 | comment: Optional[str] = None, |
||
279 | alert_ids: Optional[List[str]] = None, |
||
280 | observers: Optional[List[str]] = None, |
||
281 | preferences: Optional[dict] = None, |
||
282 | ) -> Any: |
||
283 | """Modifies an existing task. |
||
284 | |||
285 | Arguments: |
||
286 | audit_id: UUID of audit to modify. |
||
287 | name: The name of the audit. |
||
288 | policy_id: UUID of policy to use by the audit |
||
289 | target_id: UUID of target to be scanned |
||
290 | scanner_id: UUID of scanner to use for scanning the target |
||
291 | comment: The comment on the audit. |
||
292 | alert_ids: List of UUIDs for alerts to be applied to the audit |
||
293 | hosts_ordering: The order hosts are scanned in |
||
294 | schedule_id: UUID of a schedule when the audit should be run. |
||
295 | schedule_periods: A limit to the number of times the audit will be |
||
296 | scheduled, or 0 for no limit. |
||
297 | observers: List of names or ids of users which should be allowed to |
||
298 | observe this audit |
||
299 | preferences: Name/Value pairs of scanner preferences. |
||
300 | |||
301 | Returns: |
||
302 | The response. See :py:meth:`send_command` for details. |
||
303 | """ |
||
304 | if not audit_id: |
||
305 | raise RequiredArgument( |
||
306 | function=self.modify_task.__name__, argument='task_id argument' |
||
307 | ) |
||
308 | |||
309 | cmd = XmlCommand("modify_task") |
||
310 | cmd.set_attribute("task_id", audit_id) |
||
311 | |||
312 | if name: |
||
313 | cmd.add_element("name", name) |
||
314 | |||
315 | if comment: |
||
316 | cmd.add_element("comment", comment) |
||
317 | |||
318 | if policy_id: |
||
319 | cmd.add_element("config", attrs={"id": policy_id}) |
||
320 | |||
321 | if target_id: |
||
322 | cmd.add_element("target", attrs={"id": target_id}) |
||
323 | |||
324 | if alterable is not None: |
||
325 | cmd.add_element("alterable", to_bool(alterable)) |
||
326 | |||
327 | if hosts_ordering: |
||
328 | if not isinstance(hosts_ordering, HostsOrdering): |
||
329 | raise InvalidArgumentType( |
||
330 | function=self.modify_task.__name__, |
||
331 | argument='hosts_ordering', |
||
332 | arg_type=HostsOrdering.__name__, |
||
333 | ) |
||
334 | cmd.add_element("hosts_ordering", hosts_ordering.value) |
||
335 | |||
336 | if scanner_id: |
||
337 | cmd.add_element("scanner", attrs={"id": scanner_id}) |
||
338 | |||
339 | if schedule_id: |
||
340 | cmd.add_element("schedule", attrs={"id": schedule_id}) |
||
341 | |||
342 | if schedule_periods is not None: |
||
343 | if ( |
||
344 | not isinstance(schedule_periods, Integral) |
||
345 | or schedule_periods < 0 |
||
346 | ): |
||
347 | raise InvalidArgument( |
||
348 | "schedule_periods must be an integer greater or equal " |
||
349 | "than 0" |
||
350 | ) |
||
351 | cmd.add_element("schedule_periods", str(schedule_periods)) |
||
352 | |||
353 | if alert_ids is not None: |
||
354 | if not is_list_like(alert_ids): |
||
355 | raise InvalidArgumentType( |
||
356 | function=self.modify_task.__name__, |
||
357 | argument='alert_ids', |
||
358 | arg_type='list', |
||
359 | ) |
||
360 | |||
361 | if len(alert_ids) == 0: |
||
362 | cmd.add_element("alert", attrs={"id": "0"}) |
||
363 | else: |
||
364 | for alert in alert_ids: |
||
365 | cmd.add_element("alert", attrs={"id": str(alert)}) |
||
366 | |||
367 | if observers is not None: |
||
368 | if not is_list_like(observers): |
||
369 | raise InvalidArgumentType( |
||
370 | function=self.modify_task.__name__, |
||
371 | argument='observers', |
||
372 | arg_type='list', |
||
373 | ) |
||
374 | |||
375 | cmd.add_element("observers", to_comma_list(observers)) |
||
376 | |||
377 | if preferences is not None: |
||
378 | if not isinstance(preferences, Mapping): |
||
379 | raise InvalidArgumentType( |
||
380 | function=self.modify_task.__name__, |
||
381 | argument='preferences', |
||
382 | arg_type=Mapping.__name__, |
||
383 | ) |
||
384 | |||
385 | _xmlprefs = cmd.add_element("preferences") |
||
386 | for pref_name, pref_value in preferences.items(): |
||
387 | _xmlpref = _xmlprefs.add_element("preference") |
||
388 | _xmlpref.add_element("scanner_name", pref_name) |
||
389 | _xmlpref.add_element("value", str(pref_value)) |
||
390 | |||
391 | return self._send_xml_command(cmd) |
||
392 | |||
449 |