| Conditions | 27 |
| Total Lines | 111 |
| Code Lines | 68 |
| 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 action.guessDeps 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 action |
||
| 76 | func guessDeps(base string, skipImport bool) *cfg.Config { |
||
| 77 | buildContext, err := util.GetBuildContext() |
||
| 78 | if err != nil { |
||
| 79 | msg.Die("Failed to build an import context: %s", err) |
||
| 80 | } |
||
| 81 | name := buildContext.PackageName(base) |
||
| 82 | |||
| 83 | msg.Info("Generating a YAML configuration file and guessing the dependencies") |
||
| 84 | |||
| 85 | config := new(cfg.Config) |
||
| 86 | |||
| 87 | // Get the name of the top level package |
||
| 88 | config.Name = name |
||
| 89 | |||
| 90 | // Import by looking at other package managers and looking over the |
||
| 91 | // entire directory structure. |
||
| 92 | |||
| 93 | // Attempt to import from other package managers. |
||
| 94 | if !skipImport { |
||
| 95 | guessImportDeps(base, config) |
||
| 96 | } |
||
| 97 | |||
| 98 | importLen := len(config.Imports) |
||
| 99 | if importLen == 0 { |
||
| 100 | msg.Info("Scanning code to look for dependencies") |
||
| 101 | } else { |
||
| 102 | msg.Info("Scanning code to look for dependencies not found in import") |
||
| 103 | } |
||
| 104 | |||
| 105 | // Resolve dependencies by looking at the tree. |
||
| 106 | r, err := dependency.NewResolver(base) |
||
| 107 | if err != nil { |
||
| 108 | msg.Die("Error creating a dependency resolver: %s", err) |
||
| 109 | } |
||
| 110 | |||
| 111 | // When creating resolve the test dependencies as well as the application ones. |
||
| 112 | r.ResolveTest = true |
||
| 113 | |||
| 114 | h := &dependency.DefaultMissingPackageHandler{Missing: []string{}, Gopath: []string{}} |
||
| 115 | r.Handler = h |
||
| 116 | |||
| 117 | sortable, testSortable, err := r.ResolveLocal(false) |
||
| 118 | if err != nil { |
||
| 119 | msg.Die("Error resolving local dependencies: %s", err) |
||
| 120 | } |
||
| 121 | |||
| 122 | sort.Strings(sortable) |
||
| 123 | sort.Strings(testSortable) |
||
| 124 | |||
| 125 | vpath := r.VendorDir |
||
| 126 | if !strings.HasSuffix(vpath, "/") { |
||
| 127 | vpath = vpath + string(os.PathSeparator) |
||
| 128 | } |
||
| 129 | |||
| 130 | for _, pa := range sortable { |
||
| 131 | n := strings.TrimPrefix(pa, vpath) |
||
| 132 | root, subpkg := util.NormalizeName(n) |
||
| 133 | |||
| 134 | if !config.Imports.Has(root) && root != config.Name { |
||
| 135 | msg.Info("--> Found reference to %s\n", n) |
||
| 136 | d := &cfg.Dependency{ |
||
| 137 | Name: root, |
||
| 138 | } |
||
| 139 | if len(subpkg) > 0 { |
||
| 140 | d.Subpackages = []string{subpkg} |
||
| 141 | } |
||
| 142 | config.Imports = append(config.Imports, d) |
||
| 143 | } else if config.Imports.Has(root) { |
||
| 144 | if len(subpkg) > 0 { |
||
| 145 | subpkg = strings.TrimPrefix(subpkg, "/") |
||
| 146 | d := config.Imports.Get(root) |
||
| 147 | if !d.HasSubpackage(subpkg) { |
||
| 148 | msg.Info("--> Adding sub-package %s to %s\n", subpkg, root) |
||
| 149 | d.Subpackages = append(d.Subpackages, subpkg) |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | for _, pa := range testSortable { |
||
| 156 | n := strings.TrimPrefix(pa, vpath) |
||
| 157 | root, subpkg := util.NormalizeName(n) |
||
| 158 | |||
| 159 | if config.Imports.Has(root) && root != config.Name { |
||
| 160 | msg.Debug("--> Found test reference to %s already listed as an import", n) |
||
| 161 | } else if !config.DevImports.Has(root) && root != config.Name { |
||
| 162 | msg.Info("--> Found test reference to %s", n) |
||
| 163 | d := &cfg.Dependency{ |
||
| 164 | Name: root, |
||
| 165 | } |
||
| 166 | if len(subpkg) > 0 { |
||
| 167 | d.Subpackages = []string{subpkg} |
||
| 168 | } |
||
| 169 | config.DevImports = append(config.DevImports, d) |
||
| 170 | } else if config.DevImports.Has(root) { |
||
| 171 | if len(subpkg) > 0 { |
||
| 172 | subpkg = strings.TrimPrefix(subpkg, "/") |
||
| 173 | d := config.DevImports.Get(root) |
||
| 174 | if !d.HasSubpackage(subpkg) { |
||
| 175 | msg.Info("--> Adding test sub-package %s to %s\n", subpkg, root) |
||
| 176 | d.Subpackages = append(d.Subpackages, subpkg) |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | if len(config.Imports) == importLen && importLen != 0 { |
||
| 183 | msg.Info("--> Code scanning found no additional imports") |
||
| 184 | } |
||
| 185 | |||
| 186 | return config |
||
| 187 | } |
||
| 246 |