topfreegames/khan

View on GitHub
models/membership.go

Summary

Maintainability
F
3 days
Test Coverage

File membership.go has 600 lines of code (exceeds 500 allowed). Consider refactoring.
Open

// khan
// https://github.com/topfreegames/khan
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
Severity: Minor
Found in models/membership.go - About 4 hrs to fix

    Function validateMembership has a Cognitive Complexity of 40 (exceeds 20 allowed). Consider refactoring.
    Open

    func validateMembership(db DB, encryptionKey []byte, game *Game, membership *Membership, clan *Clan, playerPublicID, requestorPublicID string) (int64, bool, error) {
        playerID := int64(-1)
        previousMembership := false
        if membership != nil {
            previousMembership = true
    Severity: Minor
    Found in models/membership.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

    Function validateMembership has 56 lines of code (exceeds 50 allowed). Consider refactoring.
    Open

    func validateMembership(db DB, encryptionKey []byte, game *Game, membership *Membership, clan *Clan, playerPublicID, requestorPublicID string) (int64, bool, error) {
        playerID := int64(-1)
        previousMembership := false
        if membership != nil {
            previousMembership = true
    Severity: Minor
    Found in models/membership.go - About 1 hr to fix

      Function ApproveOrDenyMembershipApplication has 12 return statements (exceeds 4 allowed).
      Open

      func ApproveOrDenyMembershipApplication(db DB, encryptionKey []byte, game *Game, gameID, playerPublicID, clanPublicID, requestorPublicID, action string) (*Membership, error) {
          if playerPublicID == requestorPublicID {
              return nil, &PlayerCannotPerformMembershipActionError{action, playerPublicID, clanPublicID, requestorPublicID}
          }
      
      
      Severity: Major
      Found in models/membership.go - About 1 hr to fix

        Function inviteMember has 9 arguments (exceeds 4 allowed). Consider refactoring.
        Open

        func inviteMember(db DB, encryptionKey []byte, game *Game, membership *Membership, level string, clan *Clan, playerID int64, requestorPublicID, message string, previousMembership bool) (*Membership, error) {
        Severity: Major
        Found in models/membership.go - About 1 hr to fix

          Function inviteMember has 10 return statements (exceeds 4 allowed).
          Open

          func inviteMember(db DB, encryptionKey []byte, game *Game, membership *Membership, level string, clan *Clan, playerID int64, requestorPublicID, message string, previousMembership bool) (*Membership, error) {
              reqMembership, _ := GetValidMembershipByClanAndPlayerPublicID(db, game.PublicID, clan.PublicID, requestorPublicID)
              if reqMembership == nil {
                  requestor, err := GetPlayerByPublicID(db, encryptionKey, game.PublicID, requestorPublicID)
                  if err != nil {
          Severity: Major
          Found in models/membership.go - About 1 hr to fix

            Function applyForMembership has 8 arguments (exceeds 4 allowed). Consider refactoring.
            Open

            func applyForMembership(db DB, game *Game, membership *Membership, level string, clan *Clan, playerID int64, requestorPublicID, message string, previousMembership bool) (*Membership, error) {
            Severity: Major
            Found in models/membership.go - About 1 hr to fix

              Function validateMembership has 9 return statements (exceeds 4 allowed).
              Open

              func validateMembership(db DB, encryptionKey []byte, game *Game, membership *Membership, clan *Clan, playerPublicID, requestorPublicID string) (int64, bool, error) {
                  playerID := int64(-1)
                  previousMembership := false
                  if membership != nil {
                      previousMembership = true
              Severity: Major
              Found in models/membership.go - About 55 mins to fix

                Function PromoteOrDemoteMember has 8 return statements (exceeds 4 allowed).
                Open

                func PromoteOrDemoteMember(db DB, game *Game, gameID, playerPublicID, clanPublicID, requestorPublicID, action string) (*Membership, error) {
                    demote := action == "demote"
                    promote := action == "promote"
                
                    levelOffset := game.MinLevelOffsetToDemoteMember
                Severity: Major
                Found in models/membership.go - About 50 mins to fix

                  Avoid deeply nested control flow statements.
                  Open

                              if cd != 0 {
                                  timeToBeReady := cd - int(nowInMilliseconds-membership.UpdatedAt)/1000
                                  if timeToBeReady > 0 {
                                      return -1, false, &MustWaitMembershipCooldownError{timeToBeReady, playerPublicID, clan.PublicID}
                                  }
                  Severity: Major
                  Found in models/membership.go - About 45 mins to fix

                    Function updatePreviousMembershipHelper has 6 arguments (exceeds 4 allowed). Consider refactoring.
                    Open

                    func updatePreviousMembershipHelper(db DB, membership *Membership, level string, requestorID int64, message string, approved bool) (*Membership, error) {
                    Severity: Minor
                    Found in models/membership.go - About 45 mins to fix

                      Function ApproveOrDenyMembershipInvitation has 7 return statements (exceeds 4 allowed).
                      Open

                      func ApproveOrDenyMembershipInvitation(db DB, encryptionKey []byte, game *Game, gameID, playerPublicID, clanPublicID, action string) (*Membership, error) {
                          membership, err := GetValidMembershipByClanAndPlayerPublicID(db, gameID, clanPublicID, playerPublicID)
                          if err != nil {
                              return nil, err
                          }
                      Severity: Major
                      Found in models/membership.go - About 45 mins to fix

                        Function validateMembership has 6 arguments (exceeds 4 allowed). Consider refactoring.
                        Open

                        func validateMembership(db DB, encryptionKey []byte, game *Game, membership *Membership, clan *Clan, playerPublicID, requestorPublicID string) (int64, bool, error) {
                        Severity: Minor
                        Found in models/membership.go - About 45 mins to fix

                          Avoid deeply nested control flow statements.
                          Open

                                      if timeToBeReady > 0 {
                                          return -1, false, &MustWaitMembershipCooldownError{timeToBeReady, playerPublicID, clan.PublicID}
                                      }
                          Severity: Major
                          Found in models/membership.go - About 45 mins to fix

                            Avoid deeply nested control flow statements.
                            Open

                                        if previousInvite && requestorPublicID != playerPublicID {
                                            cd = game.CooldownBeforeInvite
                                        }
                            Severity: Major
                            Found in models/membership.go - About 45 mins to fix

                              Avoid deeply nested control flow statements.
                              Open

                                          if !previousInvite && requestorPublicID == playerPublicID && !memberLeft {
                                              cd = game.CooldownBeforeApply
                                          }
                              Severity: Major
                              Found in models/membership.go - About 45 mins to fix

                                Function CreateMembership has 6 return statements (exceeds 4 allowed).
                                Open

                                func CreateMembership(db DB, encryptionKey []byte, game *Game, gameID, level, playerPublicID, clanPublicID, requestorPublicID, message string) (*Membership, error) {
                                    if _, levelValid := game.MembershipLevels[level]; !levelValid {
                                        return nil, &InvalidLevelForGameError{gameID, level}
                                    }
                                
                                
                                Severity: Major
                                Found in models/membership.go - About 40 mins to fix

                                  Function DeleteMembership has 6 return statements (exceeds 4 allowed).
                                  Open

                                  func DeleteMembership(db DB, game *Game, gameID, playerPublicID, clanPublicID, requestorPublicID string) (*Membership, error) {
                                      membership, err := GetValidMembershipByClanAndPlayerPublicID(db, gameID, clanPublicID, playerPublicID)
                                      if err != nil {
                                          return nil, err
                                      }
                                  Severity: Major
                                  Found in models/membership.go - About 40 mins to fix

                                    Function createMembershipHelper has 5 arguments (exceeds 4 allowed). Consider refactoring.
                                    Open

                                    func createMembershipHelper(db DB, gameID, level string, playerID, clanID, requestorID int64, message string, approved bool) (*Membership, error) {
                                    Severity: Minor
                                    Found in models/membership.go - About 35 mins to fix

                                      Function approveOrDenyMembershipHelper has 5 return statements (exceeds 4 allowed).
                                      Open

                                      func approveOrDenyMembershipHelper(db DB, membership *Membership, action string, performer *Player) (*Membership, error) {
                                          approve := action == approveString
                                          if approve {
                                              membership.Approved = true
                                              membership.ApproverID = sql.NullInt64{Int64: performer.ID, Valid: true}
                                      Severity: Major
                                      Found in models/membership.go - About 35 mins to fix

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

                                        func GetMembershipByClanAndPlayerPublicID(db DB, gameID, clanPublicID, playerPublicID string) (*Membership, error) {
                                            var memberships []*Membership
                                            query := `
                                            SELECT
                                                m.*
                                        Severity: Major
                                        Found in models/membership.go and 1 other location - About 1 hr to fix
                                        models/membership.go on lines 70..90

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

                                        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

                                        func GetValidMembershipByClanAndPlayerPublicID(db DB, gameID, clanPublicID, playerPublicID string) (*Membership, error) {
                                            var memberships []*Membership
                                            query := `
                                            SELECT
                                                m.*
                                        Severity: Major
                                        Found in models/membership.go and 1 other location - About 1 hr to fix
                                        models/membership.go on lines 93..111

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

                                        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

                                        func getLevelByLevelInt(levelInt int, levels map[string]interface{}) string {
                                            for k, v := range levels {
                                                switch v.(type) {
                                                case float64:
                                                    if int(v.(float64)) == levelInt {
                                        Severity: Minor
                                        Found in models/membership.go and 1 other location - About 30 mins to fix
                                        models/clan.go on lines 683..697

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

                                        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