Conditions | 14 |
Total Lines | 73 |
Code Lines | 44 |
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 cmd.scanConfigFromCmd 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 | package cmd |
||
14 | func scanConfigFromCmd(cmd *cobra.Command) (*scan.Config, error) { |
||
15 | c := &scan.Config{} |
||
16 | |||
17 | var err error |
||
18 | |||
19 | c.DictionaryPath = cmd.Flag(flagDictionary).Value.String() |
||
20 | |||
21 | c.HTTPMethods, err = cmd.Flags().GetStringSlice(flagHTTPMethods) |
||
22 | if err != nil { |
||
23 | return nil, errors.Wrap(err, "failed to read http methods flag") |
||
24 | } |
||
25 | |||
26 | c.HTTPStatusesToIgnore, err = cmd.Flags().GetIntSlice(flagHTTPStatusesToIgnore) |
||
27 | if err != nil { |
||
28 | return nil, errors.Wrap(err, "failed to read http methods flag") |
||
29 | } |
||
30 | |||
31 | c.Threads, err = cmd.Flags().GetInt(flagThreads) |
||
32 | if err != nil { |
||
33 | return nil, errors.Wrap(err, "failed to read threads flag") |
||
34 | } |
||
35 | |||
36 | c.TimeoutInMilliseconds, err = cmd.Flags().GetInt(flagHTTPTimeout) |
||
37 | if err != nil { |
||
38 | return nil, errors.Wrap(err, "failed to read http-timeout flag") |
||
39 | } |
||
40 | |||
41 | c.CacheRequests, err = cmd.Flags().GetBool(flagHTTPCacheRequests) |
||
42 | if err != nil { |
||
43 | return nil, errors.Wrap(err, "failed to read http-cache-requests flag") |
||
44 | } |
||
45 | |||
46 | c.ScanDepth, err = cmd.Flags().GetInt(flagScanDepth) |
||
47 | if err != nil { |
||
48 | return nil, errors.Wrap(err, "failed to read http-timeout flag") |
||
49 | } |
||
50 | |||
51 | socks5Host := cmd.Flag(flagSocks5Host).Value.String() |
||
52 | if len(socks5Host) > 0 { |
||
53 | c.Socks5Url, err = url.Parse("socks5://" + socks5Host) |
||
54 | if err != nil { |
||
55 | return nil, errors.Wrap(err, "invalid value for "+flagSocks5Host) |
||
56 | } |
||
57 | } |
||
58 | |||
59 | c.UserAgent = cmd.Flag(flagUserAgent).Value.String() |
||
60 | |||
61 | c.UseCookieJar, err = cmd.Flags().GetBool(flagCookieJar) |
||
62 | if err != nil { |
||
63 | return nil, errors.Wrap(err, "cookie jar flag is invalid") |
||
64 | } |
||
65 | |||
66 | rawCookies, err := cmd.Flags().GetStringArray(flagCookie) |
||
67 | if err != nil { |
||
68 | return nil, errors.Wrap(err, "failed to read cookies flag") |
||
69 | } |
||
70 | |||
71 | c.Cookies, err = rawCookiesToCookies(rawCookies) |
||
72 | if err != nil { |
||
73 | return nil, errors.Wrap(err, "failed to convert rawCookies to objects") |
||
74 | } |
||
75 | |||
76 | rawHeaders, err := cmd.Flags().GetStringArray(flagHeader) |
||
77 | if err != nil { |
||
78 | return nil, errors.Wrap(err, "failed to read cookies flag") |
||
79 | } |
||
80 | |||
81 | c.Headers, err = rawHeadersToHeaders(rawHeaders) |
||
82 | if err != nil { |
||
83 | return nil, errors.Wrap(err, "failed to convert rawHeaders") |
||
84 | } |
||
85 | |||
86 | return c, nil |
||
87 | } |
||
125 |