| Conditions | 36 |
| Total Lines | 113 |
| Code Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| 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 ospd_openvas.vthelper.VtHelper.get_single_vt() 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.
| 1 | # -*- coding: utf-8 -*- |
||
| 32 | def get_single_vt(self, vt_id: str, oids=None) -> Optional[Dict[str, any]]: |
||
| 33 | custom = self.nvti.get_nvt_metadata(vt_id) |
||
| 34 | |||
| 35 | if not custom: |
||
| 36 | return None |
||
| 37 | |||
| 38 | vt_params = custom.pop('vt_params') |
||
| 39 | vt_refs = custom.pop('refs') |
||
| 40 | name = custom.pop('name') |
||
| 41 | vt_creation_time = custom.pop('creation_date') |
||
| 42 | vt_modification_time = custom.pop('last_modification') |
||
| 43 | |||
| 44 | if oids: |
||
| 45 | vt_dependencies = list() |
||
| 46 | if 'dependencies' in custom: |
||
| 47 | deps = custom.pop('dependencies') |
||
| 48 | deps_list = deps.split(', ') |
||
| 49 | for dep in deps_list: |
||
| 50 | vt_dependencies.append(oids.get(dep)) |
||
| 51 | else: |
||
| 52 | vt_dependencies = None |
||
| 53 | |||
| 54 | summary = None |
||
| 55 | impact = None |
||
| 56 | affected = None |
||
| 57 | insight = None |
||
| 58 | solution = None |
||
| 59 | solution_t = None |
||
| 60 | solution_m = None |
||
| 61 | vuldetect = None |
||
| 62 | qod_t = None |
||
| 63 | qod_v = None |
||
| 64 | |||
| 65 | if 'summary' in custom: |
||
| 66 | summary = custom.pop('summary') |
||
| 67 | if 'impact' in custom: |
||
| 68 | impact = custom.pop('impact') |
||
| 69 | if 'affected' in custom: |
||
| 70 | affected = custom.pop('affected') |
||
| 71 | if 'insight' in custom: |
||
| 72 | insight = custom.pop('insight') |
||
| 73 | if 'solution' in custom: |
||
| 74 | solution = custom.pop('solution') |
||
| 75 | if 'solution_type' in custom: |
||
| 76 | solution_t = custom.pop('solution_type') |
||
| 77 | if 'solution_method' in custom: |
||
| 78 | solution_m = custom.pop('solution_method') |
||
| 79 | |||
| 80 | if 'vuldetect' in custom: |
||
| 81 | vuldetect = custom.pop('vuldetect') |
||
| 82 | if 'qod_type' in custom: |
||
| 83 | qod_t = custom.pop('qod_type') |
||
| 84 | elif 'qod' in custom: |
||
| 85 | qod_v = custom.pop('qod') |
||
| 86 | |||
| 87 | severity = dict() |
||
| 88 | if 'severity_base_vector' in custom: |
||
| 89 | severity_vector = custom.pop('severity_base_vector') |
||
| 90 | else: |
||
| 91 | severity_vector = custom.pop('cvss_base_vector') |
||
| 92 | severity['severity_base_vector'] = severity_vector |
||
| 93 | if 'severity_type' in custom: |
||
| 94 | severity_type = custom.pop('severity_type') |
||
| 95 | else: |
||
| 96 | severity_type = 'cvss_base_v2' |
||
| 97 | severity['severity_type'] = severity_type |
||
| 98 | if 'severity_origin' in custom: |
||
| 99 | severity['severity_origin'] = custom.pop('severity_origin') |
||
| 100 | |||
| 101 | if name is None: |
||
| 102 | name = '' |
||
| 103 | |||
| 104 | vt = {'name': name} |
||
| 105 | if custom is not None: |
||
| 106 | vt["custom"] = custom |
||
| 107 | if vt_params is not None: |
||
| 108 | vt["vt_params"] = vt_params |
||
| 109 | if vt_refs is not None: |
||
| 110 | vt["vt_refs"] = vt_refs |
||
| 111 | if vt_dependencies is not None: |
||
| 112 | vt["vt_dependencies"] = vt_dependencies |
||
| 113 | if vt_creation_time is not None: |
||
| 114 | vt["creation_time"] = vt_creation_time |
||
| 115 | if vt_modification_time is not None: |
||
| 116 | vt["modification_time"] = vt_modification_time |
||
| 117 | if summary is not None: |
||
| 118 | vt["summary"] = summary |
||
| 119 | if impact is not None: |
||
| 120 | vt["impact"] = impact |
||
| 121 | if affected is not None: |
||
| 122 | vt["affected"] = affected |
||
| 123 | if insight is not None: |
||
| 124 | vt["insight"] = insight |
||
| 125 | |||
| 126 | if solution is not None: |
||
| 127 | vt["solution"] = solution |
||
| 128 | if solution_t is not None: |
||
| 129 | vt["solution_type"] = solution_t |
||
| 130 | if solution_m is not None: |
||
| 131 | vt["solution_method"] = solution_m |
||
| 132 | |||
| 133 | if vuldetect is not None: |
||
| 134 | vt["detection"] = vuldetect |
||
| 135 | |||
| 136 | if qod_t is not None: |
||
| 137 | vt["qod_type"] = qod_t |
||
| 138 | elif qod_v is not None: |
||
| 139 | vt["qod"] = qod_v |
||
| 140 | |||
| 141 | if severity is not None: |
||
| 142 | vt["severities"] = severity |
||
| 143 | |||
| 144 | return vt |
||
| 145 | |||
| 188 |