robertgauld/osm

View on GitHub
lib/osm/badge.rb

Summary

Maintainability
F
5 days
Test Coverage

File badge.rb has 619 lines of code (exceeds 250 allowed). Consider refactoring.
Open

module Osm

  class Badge < Osm::Model
    class Requirement; end # Ensure the constant exists for the validators
    class RequirementModule; end # Ensure the constant exists for the validators
Severity: Major
Found in lib/osm/badge.rb - About 1 day to fix

    Method earnt? has a Cognitive Complexity of 36 (exceeds 5 allowed). Consider refactoring.
    Open

          def earnt?
            if badge.has_levels?
              return earnt > awarded
            else
              return false if (due.eql?(1) && awarded.eql?(1))
    Severity: Minor
    Found in lib/osm/badge.rb - About 5 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 started has a Cognitive Complexity of 28 (exceeds 5 allowed). Consider refactoring.
    Open

          def started
            unless badge.has_levels?
              return started? ? 1 : 0
            end
            unless badge.show_level_letters
    Severity: Minor
    Found in lib/osm/badge.rb - 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 get_summary_for_section has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
    Open

        def self.get_summary_for_section(api, section, term=nil, options={})
          raise Error, 'This method must NOT be called on one of the subclasses(CoreBadge, ChallengeBadge, StagedBadge or ActivityBadge)' unless type.nil?
          require_ability_to(api, :read, :badge, section, options)
          section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
          term_id = (term.nil? ? Osm::Term.get_current_term_for_section(api, section, options) : term).to_i
    Severity: Minor
    Found in lib/osm/badge.rb - About 2 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 get_badges_for_section has 64 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        def self.get_badges_for_section(api, section, section_type=nil, options={})
          raise Error, 'This method must be called on one of the subclasses (CoreBadge, ChallengeBadge, StagedBadge or ActivityBadge)' if type.nil?
          require_ability_to(api, :read, :badge, section, options)
          section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
          section_type ||= section.type
    Severity: Major
    Found in lib/osm/badge.rb - About 2 hrs to fix

      Method update has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

            def update(api)
              raise Osm::ObjectIsInvalid, 'data is invalid' unless valid?
              section = Osm::Section.get(api, section_id)
              require_ability_to(api, :write, :badge, section)
      
      
      Severity: Minor
      Found in lib/osm/badge.rb - 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 get_summary_for_section has 46 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          def self.get_summary_for_section(api, section, term=nil, options={})
            raise Error, 'This method must NOT be called on one of the subclasses(CoreBadge, ChallengeBadge, StagedBadge or ActivityBadge)' unless type.nil?
            require_ability_to(api, :read, :badge, section, options)
            section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
            term_id = (term.nil? ? Osm::Term.get_current_term_for_section(api, section, options) : term).to_i
      Severity: Minor
      Found in lib/osm/badge.rb - About 1 hr to fix

        Method get_module_completion_data has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
        Open

            def self.get_module_completion_data(api, options={})
              cache_key = ['badge_module_completion_data']
              if !options[:no_cache] && cache_exist?(api, cache_key)
                return cache_read(api, cache_key)
              end
        Severity: Minor
        Found in lib/osm/badge.rb - 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 earnt has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
        Open

              def earnt
                unless badge.has_levels?
                  return earnt? ? 1 : 0
                end
        
        
        Severity: Minor
        Found in lib/osm/badge.rb - 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 update has 35 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

              def update(api)
                raise Osm::ObjectIsInvalid, 'data is invalid' unless valid?
                section = Osm::Section.get(api, section_id)
                require_ability_to(api, :write, :badge, section)
        
        
        Severity: Minor
        Found in lib/osm/badge.rb - About 1 hr to fix

          Method get_module_completion_data has 29 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              def self.get_module_completion_data(api, options={})
                cache_key = ['badge_module_completion_data']
                if !options[:no_cache] && cache_exist?(api, cache_key)
                  return cache_read(api, cache_key)
                end
          Severity: Minor
          Found in lib/osm/badge.rb - About 1 hr to fix

            Method earnt? has 28 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                  def earnt?
                    if badge.has_levels?
                      return earnt > awarded
                    else
                      return false if (due.eql?(1) && awarded.eql?(1))
            Severity: Minor
            Found in lib/osm/badge.rb - About 1 hr to fix

              Method started has 26 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                    def started
                      unless badge.has_levels?
                        return started? ? 1 : 0
                      end
                      unless badge.show_level_letters
              Severity: Minor
              Found in lib/osm/badge.rb - About 1 hr to fix

                Method get_badges_for_section has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                    def self.get_badges_for_section(api, section, section_type=nil, options={})
                      raise Error, 'This method must be called on one of the subclasses (CoreBadge, ChallengeBadge, StagedBadge or ActivityBadge)' if type.nil?
                      require_ability_to(api, :read, :badge, section, options)
                      section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
                      section_type ||= section.type
                Severity: Minor
                Found in lib/osm/badge.rb - 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 get_data_for_section has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                    def get_data_for_section(api, section, term=nil, options={})
                      raise Error, 'This method must be called on one of the subclasses (CoreBadge, ChallengeBadge, StagedBadge or ActivityBadge)' if type.nil?
                      Osm::Model.require_ability_to(api, :read, :badge, section, options)
                      section = Osm::Section.get(api, section, options) unless section.is_a?(Osm::Section)
                      term_id = (term.nil? ? Osm::Term.get_current_term_for_section(api, section, options) : term).to_i
                Severity: Minor
                Found in lib/osm/badge.rb - 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

                Avoid too many return statements within this method.
                Open

                          return 0 # No reason we should ever get here
                Severity: Major
                Found in lib/osm/badge.rb - About 30 mins to fix

                  Avoid too many return statements within this method.
                  Open

                              return this_level if (done < this_level && done > previous_level) # this_level has been started (and not finished)
                  Severity: Major
                  Found in lib/osm/badge.rb - About 30 mins to fix

                    Avoid too many return statements within this method.
                    Open

                                return false unless total_gained >= badge.min_requirements_required
                    Severity: Major
                    Found in lib/osm/badge.rb - About 30 mins to fix

                      Avoid too many return statements within this method.
                      Open

                                return 0 if done >= levels[-1]           # No more levels to do
                      Severity: Major
                      Found in lib/osm/badge.rb - About 30 mins to fix

                        Avoid too many return statements within this method.
                        Open

                                      return false unless requirement_met?(c[:id])
                        Severity: Major
                        Found in lib/osm/badge.rb - About 30 mins to fix

                          Avoid too many return statements within this method.
                          Open

                                    return true
                          Severity: Major
                          Found in lib/osm/badge.rb - About 30 mins to fix

                            Avoid too many return statements within this method.
                            Open

                                      return 0 if levels.include?(done)        # Has achieved a level (and not started next )
                            Severity: Major
                            Found in lib/osm/badge.rb - About 30 mins to fix

                              Avoid too many return statements within this method.
                              Open

                                          return false if requires.include?(false) # Only earnt if all combinations are met
                              Severity: Major
                              Found in lib/osm/badge.rb - About 30 mins to fix

                                Avoid too many return statements within this method.
                                Open

                                              return false if requirements[c[:id]].to_i < c[:min]
                                Severity: Major
                                Found in lib/osm/badge.rb - About 30 mins to fix

                                  Method module_completion_data has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                  Open

                                      def self.module_completion_data(api, badge, options={})
                                        fetched_this_time = @module_completion_data.nil? # Flag to ensure we only get the data once (at most) per invocation
                                        @module_completion_data = get_module_completion_data(api, options) if fetched_this_time
                                  
                                        if @module_completion_data[badge.id].nil? && !fetched_this_time
                                  Severity: Minor
                                  Found in lib/osm/badge.rb - 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

                                  Method gained_in_modules has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                  Open

                                        def gained_in_modules
                                          count = {}
                                          badge.modules.each do |mod|
                                            count[mod.id] ||= 0
                                            count[mod.letter] ||= 0
                                  Severity: Minor
                                  Found in lib/osm/badge.rb - 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

                                        def <=>(another)
                                          result = self.badge <=> another.try(:badge)
                                          result = self.letter <=> another.try(:letter) if result == 0
                                          result = self.id <=> another.try(:id) if result == 0
                                          return result
                                  Severity: Minor
                                  Found in lib/osm/badge.rb and 1 other location - About 30 mins to fix
                                  lib/osm/flexi_record.rb on lines 324..328

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

                                  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 ActiveModel::VERSION::MAJOR < 4
                                        attr_accessible :name, :requirement_notes, :requirements, :id, :version, :identifier, :group_name, :latest, :sharing, :user_id, :levels, :modules, :min_modules_required, :min_requirements_required, :add_columns_to_module, :level_requirement, :requires_modules, :other_requirements_required, :badges_required, :show_level_letters
                                      end
                                  Severity: Minor
                                  Found in lib/osm/badge.rb and 1 other location - About 20 mins to fix
                                  lib/osm/activity.rb on lines 72..77

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

                                  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