Conditions | 19 |
Total Lines | 85 |
Code Lines | 62 |
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 |
||
92 | func (red RedHat) fillUnfixed(driver db.DB, r *models.ScanResult) (nCVEs int, err error) { |
||
93 | if red.isFetchViaHTTP() { |
||
94 | prefix, _ := util.URLPathJoin(config.Conf.Gost.URL, |
||
95 | "redhat", major(r.Release), "pkgs") |
||
96 | responses, err := getAllUnfixedCvesViaHTTP(r, prefix) |
||
97 | if err != nil { |
||
98 | return 0, err |
||
99 | } |
||
100 | for _, res := range responses { |
||
101 | // CVE-ID: RedhatCVE |
||
102 | cves := map[string]gostmodels.RedhatCVE{} |
||
103 | if err := json.Unmarshal([]byte(res.json), &cves); err != nil { |
||
104 | return 0, err |
||
105 | } |
||
106 | |||
107 | for _, cve := range cves { |
||
108 | cveCont := red.ConvertToModel(&cve) |
||
109 | v, ok := r.ScannedCves[cve.Name] |
||
110 | if ok { |
||
111 | if _, ok := v.CveContents[models.RedHatAPI]; ok { |
||
112 | v.CveContents[models.RedHatAPI] = *cveCont |
||
113 | } else { |
||
114 | v = models.VulnInfo{ |
||
115 | CveID: cveCont.CveID, |
||
116 | CveContents: models.NewCveContents(*cveCont), |
||
117 | Confidences: models.Confidences{models.RedHatAPIMatch}, |
||
118 | } |
||
119 | } |
||
120 | } else { |
||
121 | v = models.VulnInfo{ |
||
122 | CveID: cveCont.CveID, |
||
123 | CveContents: models.NewCveContents(*cveCont), |
||
124 | Confidences: models.Confidences{models.RedHatAPIMatch}, |
||
125 | } |
||
126 | nCVEs++ |
||
127 | } |
||
128 | |||
129 | pkgStats := red.mergePackageStates(v, |
||
130 | cve.PackageState, r.Packages, r.Release) |
||
131 | if 0 < len(pkgStats) { |
||
132 | v.AffectedPackages = pkgStats |
||
133 | r.ScannedCves[cve.Name] = v |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | } else { |
||
138 | if driver == nil { |
||
139 | return 0, nil |
||
140 | } |
||
141 | for _, pack := range r.Packages { |
||
142 | // CVE-ID: RedhatCVE |
||
143 | cves := map[string]gostmodels.RedhatCVE{} |
||
144 | cves = driver.GetUnfixedCvesRedhat(major(r.Release), pack.Name) |
||
145 | for _, cve := range cves { |
||
146 | cveCont := red.ConvertToModel(&cve) |
||
147 | v, ok := r.ScannedCves[cve.Name] |
||
148 | if ok { |
||
149 | if _, ok := v.CveContents[models.RedHatAPI]; ok { |
||
150 | v.CveContents[models.RedHatAPI] = *cveCont |
||
151 | } else { |
||
152 | v = models.VulnInfo{ |
||
153 | CveID: cveCont.CveID, |
||
154 | CveContents: models.NewCveContents(*cveCont), |
||
155 | Confidences: models.Confidences{models.RedHatAPIMatch}, |
||
156 | } |
||
157 | } |
||
158 | } else { |
||
159 | v = models.VulnInfo{ |
||
160 | CveID: cveCont.CveID, |
||
161 | CveContents: models.NewCveContents(*cveCont), |
||
162 | Confidences: models.Confidences{models.RedHatAPIMatch}, |
||
163 | } |
||
164 | nCVEs++ |
||
165 | } |
||
166 | |||
167 | pkgStats := red.mergePackageStates(v, |
||
168 | cve.PackageState, r.Packages, r.Release) |
||
169 | if 0 < len(pkgStats) { |
||
170 | v.AffectedPackages = pkgStats |
||
171 | r.ScannedCves[cve.Name] = v |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | return nCVEs, nil |
||
177 | } |
||
275 |