scope.go

Summary

Maintainability
F
1 wk
Test Coverage

File scope.go has 1189 lines of code (exceeds 500 allowed). Consider refactoring.
Open

package gorm

import (
    "bytes"
    "database/sql"
Severity: Major
Found in scope.go - About 2 days to fix

    Scope has 78 methods (exceeds 20 allowed). Consider refactoring.
    Open

    type Scope struct {
        Search          *search
        Value           interface{}
        SQL             string
        SQLVars         []interface{}
    Severity: Major
    Found in scope.go - About 1 day to fix

      Method Scope.buildCondition has a Cognitive Complexity of 79 (exceeds 20 allowed). Consider refactoring.
      Open

      func (scope *Scope) buildCondition(clause map[string]interface{}, include bool) (str string) {
          var (
              quotedTableName  = scope.QuotedTableName()
              quotedPrimaryKey = scope.Quote(scope.PrimaryKey())
              equalSQL         = "="
      Severity: Minor
      Found in scope.go - About 1 day to fix

      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

      Method Scope.buildCondition has 124 lines of code (exceeds 50 allowed). Consider refactoring.
      Open

      func (scope *Scope) buildCondition(clause map[string]interface{}, include bool) (str string) {
          var (
              quotedTableName  = scope.QuotedTableName()
              quotedPrimaryKey = scope.Quote(scope.PrimaryKey())
              equalSQL         = "="
      Severity: Major
      Found in scope.go - About 3 hrs to fix

        Method Scope.related has a Cognitive Complexity of 42 (exceeds 20 allowed). Consider refactoring.
        Open

        func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
            toScope := scope.db.NewScope(value)
            tx := scope.db.Set("gorm:association:source", scope.Value)
        
            for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") {
        Severity: Minor
        Found in scope.go - About 3 hrs to fix

        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

        Method Scope.getColumnAsArray has a Cognitive Complexity of 41 (exceeds 20 allowed). Consider refactoring.
        Open

        func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (results [][]interface{}) {
            resultMap := make(map[string][]interface{})
            for _, value := range values {
                indirectValue := indirect(reflect.ValueOf(value))
        
        
        Severity: Minor
        Found in scope.go - About 3 hrs to fix

        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

        Method Scope.getColumnAsScope has a Cognitive Complexity of 30 (exceeds 20 allowed). Consider refactoring.
        Open

        func (scope *Scope) getColumnAsScope(column string) *Scope {
            indirectScopeValue := scope.IndirectValue()
        
            switch indirectScopeValue.Kind() {
            case reflect.Slice:
        Severity: Minor
        Found in scope.go - About 1 hr to fix

        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

        Method Scope.autoIndex has a Cognitive Complexity of 27 (exceeds 20 allowed). Consider refactoring.
        Open

        func (scope *Scope) autoIndex() *Scope {
            var indexes = map[string][]string{}
            var uniqueIndexes = map[string][]string{}
        
            for _, field := range scope.GetStructFields() {
        Severity: Minor
        Found in scope.go - About 1 hr to fix

        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

        Method Scope.updatedAttrsWithValues has a Cognitive Complexity of 26 (exceeds 20 allowed). Consider refactoring.
        Open

        func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[string]interface{}, hasUpdate bool) {
            if scope.IndirectValue().Kind() != reflect.Struct {
                return convertInterfaceToMap(value, false, scope.db), true
            }
        
        
        Severity: Minor
        Found in scope.go - About 1 hr to fix

        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

        Avoid deeply nested control flow statements.
        Open

                            if len(arrayMarks) > 0 {
                                tempMarks = append(tempMarks, fmt.Sprintf("(%v)", strings.Join(arrayMarks, ",")))
                            }
        Severity: Major
        Found in scope.go - About 45 mins to fix

          Avoid deeply nested control flow statements.
          Open

                          } else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
                              for idx, foreignKey := range relationship.ForeignDBNames {
                                  if field, ok := scope.FieldByName(relationship.AssociationForeignDBNames[idx]); ok {
                                      tx = tx.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
                                  }
          Severity: Major
          Found in scope.go - About 45 mins to fix

            Avoid deeply nested control flow statements.
            Open

                            } else if result.CanAddr() && resultsMap[result.Addr()] != true {
                                resultsMap[result.Addr()] = true
                                results = reflect.Append(results, result.Addr())
                            }
            Severity: Major
            Found in scope.go - About 45 mins to fix

              Method Scope.buildCondition has 7 return statements (exceeds 4 allowed).
              Open

              func (scope *Scope) buildCondition(clause map[string]interface{}, include bool) (str string) {
                  var (
                      quotedTableName  = scope.QuotedTableName()
                      quotedPrimaryKey = scope.Quote(scope.PrimaryKey())
                      equalSQL         = "="
              Severity: Major
              Found in scope.go - About 45 mins to fix

                Avoid deeply nested control flow statements.
                Open

                            } else if values := reflect.ValueOf(arg); values.Len() > 0 {
                                var tempMarks []string
                                for i := 0; i < values.Len(); i++ {
                                    tempMarks = append(tempMarks, scope.AddToVars(values.Index(i).Interface()))
                                }
                Severity: Major
                Found in scope.go - About 45 mins to fix

                  Avoid deeply nested control flow statements.
                  Open

                                          if field, ok := scope.FieldByName(foreignKey); ok {
                                              tx = tx.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.AssociationForeignDBNames[idx])), field.Field.Interface())
                                          }
                  Severity: Major
                  Found in scope.go - About 45 mins to fix

                    Avoid deeply nested control flow statements.
                    Open

                                    if len(tempMarks) > 0 {
                                        replacements = append(replacements, strings.Join(tempMarks, ","))
                                    }
                    Severity: Major
                    Found in scope.go - About 45 mins to fix

                      Avoid deeply nested control flow statements.
                      Open

                                          for j := 0; j < result.Len(); j++ {
                                              if elem := result.Index(j); elem.CanAddr() && resultsMap[elem.Addr()] != true {
                                                  resultsMap[elem.Addr()] = true
                                                  results = reflect.Append(results, elem.Addr())
                                              }
                      Severity: Major
                      Found in scope.go - About 45 mins to fix

                        Avoid deeply nested control flow statements.
                        Open

                                                if err == ErrUnaddressable {
                                                    results[field.DBName] = value
                                                } else {
                                                    results[field.DBName] = field.Field.Interface()
                                                }
                        Severity: Major
                        Found in scope.go - About 45 mins to fix

                          Method Scope.whereSQL has a Cognitive Complexity of 22 (exceeds 20 allowed). Consider refactoring.
                          Open

                          func (scope *Scope) whereSQL() (sql string) {
                              var (
                                  quotedTableName                                = scope.QuotedTableName()
                                  deletedAtField, hasDeletedAtField              = scope.FieldByName("DeletedAt")
                                  primaryConditions, andConditions, orConditions []string
                          Severity: Minor
                          Found in scope.go - About 35 mins to fix

                          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 convertInterfaceToMap has a Cognitive Complexity of 21 (exceeds 20 allowed). Consider refactoring.
                          Open

                          func convertInterfaceToMap(values interface{}, withIgnoredField bool, db *DB) map[string]interface{} {
                              var attrs = map[string]interface{}{}
                          
                              switch value := values.(type) {
                              case map[string]interface{}:
                          Severity: Minor
                          Found in scope.go - About 25 mins to fix

                          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

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                                      for idx, fieldName := range relationship.AssociationForeignFieldNames {
                                          if field, ok := toScope.FieldByName(fieldName); ok {
                                              foreignKeyStruct := field.clone()
                                              foreignKeyStruct.IsPrimaryKey = false
                                              foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true")
                          Severity: Major
                          Found in scope.go and 1 other location - About 1 hr to fix
                          scope.go on lines 1143..1152

                          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 152.

                          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

                          Further Reading

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                                      for idx, fieldName := range relationship.ForeignFieldNames {
                                          if field, ok := scope.FieldByName(fieldName); ok {
                                              foreignKeyStruct := field.clone()
                                              foreignKeyStruct.IsPrimaryKey = false
                                              foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true")
                          Severity: Major
                          Found in scope.go and 1 other location - About 1 hr to fix
                          scope.go on lines 1154..1163

                          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 152.

                          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

                          Further Reading

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                                  if name, ok := field.TagSettingsGet("INDEX"); ok {
                                      names := strings.Split(name, ",")
                          
                                      for _, name := range names {
                                          if name == "INDEX" || name == "" {
                          Severity: Major
                          Found in scope.go and 1 other location - About 1 hr to fix
                          scope.go on lines 1306..1316

                          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 137.

                          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

                          Further Reading

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                                  if name, ok := field.TagSettingsGet("UNIQUE_INDEX"); ok {
                                      names := strings.Split(name, ",")
                          
                                      for _, name := range names {
                                          if name == "UNIQUE_INDEX" || name == "" {
                          Severity: Major
                          Found in scope.go and 1 other location - About 1 hr to fix
                          scope.go on lines 1294..1304

                          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 137.

                          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

                          Further Reading

                          There are no issues that match your filters.

                          Category
                          Status