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