Conditions | 37 |
Total Lines | 120 |
Code Lines | 99 |
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 in deps_list: |
||
51 | vt_dependencies.append(oids.get(dep)) |
||
52 | else: |
||
53 | vt_dependencies = None |
||
54 | |||
55 | summary = None |
||
56 | impact = None |
||
57 | affected = None |
||
58 | insight = None |
||
59 | solution = None |
||
60 | solution_t = None |
||
61 | solution_m = None |
||
62 | vuldetect = None |
||
63 | qod_t = None |
||
64 | qod_v = None |
||
65 | |||
66 | if 'summary' in custom: |
||
67 | summary = custom.pop('summary') |
||
68 | if 'impact' in custom: |
||
69 | impact = custom.pop('impact') |
||
70 | if 'affected' in custom: |
||
71 | affected = custom.pop('affected') |
||
72 | if 'insight' in custom: |
||
73 | insight = custom.pop('insight') |
||
74 | if 'solution' in custom: |
||
75 | solution = custom.pop('solution') |
||
76 | if 'solution_type' in custom: |
||
77 | solution_t = custom.pop('solution_type') |
||
78 | if 'solution_method' in custom: |
||
79 | solution_m = custom.pop('solution_method') |
||
80 | |||
81 | if 'vuldetect' in custom: |
||
82 | vuldetect = custom.pop('vuldetect') |
||
83 | if 'qod_type' in custom: |
||
84 | qod_t = custom.pop('qod_type') |
||
85 | elif 'qod' in custom: |
||
86 | qod_v = custom.pop('qod') |
||
87 | |||
88 | severity = dict() |
||
89 | if 'severity_vector' in custom: |
||
90 | severity_vector = custom.pop('severity_vector') |
||
91 | else: |
||
92 | severity_vector = custom.pop('cvss_base_vector') |
||
93 | severity['severity_base_vector'] = severity_vector |
||
94 | |||
95 | if "CVSS:3" in severity_vector: |
||
96 | severity_type = 'cvss_base_v3' |
||
97 | else: |
||
98 | severity_type = 'cvss_base_v2' |
||
99 | severity['severity_type'] = severity_type |
||
100 | |||
101 | if 'severity_date' in custom: |
||
102 | severity['severity_date'] = custom.pop('severity_date') |
||
103 | else: |
||
104 | severity['severity_date'] = vt_creation_time |
||
105 | |||
106 | if 'severity_origin' in custom: |
||
107 | severity['severity_origin'] = custom.pop('severity_origin') |
||
108 | |||
109 | if name is None: |
||
110 | name = '' |
||
111 | |||
112 | vt = {'name': name} |
||
113 | if custom is not None: |
||
114 | vt["custom"] = custom |
||
115 | if vt_params is not None: |
||
116 | vt["vt_params"] = vt_params |
||
117 | if vt_refs is not None: |
||
118 | vt["vt_refs"] = vt_refs |
||
119 | if vt_dependencies is not None: |
||
120 | vt["vt_dependencies"] = vt_dependencies |
||
121 | if vt_creation_time is not None: |
||
122 | vt["creation_time"] = vt_creation_time |
||
123 | if vt_modification_time is not None: |
||
124 | vt["modification_time"] = vt_modification_time |
||
125 | if summary is not None: |
||
126 | vt["summary"] = summary |
||
127 | if impact is not None: |
||
128 | vt["impact"] = impact |
||
129 | if affected is not None: |
||
130 | vt["affected"] = affected |
||
131 | if insight is not None: |
||
132 | vt["insight"] = insight |
||
133 | |||
134 | if solution is not None: |
||
135 | vt["solution"] = solution |
||
136 | if solution_t is not None: |
||
137 | vt["solution_type"] = solution_t |
||
138 | if solution_m is not None: |
||
139 | vt["solution_method"] = solution_m |
||
140 | |||
141 | if vuldetect is not None: |
||
142 | vt["detection"] = vuldetect |
||
143 | |||
144 | if qod_t is not None: |
||
145 | vt["qod_type"] = qod_t |
||
146 | elif qod_v is not None: |
||
147 | vt["qod"] = qod_v |
||
148 | |||
149 | if severity is not None: |
||
150 | vt["severities"] = severity |
||
151 | |||
152 | return vt |
||
153 | |||
214 |