portainer/portainer

View on GitHub
api/http/handler/settings/settings_update.go

Summary

Maintainability
C
1 day
Test Coverage

Method Handler.updateSettings has a Cognitive Complexity of 46 (exceeds 20 allowed). Consider refactoring.
Open

func (handler *Handler) updateSettings(tx dataservices.DataStoreTx, payload settingsUpdatePayload) (*portainer.Settings, error) {
    settings, err := tx.Settings().Settings()
    if err != nil {
        return nil, httperror.InternalServerError("Unable to retrieve the settings from the database", err)
    }
Severity: Minor
Found in api/http/handler/settings/settings_update.go - About 4 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 Handler.updateSettings has 114 lines of code (exceeds 50 allowed). Consider refactoring.
Open

func (handler *Handler) updateSettings(tx dataservices.DataStoreTx, payload settingsUpdatePayload) (*portainer.Settings, error) {
    settings, err := tx.Settings().Settings()
    if err != nil {
        return nil, httperror.InternalServerError("Unable to retrieve the settings from the database", err)
    }
Severity: Major
Found in api/http/handler/settings/settings_update.go - About 3 hrs to fix

    Method settingsUpdatePayload.Validate has 9 return statements (exceeds 4 allowed).
    Open

    func (payload *settingsUpdatePayload) Validate(r *http.Request) error {
        if payload.AuthenticationMethod != nil && *payload.AuthenticationMethod != 1 && *payload.AuthenticationMethod != 2 && *payload.AuthenticationMethod != 3 {
            return errors.New("Invalid authentication method value. Value must be one of: 1 (internal), 2 (LDAP/AD) or 3 (OAuth)")
        }
    
    
    Severity: Major
    Found in api/http/handler/settings/settings_update.go - About 55 mins to fix

      Method Handler.updateSettings has 6 return statements (exceeds 4 allowed).
      Open

      func (handler *Handler) updateSettings(tx dataservices.DataStoreTx, payload settingsUpdatePayload) (*portainer.Settings, error) {
          settings, err := tx.Settings().Settings()
          if err != nil {
              return nil, httperror.InternalServerError("Unable to retrieve the settings from the database", err)
          }
      Severity: Major
      Found in api/http/handler/settings/settings_update.go - About 40 mins to fix

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

        func (payload *settingsUpdatePayload) Validate(r *http.Request) error {
            if payload.AuthenticationMethod != nil && *payload.AuthenticationMethod != 1 && *payload.AuthenticationMethod != 2 && *payload.AuthenticationMethod != 3 {
                return errors.New("Invalid authentication method value. Value must be one of: 1 (internal), 2 (LDAP/AD) or 3 (OAuth)")
            }
        
        
        Severity: Minor
        Found in api/http/handler/settings/settings_update.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

        Method Handler.settingsUpdate has 5 return statements (exceeds 4 allowed).
        Open

        func (handler *Handler) settingsUpdate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
            var payload settingsUpdatePayload
            err := request.DecodeAndValidateJSONPayload(r, &payload)
            if err != nil {
                return httperror.BadRequest("Invalid request payload", err)
        Severity: Major
        Found in api/http/handler/settings/settings_update.go - About 35 mins to fix

          There are no issues that match your filters.

          Category
          Status