Showing 37 of 37 total issues
Similar blocks of code found in 2 locations. Consider refactoring. Open
func postRunPlugins(pluginsRootPath string, plugins []*domain.Plugin) error {
for _, plugin := range plugins {
if plugin.ExecNumber <= 0 {
continue
}
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 111.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Method Service.getRepository
has 6 return statements (exceeds 4 allowed). Open
func (s Service) getRepository(url *url.URL) (*repo, error) {
if url.Hostname() != "github.com" {
return nil, fmt.Errorf("invalid host %s", url.Hostname())
}
Method packageStore.RemovePackage
has 6 return statements (exceeds 4 allowed). Open
func (ps packageStore) RemovePackage(label string) error {
tx := ps.db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
Method repo.setBranch
has 6 return statements (exceeds 4 allowed). Open
func (r *repo) setBranch(ctx context.Context, isAuthenticated bool) error {
// Skip if a branch was already set
if r.branch != "" {
return nil
}
Method projectService.CreateProject
has 6 return statements (exceeds 4 allowed). Open
func (ps projectService) CreateProject(configRootPath string, project *domain.Project) (err error) {
// Create the root folder of the project.
err = createProjectRootFolder(project.Path)
if err != nil {
return errors.Wrap(err, "create base folder")
Function newProjectSetPathCommand
has 5 return statements (exceeds 4 allowed). Open
func newProjectSetPathCommand() *projectSetPath {
cmd := &cobra.Command{
Use: "path OLD-PATH NEW-PATH",
Short: "Set a new path",
Aliases: []string{"p"},
Method packageService.ImportPackagesFromCollection
has 5 return statements (exceeds 4 allowed). Open
func (ps packageService) ImportPackagesFromCollection(url *url.URL, exclude *regexp.Regexp) ([]*domain.Package, error) {
// Get code repo
codeRepo, err := remote.NewCodeRepository(url, ps.authentication)
if err != nil {
return nil, errors.Wrap(err, "get code repository")
Function newProjectAddCommand
has 5 return statements (exceeds 4 allowed). Open
func newProjectAddCommand() *projectAddCommand {
cmd := &cobra.Command{
Use: "add LABEL PATH",
Short: "Add an existing project",
Aliases: []string{"a"},
Method packageService.ImportPackageFromConfig
has 5 return statements (exceeds 4 allowed). Open
func (ps packageService) ImportPackageFromConfig(path string) (*domain.Package, error) {
// Validate file path
err := isConfigPathValid(path)
if err != nil {
return nil, err
Function storePlugins
has 5 return statements (exceeds 4 allowed). Open
func storePlugins(tx *gorm.DB, plugins []*domain.Plugin, packageID uint) error {
var err error
insertPluginStmt := "INSERT OR IGNORE INTO plugins (created_at, updated_at, path, exec_number, description) VALUES (?, ?, ?, ?, ?)"
insertAssociationStmt := "INSERT OR IGNORE INTO package_plugins (package_id, plugin_id) VALUES (?, ?)"
queryIDStmt := "SELECT id from plugins WHERE path = ?"
Method packageStore.deepQueryPackage
has 5 return statements (exceeds 4 allowed). Open
func (ps packageStore) deepQueryPackage(conditions string, values ...string) (pkg *domain.Package, err error) {
rows, err := ps.db.Raw(defaultPackageDeepQueryBase+" "+conditions, values).Rows()
if err != nil {
return nil, err
}
Function newPackageRemoveCommand
has a Cognitive Complexity of 22 (exceeds 20 allowed). Consider refactoring. Open
func newPackageRemoveCommand() *packageRemoveCommand {
var removeAllPackages, forceRemovePackages bool
cmd := &cobra.Command{
Use: "rm LABEL [LABEL...]",
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function newPackageImportCommand
has 5 return statements (exceeds 4 allowed). Open
func newPackageImportCommand() *packageImportCommand {
var remoteRepos, directories, configs, packages, collections []string
cmd := &cobra.Command{
Use: "import FROM [FROM...]",
Method projectStore.StoreProject
has 5 return statements (exceeds 4 allowed). Open
func (ps *projectStore) StoreProject(project *domain.Project) error {
err := ps.db.Where("path = ?", project.Path).First(project).Error
if err == nil {
return ErrProjectExists
}
Function storeTemplates
has 5 return statements (exceeds 4 allowed). Open
func storeTemplates(tx *gorm.DB, templates []*domain.Template, packageID uint) error {
insertTemplateStmt := "INSERT OR IGNORE INTO templates (created_at, updated_at, is_file, destination, path, description) VALUES (?, ?, ?, ?, ?, ?)"
insertAssociationStmt := "INSERT OR IGNORE INTO package_templates (package_id, template_id) VALUES (?, ?)"
queryIDStmt := "SELECT id from templates WHERE destination = ? AND path = ?"
for _, template := range templates {
Method packageService.ImportPackageFromRemote
has 5 return statements (exceeds 4 allowed). Open
func (ps packageService) ImportPackageFromRemote(url *url.URL) (*domain.Package, error) {
// Get code repo
codeRepo, err := remote.NewCodeRepository(url, ps.authentication)
if err != nil {
return nil, errors.Wrap(err, "get code repository")
Function newProjectRemoveCommand
has 5 return statements (exceeds 4 allowed). Open
func newProjectRemoveCommand() *projectRemoveCommand {
var removeAllProjects, forceRemoveProjects bool
cmd := &cobra.Command{
Use: "rm PATH [PATH...]",