| Conditions | 25 |
| Total Lines | 107 |
| Code Lines | 69 |
| 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 wordpress.FillWordPress 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 |
||
| 66 | func FillWordPress(r *models.ScanResult, token string) (int, error) { |
||
| 67 | // Core |
||
| 68 | ver := strings.Replace(r.WordPressPackages.CoreVersion(), ".", "", -1) |
||
| 69 | if ver == "" { |
||
| 70 | return 0, xerrors.New("Failed to get WordPress core version") |
||
| 71 | } |
||
| 72 | url := fmt.Sprintf("https://wpvulndb.com/api/v3/wordpresses/%s", ver) |
||
| 73 | body, err := httpRequest(url, token) |
||
| 74 | if err != nil { |
||
| 75 | return 0, err |
||
| 76 | } |
||
| 77 | if body == "" { |
||
| 78 | util.Log.Warnf("A result of REST access is empty: %s", url) |
||
| 79 | } |
||
| 80 | wpVinfos, err := convertToVinfos(models.WPCore, body) |
||
| 81 | if err != nil { |
||
| 82 | return 0, err |
||
| 83 | } |
||
| 84 | |||
| 85 | //TODO add a flag ignore inactive plugin or themes such as -wp-ignore-inactive flag to cmd line option or config.toml |
||
| 86 | |||
| 87 | // Themes |
||
| 88 | for _, p := range r.WordPressPackages.Themes() { |
||
| 89 | url := fmt.Sprintf("https://wpvulndb.com/api/v3/themes/%s", p.Name) |
||
| 90 | body, err := httpRequest(url, token) |
||
| 91 | if err != nil { |
||
| 92 | return 0, err |
||
| 93 | } |
||
| 94 | if body == "" { |
||
| 95 | continue |
||
| 96 | } |
||
| 97 | |||
| 98 | templateVinfos, err := convertToVinfos(p.Name, body) |
||
| 99 | if err != nil { |
||
| 100 | return 0, err |
||
| 101 | } |
||
| 102 | |||
| 103 | for _, v := range templateVinfos { |
||
| 104 | for _, fixstat := range v.WpPackageFixStats { |
||
| 105 | pkg, ok := r.WordPressPackages.Find(fixstat.Name) |
||
| 106 | if !ok { |
||
| 107 | continue |
||
| 108 | } |
||
| 109 | ok, err := match(pkg.Version, fixstat.FixedIn) |
||
| 110 | if err != nil { |
||
| 111 | return 0, xerrors.Errorf("Not a semantic versioning: %w", err) |
||
|
|
|||
| 112 | } |
||
| 113 | if ok { |
||
| 114 | wpVinfos = append(wpVinfos, v) |
||
| 115 | util.Log.Infof("[match] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn) |
||
| 116 | } else { |
||
| 117 | util.Log.Debugf("[miss] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn) |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | // Plugins |
||
| 124 | for _, p := range r.WordPressPackages.Plugins() { |
||
| 125 | url := fmt.Sprintf("https://wpvulndb.com/api/v3/plugins/%s", p.Name) |
||
| 126 | body, err := httpRequest(url, token) |
||
| 127 | if err != nil { |
||
| 128 | return 0, err |
||
| 129 | } |
||
| 130 | if body == "" { |
||
| 131 | continue |
||
| 132 | } |
||
| 133 | |||
| 134 | pluginVinfos, err := convertToVinfos(p.Name, body) |
||
| 135 | if err != nil { |
||
| 136 | return 0, err |
||
| 137 | } |
||
| 138 | |||
| 139 | for _, v := range pluginVinfos { |
||
| 140 | for _, fixstat := range v.WpPackageFixStats { |
||
| 141 | pkg, ok := r.WordPressPackages.Find(fixstat.Name) |
||
| 142 | if !ok { |
||
| 143 | continue |
||
| 144 | } |
||
| 145 | ok, err := match(pkg.Version, fixstat.FixedIn) |
||
| 146 | if err != nil { |
||
| 147 | return 0, xerrors.Errorf("Not a semantic versioning: %w", err) |
||
| 148 | } |
||
| 149 | if ok { |
||
| 150 | wpVinfos = append(wpVinfos, v) |
||
| 151 | //TODO Debugf |
||
| 152 | util.Log.Infof("[match] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn) |
||
| 153 | } else { |
||
| 154 | //TODO Debugf |
||
| 155 | util.Log.Infof("[miss] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn) |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | for _, wpVinfo := range wpVinfos { |
||
| 162 | if vinfo, ok := r.ScannedCves[wpVinfo.CveID]; ok { |
||
| 163 | vinfo.CveContents[models.WPVulnDB] = wpVinfo.CveContents[models.WPVulnDB] |
||
| 164 | vinfo.VulnType = wpVinfo.VulnType |
||
| 165 | vinfo.Confidences = append(vinfo.Confidences, wpVinfo.Confidences...) |
||
| 166 | vinfo.WpPackageFixStats = append(vinfo.WpPackageFixStats, wpVinfo.WpPackageFixStats...) |
||
| 167 | r.ScannedCves[wpVinfo.CveID] = vinfo |
||
| 168 | } else { |
||
| 169 | r.ScannedCves[wpVinfo.CveID] = wpVinfo |
||
| 170 | } |
||
| 171 | } |
||
| 172 | return len(wpVinfos), nil |
||
| 173 | } |
||
| 271 |