| Conditions | 36 |
| Total Lines | 117 |
| 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 dependency.IterativeScan 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 dependency |
||
| 38 | func IterativeScan(path string) ([]string, []string, error) { |
||
| 39 | |||
| 40 | // TODO(mattfarina): Add support for release tags. |
||
| 41 | |||
| 42 | tgs, _ := readBuildTags(path) |
||
| 43 | // Handle the case of scanning with no tags |
||
| 44 | tgs = append(tgs, "") |
||
| 45 | |||
| 46 | var pkgs []string |
||
| 47 | var testPkgs []string |
||
| 48 | for _, tt := range tgs { |
||
| 49 | |||
| 50 | // split the tag combination to look at permutations. |
||
| 51 | ts := strings.Split(tt, ",") |
||
| 52 | var ttgs []string |
||
| 53 | var arch string |
||
| 54 | var ops string |
||
| 55 | for _, ttt := range ts { |
||
| 56 | dirty := false |
||
| 57 | if strings.HasPrefix(ttt, "!") { |
||
| 58 | dirty = true |
||
| 59 | ttt = strings.TrimPrefix(ttt, "!") |
||
| 60 | } |
||
| 61 | if isSupportedOs(ttt) { |
||
| 62 | if dirty { |
||
| 63 | ops = getOsValue(ttt) |
||
| 64 | } else { |
||
| 65 | ops = ttt |
||
| 66 | } |
||
| 67 | } else if isSupportedArch(ttt) { |
||
| 68 | if dirty { |
||
| 69 | arch = getArchValue(ttt) |
||
| 70 | } else { |
||
| 71 | arch = ttt |
||
| 72 | } |
||
| 73 | } else { |
||
| 74 | if !dirty { |
||
| 75 | ttgs = append(ttgs, ttt) |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | // Handle the case where there are no tags but we need to iterate |
||
| 81 | // on something. |
||
| 82 | if len(ttgs) == 0 { |
||
| 83 | ttgs = append(ttgs, "") |
||
| 84 | } |
||
| 85 | |||
| 86 | b, err := util.GetBuildContext() |
||
| 87 | if err != nil { |
||
| 88 | return []string{}, []string{}, err |
||
| 89 | } |
||
| 90 | |||
| 91 | // Make sure use all files is off |
||
| 92 | b.UseAllFiles = false |
||
| 93 | |||
| 94 | // Set the OS and Arch for this pass |
||
| 95 | b.GOARCH = arch |
||
| 96 | b.GOOS = ops |
||
| 97 | b.BuildTags = ttgs |
||
| 98 | msg.Debug("Scanning with Arch(%s), OS(%s), and Build Tags(%v)", arch, ops, ttgs) |
||
| 99 | |||
| 100 | pk, err := b.ImportDir(path, 0) |
||
| 101 | |||
| 102 | // If there are no buildable souce with this permutation we skip it. |
||
| 103 | if err != nil && strings.HasPrefix(err.Error(), "no buildable Go source files in") { |
||
| 104 | continue |
||
| 105 | } else if err != nil && strings.HasPrefix(err.Error(), "found packages ") { |
||
| 106 | // A permutation may cause multiple packages to appear. For example, |
||
| 107 | // an example file with an ignore build tag. If this happens we |
||
| 108 | // ignore it. |
||
| 109 | // TODO(mattfarina): Find a better way. |
||
| 110 | msg.Debug("Found multiple packages while scanning %s: %s", path, err) |
||
| 111 | continue |
||
| 112 | } else if err != nil { |
||
| 113 | msg.Debug("Problem parsing package at %s for %s %s", path, ops, arch) |
||
| 114 | return []string{}, []string{}, err |
||
| 115 | } |
||
| 116 | |||
| 117 | for _, dep := range pk.Imports { |
||
| 118 | found := false |
||
| 119 | for _, p := range pkgs { |
||
| 120 | if p == dep { |
||
| 121 | found = true |
||
| 122 | } |
||
| 123 | } |
||
| 124 | if !found { |
||
| 125 | pkgs = append(pkgs, dep) |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | for _, dep := range pk.TestImports { |
||
| 130 | found := false |
||
| 131 | for _, p := range pkgs { |
||
| 132 | if p == dep { |
||
| 133 | found = true |
||
| 134 | } |
||
| 135 | } |
||
| 136 | if !found { |
||
| 137 | testPkgs = append(testPkgs, dep) |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | for _, dep := range pk.XTestImports { |
||
| 142 | found := false |
||
| 143 | for _, p := range pkgs { |
||
| 144 | if p == dep { |
||
| 145 | found = true |
||
| 146 | } |
||
| 147 | } |
||
| 148 | if !found { |
||
| 149 | testPkgs = append(testPkgs, dep) |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | return pkgs, testPkgs, nil |
||
| 155 | } |
||
| 313 |