Conditions | 15 |
Total Lines | 76 |
Code Lines | 56 |
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 gost.RedHat.fillUnfixed 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 | /* Vuls - Vulnerability Scanner |
||
116 | func (red RedHat) fillUnfixed(driver db.DB, r *models.ScanResult) (nCVEs int, err error) { |
||
117 | if config.Conf.Gost.IsFetchViaHTTP() { |
||
118 | prefix, _ := util.URLPathJoin(config.Conf.Gost.URL, |
||
119 | "redhat", major(r.Release), "pkgs") |
||
120 | responses, err := getAllUnfixedCvesViaHTTP(r, prefix) |
||
121 | if err != nil { |
||
122 | return 0, err |
||
123 | } |
||
124 | for _, res := range responses { |
||
125 | // CVE-ID: RedhatCVE |
||
126 | cves := map[string]gostmodels.RedhatCVE{} |
||
127 | if err := json.Unmarshal([]byte(res.json), &cves); err != nil { |
||
128 | return 0, err |
||
129 | } |
||
130 | |||
131 | for _, cve := range cves { |
||
132 | cveCont := red.ConvertToModel(&cve) |
||
133 | v, ok := r.ScannedCves[cve.Name] |
||
134 | if ok { |
||
135 | if v.CveContents == nil { |
||
136 | v.CveContents = models.NewCveContents(*cveCont) |
||
137 | } else { |
||
138 | v.CveContents[models.RedHatAPI] = *cveCont |
||
139 | } |
||
140 | } else { |
||
141 | v = models.VulnInfo{ |
||
142 | CveID: cveCont.CveID, |
||
143 | CveContents: models.NewCveContents(*cveCont), |
||
144 | Confidences: models.Confidences{models.RedHatAPIMatch}, |
||
145 | } |
||
146 | nCVEs++ |
||
147 | } |
||
148 | pkgStats := red.mergePackageStates(v, |
||
149 | cve.PackageState, r.Packages, r.Release) |
||
150 | if 0 < len(pkgStats) { |
||
151 | v.AffectedPackages = pkgStats |
||
152 | r.ScannedCves[cve.Name] = v |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | } else { |
||
157 | if driver == nil { |
||
158 | return 0, nil |
||
159 | } |
||
160 | for _, pack := range r.Packages { |
||
161 | // CVE-ID: RedhatCVE |
||
162 | cves := map[string]gostmodels.RedhatCVE{} |
||
163 | cves = driver.GetUnfixedCvesRedhat(major(r.Release), pack.Name) |
||
164 | for _, cve := range cves { |
||
165 | cveCont := red.ConvertToModel(&cve) |
||
166 | v, ok := r.ScannedCves[cve.Name] |
||
167 | if ok { |
||
168 | if v.CveContents == nil { |
||
169 | v.CveContents = models.NewCveContents(*cveCont) |
||
170 | } else { |
||
171 | v.CveContents[models.RedHatAPI] = *cveCont |
||
172 | } |
||
173 | } else { |
||
174 | v = models.VulnInfo{ |
||
175 | CveID: cveCont.CveID, |
||
176 | CveContents: models.NewCveContents(*cveCont), |
||
177 | Confidences: models.Confidences{models.RedHatAPIMatch}, |
||
178 | } |
||
179 | nCVEs++ |
||
180 | } |
||
181 | |||
182 | pkgStats := red.mergePackageStates(v, |
||
183 | cve.PackageState, r.Packages, r.Release) |
||
184 | if 0 < len(pkgStats) { |
||
185 | v.AffectedPackages = pkgStats |
||
186 | r.ScannedCves[cve.Name] = v |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | return nCVEs, nil |
||
192 | } |
||
290 |