Conditions | 23 |
Total Lines | 118 |
Code Lines | 81 |
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.Debian.FillWithGost 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 |
||
42 | func (deb Debian) FillWithGost(driver db.DB, r *models.ScanResult) (nCVEs int, err error) { |
||
43 | linuxImage := "linux-image-" + r.RunningKernel.Release |
||
44 | // Add linux and set the version of running kernel to search OVAL. |
||
45 | if r.Container.ContainerID == "" { |
||
46 | newVer := "" |
||
47 | if p, ok := r.Packages[linuxImage]; ok { |
||
48 | newVer = p.NewVersion |
||
49 | } |
||
50 | r.Packages["linux"] = models.Package{ |
||
51 | Name: "linux", |
||
52 | Version: r.RunningKernel.Version, |
||
53 | NewVersion: newVer, |
||
54 | } |
||
55 | } |
||
56 | |||
57 | packCvesList := []packCves{} |
||
58 | if config.Conf.Gost.IsFetchViaHTTP() { |
||
59 | url, _ := util.URLPathJoin(config.Conf.Gost.URL, "debian", major(r.Release), "pkgs") |
||
60 | responses, err := getAllUnfixedCvesViaHTTP(r, url) |
||
61 | if err != nil { |
||
62 | return 0, err |
||
63 | } |
||
64 | |||
65 | for _, res := range responses { |
||
66 | debCves := map[string]gostmodels.DebianCVE{} |
||
67 | if err := json.Unmarshal([]byte(res.json), &debCves); err != nil { |
||
68 | return 0, err |
||
69 | } |
||
70 | cves := []models.CveContent{} |
||
71 | for _, debcve := range debCves { |
||
72 | cves = append(cves, *deb.ConvertToModel(&debcve)) |
||
73 | } |
||
74 | packCvesList = append(packCvesList, packCves{ |
||
75 | packName: res.request.packName, |
||
76 | isSrcPack: res.request.isSrcPack, |
||
77 | cves: cves, |
||
78 | }) |
||
79 | } |
||
80 | } else { |
||
81 | if driver == nil { |
||
82 | return 0, nil |
||
83 | } |
||
84 | for _, pack := range r.Packages { |
||
85 | cveDebs := driver.GetUnfixedCvesDebian(major(r.Release), pack.Name) |
||
86 | cves := []models.CveContent{} |
||
87 | for _, cveDeb := range cveDebs { |
||
88 | cves = append(cves, *deb.ConvertToModel(&cveDeb)) |
||
89 | } |
||
90 | packCvesList = append(packCvesList, packCves{ |
||
91 | packName: pack.Name, |
||
92 | isSrcPack: false, |
||
93 | cves: cves, |
||
94 | }) |
||
95 | } |
||
96 | |||
97 | // SrcPack |
||
98 | for _, pack := range r.SrcPackages { |
||
99 | cveDebs := driver.GetUnfixedCvesDebian(major(r.Release), pack.Name) |
||
100 | cves := []models.CveContent{} |
||
101 | for _, cveDeb := range cveDebs { |
||
102 | cves = append(cves, *deb.ConvertToModel(&cveDeb)) |
||
103 | } |
||
104 | packCvesList = append(packCvesList, packCves{ |
||
105 | packName: pack.Name, |
||
106 | isSrcPack: true, |
||
107 | cves: cves, |
||
108 | }) |
||
109 | } |
||
110 | } |
||
111 | |||
112 | delete(r.Packages, "linux") |
||
113 | |||
114 | for _, p := range packCvesList { |
||
115 | for _, cve := range p.cves { |
||
116 | v, ok := r.ScannedCves[cve.CveID] |
||
117 | if ok { |
||
118 | if v.CveContents == nil { |
||
119 | v.CveContents = models.NewCveContents(cve) |
||
120 | } else { |
||
121 | v.CveContents[models.DebianSecurityTracker] = cve |
||
122 | } |
||
123 | } else { |
||
124 | v = models.VulnInfo{ |
||
125 | CveID: cve.CveID, |
||
126 | CveContents: models.NewCveContents(cve), |
||
127 | Confidences: models.Confidences{models.DebianSecurityTrackerMatch}, |
||
128 | } |
||
129 | nCVEs++ |
||
130 | } |
||
131 | |||
132 | names := []string{} |
||
133 | if p.isSrcPack { |
||
134 | if srcPack, ok := r.SrcPackages[p.packName]; ok { |
||
135 | for _, binName := range srcPack.BinaryNames { |
||
136 | if _, ok := r.Packages[binName]; ok { |
||
137 | names = append(names, binName) |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | } else { |
||
142 | if p.packName == "linux" { |
||
143 | names = append(names, linuxImage) |
||
144 | } else { |
||
145 | names = append(names, p.packName) |
||
146 | } |
||
147 | } |
||
148 | |||
149 | for _, name := range names { |
||
150 | v.AffectedPackages = v.AffectedPackages.Store(models.PackageFixStatus{ |
||
151 | Name: name, |
||
152 | FixState: "open", |
||
153 | NotFixedYet: true, |
||
154 | }) |
||
155 | } |
||
156 | r.ScannedCves[cve.CveID] = v |
||
157 | } |
||
158 | } |
||
159 | return nCVEs, nil |
||
160 | } |
||
183 |