Conditions | 31 |
Total Lines | 100 |
Code Lines | 63 |
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, fmt.Errorf("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 | wpVinfos, err := convertToVinfos(models.WPCore, string(body)) |
||
78 | if err != nil { |
||
79 | return 0, err |
||
80 | } |
||
81 | |||
82 | //TODO add a flag ignore inactive plugin or themes such as -wp-ignore-inactive flag to cmd line option or config.toml |
||
83 | |||
84 | // Themes |
||
85 | for _, p := range r.WordPressPackages.Themes() { |
||
86 | url := fmt.Sprintf("https://wpvulndb.com/api/v3/themes/%s", p.Name) |
||
87 | body, err := httpRequest(url, token) |
||
88 | if err != nil { |
||
89 | return 0, err |
||
90 | } |
||
91 | if body == "" { |
||
92 | continue |
||
93 | } |
||
94 | |||
95 | templateVinfos, err := convertToVinfos(p.Name, string(body)) |
||
96 | if err != nil { |
||
97 | return 0, err |
||
98 | } |
||
99 | |||
100 | for _, v := range templateVinfos { |
||
101 | for _, fixstat := range v.WpPackageFixStats { |
||
102 | pkg, ok := r.WordPressPackages.Find(fixstat.Name) |
||
103 | if !ok { |
||
104 | continue |
||
105 | } |
||
106 | ok, err := match(pkg.Version, fixstat.FixedIn) |
||
107 | if err != nil { |
||
108 | return 0, errors.Wrap(err, "Not a semantic versionng") |
||
109 | } |
||
110 | if ok { |
||
111 | wpVinfos = append(wpVinfos, v) |
||
112 | } else { |
||
113 | //TODO Debugf |
||
114 | util.Log.Infof("no match %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn) |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // Plugins |
||
121 | for _, p := range r.WordPressPackages.Plugins() { |
||
122 | url := fmt.Sprintf("https://wpvulndb.com/api/v3/plugins/%s", p.Name) |
||
123 | body, err := httpRequest(url, token) |
||
124 | if err != nil { |
||
125 | return 0, err |
||
126 | } |
||
127 | if body == "" { |
||
128 | continue |
||
129 | } |
||
130 | |||
131 | pluginVinfos, err := convertToVinfos(p.Name, string(body)) |
||
132 | if err != nil { |
||
133 | return 0, err |
||
134 | } |
||
135 | |||
136 | for _, v := range pluginVinfos { |
||
137 | for _, fixstat := range v.WpPackageFixStats { |
||
138 | pkg, ok := r.WordPressPackages.Find(fixstat.Name) |
||
139 | if !ok { |
||
140 | continue |
||
141 | } |
||
142 | ok, err := match(pkg.Version, fixstat.FixedIn) |
||
143 | if err != nil { |
||
144 | return 0, errors.Wrap(err, "Not a semantic versionng") |
||
145 | } |
||
146 | if ok { |
||
147 | wpVinfos = append(wpVinfos, v) |
||
148 | } else { |
||
149 | //TODO Debugf |
||
150 | util.Log.Infof("no match %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn) |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | for _, wpVinfo := range wpVinfos { |
||
157 | if vinfo, ok := r.ScannedCves[wpVinfo.CveID]; ok { |
||
158 | vinfo.CveContents[models.WPVulnDB] = wpVinfo.CveContents[models.WPVulnDB] |
||
159 | vinfo.WpPackageFixStats = wpVinfo.WpPackageFixStats |
||
160 | r.ScannedCves[wpVinfo.CveID] = vinfo |
||
161 | } else { |
||
162 | r.ScannedCves[wpVinfo.CveID] = wpVinfo |
||
163 | } |
||
164 | } |
||
165 | return len(wpVinfos), nil |
||
166 | } |
||
261 |