Conditions | 32 |
Total Lines | 108 |
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 | //TODO Debugf |
||
118 | util.Log.Infof("[miss] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn) |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | // Plugins |
||
125 | for _, p := range r.WordPressPackages.Plugins() { |
||
126 | url := fmt.Sprintf("https://wpvulndb.com/api/v3/plugins/%s", p.Name) |
||
127 | body, err := httpRequest(url, token) |
||
128 | if err != nil { |
||
129 | return 0, err |
||
130 | } |
||
131 | if body == "" { |
||
132 | continue |
||
133 | } |
||
134 | |||
135 | pluginVinfos, err := convertToVinfos(p.Name, body) |
||
136 | if err != nil { |
||
137 | return 0, err |
||
138 | } |
||
139 | |||
140 | for _, v := range pluginVinfos { |
||
141 | for _, fixstat := range v.WpPackageFixStats { |
||
142 | pkg, ok := r.WordPressPackages.Find(fixstat.Name) |
||
143 | if !ok { |
||
144 | continue |
||
145 | } |
||
146 | ok, err := match(pkg.Version, fixstat.FixedIn) |
||
147 | if err != nil { |
||
148 | return 0, xerrors.Errorf("Not a semantic versioning: %w", err) |
||
149 | } |
||
150 | if ok { |
||
151 | wpVinfos = append(wpVinfos, v) |
||
152 | //TODO Debugf |
||
153 | util.Log.Infof("[match] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn) |
||
154 | } else { |
||
155 | //TODO Debugf |
||
156 | util.Log.Infof("[miss] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn) |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | |||
162 | for _, wpVinfo := range wpVinfos { |
||
163 | if vinfo, ok := r.ScannedCves[wpVinfo.CveID]; ok { |
||
164 | vinfo.CveContents[models.WPVulnDB] = wpVinfo.CveContents[models.WPVulnDB] |
||
165 | vinfo.VulnType = wpVinfo.VulnType |
||
166 | vinfo.Confidences = append(vinfo.Confidences, wpVinfo.Confidences...) |
||
167 | vinfo.WpPackageFixStats = append(vinfo.WpPackageFixStats, wpVinfo.WpPackageFixStats...) |
||
168 | r.ScannedCves[wpVinfo.CveID] = vinfo |
||
169 | } else { |
||
170 | r.ScannedCves[wpVinfo.CveID] = wpVinfo |
||
171 | } |
||
172 | } |
||
173 | return len(wpVinfos), nil |
||
174 | } |
||
272 |