| Conditions | 21 |
| Total Lines | 72 |
| Code Lines | 52 |
| 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 report.cvedictClient.FetchCveDetails 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 |
||
| 71 | func (api cvedictClient) FetchCveDetails(driver cvedb.DB, cveIDs []string) (cveDetails []cve.CveDetail, err error) { |
||
| 72 | if !config.Conf.CveDict.IsFetchViaHTTP() { |
||
| 73 | for _, cveID := range cveIDs { |
||
| 74 | cveDetail, err := driver.Get(cveID) |
||
| 75 | if err != nil { |
||
| 76 | return nil, fmt.Errorf("Failed to fetch CVE. err: %s", err) |
||
| 77 | } |
||
| 78 | if len(cveDetail.CveID) == 0 { |
||
| 79 | cveDetails = append(cveDetails, cve.CveDetail{ |
||
| 80 | CveID: cveID, |
||
| 81 | }) |
||
| 82 | } else { |
||
| 83 | cveDetails = append(cveDetails, *cveDetail) |
||
| 84 | } |
||
| 85 | } |
||
| 86 | return |
||
| 87 | } |
||
| 88 | |||
| 89 | api.baseURL = config.Conf.CveDict.URL |
||
| 90 | reqChan := make(chan string, len(cveIDs)) |
||
| 91 | resChan := make(chan response, len(cveIDs)) |
||
| 92 | errChan := make(chan error, len(cveIDs)) |
||
| 93 | defer close(reqChan) |
||
| 94 | defer close(resChan) |
||
| 95 | defer close(errChan) |
||
| 96 | |||
| 97 | go func() { |
||
| 98 | for _, cveID := range cveIDs { |
||
| 99 | reqChan <- cveID |
||
| 100 | } |
||
| 101 | }() |
||
| 102 | |||
| 103 | concurrency := 10 |
||
| 104 | tasks := util.GenWorkers(concurrency) |
||
| 105 | for range cveIDs { |
||
| 106 | tasks <- func() { |
||
| 107 | select { |
||
| 108 | case cveID := <-reqChan: |
||
| 109 | url, err := util.URLPathJoin(api.baseURL, "cves", cveID) |
||
| 110 | if err != nil { |
||
| 111 | errChan <- err |
||
| 112 | } else { |
||
| 113 | util.Log.Debugf("HTTP Request to %s", url) |
||
| 114 | api.httpGet(cveID, url, resChan, errChan) |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | timeout := time.After(2 * 60 * time.Second) |
||
| 121 | var errs []error |
||
| 122 | for range cveIDs { |
||
| 123 | select { |
||
| 124 | case res := <-resChan: |
||
| 125 | if len(res.CveDetail.CveID) == 0 { |
||
| 126 | cveDetails = append(cveDetails, cve.CveDetail{ |
||
| 127 | CveID: res.Key, |
||
| 128 | }) |
||
| 129 | } else { |
||
| 130 | cveDetails = append(cveDetails, res.CveDetail) |
||
| 131 | } |
||
| 132 | case err := <-errChan: |
||
| 133 | errs = append(errs, err) |
||
| 134 | case <-timeout: |
||
| 135 | return nil, fmt.Errorf("Timeout Fetching CVE") |
||
| 136 | } |
||
| 137 | } |
||
| 138 | if len(errs) != 0 { |
||
| 139 | return nil, |
||
| 140 | fmt.Errorf("Failed to fetch CVE. err: %v", errs) |
||
| 141 | } |
||
| 142 | return |
||
| 143 | } |
||
| 225 |