theforeman/foreman

View on GitHub
app/helpers/audits_helper.rb

Summary

Maintainability
D
2 days
Test Coverage

File audits_helper.rb has 365 lines of code (exceeds 250 allowed). Consider refactoring.
Open

module AuditsHelper
  AUDIT_ADD = 'add'
  AUDIT_REMOVE = 'remove'

  # lookup the Model representing the numerical id and return its label
Severity: Minor
Found in app/helpers/audits_helper.rb - About 4 hrs to fix

    Method id_to_label has a Cognitive Complexity of 32 (exceeds 5 allowed). Consider refactoring.
    Open

      def id_to_label(name, change, audit: @audit, truncate: true)
        return _("N/A") if change.nil?
        case name
          when "ancestry"
            label = change.blank? ? "" : change.split('/').map { |i| Hostgroup.find(i).name rescue _("N/A") }.join('/')
    Severity: Minor
    Found in app/helpers/audits_helper.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 details has a Cognitive Complexity of 28 (exceeds 5 allowed). Consider refactoring.
    Open

      def details(audit, path = audits_path(:search => "id=#{audit.id}"))
        if audit.action == 'update'
          return [] unless audit.audited_changes.present?
          audit.audited_changes.map do |name, change|
            next if change.nil? || change.to_s.empty?
    Severity: Minor
    Found in app/helpers/audits_helper.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 audit_title has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
    Open

      def audit_title(audit)
        type_name = audited_type audit
        case type_name
          when 'Puppet Class'
            (id_to_label audit.audited_changes.keys[0], audit.audited_changes.values[0], audit: audit).to_s
    Severity: Minor
    Found in app/helpers/audits_helper.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 rebuild_audit_changes has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

      def rebuild_audit_changes(audit)
        css_class_name = css_class_by_action(audit.action == 'destroy')
        # update data for created template for better view
        if audit.action == 'create' && (change = audit.audited_changes['template']).present?
          audit.audited_changes['template'] = ['', change]
    Severity: Minor
    Found in app/helpers/audits_helper.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 details has 34 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

      def details(audit, path = audits_path(:search => "id=#{audit.id}"))
        if audit.action == 'update'
          return [] unless audit.audited_changes.present?
          audit.audited_changes.map do |name, change|
            next if change.nil? || change.to_s.empty?
    Severity: Minor
    Found in app/helpers/audits_helper.rb - About 1 hr to fix

      Method id_to_label has 33 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def id_to_label(name, change, audit: @audit, truncate: true)
          return _("N/A") if change.nil?
          case name
            when "ancestry"
              label = change.blank? ? "" : change.split('/').map { |i| Hostgroup.find(i).name rescue _("N/A") }.join('/')
      Severity: Minor
      Found in app/helpers/audits_helper.rb - About 1 hr to fix

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

          def audited_icon(audit)
            style = 'label-info'
            if main_object? audit
              style = case audit.action
                        when 'create'
        Severity: Minor
        Found in app/helpers/audits_helper.rb - About 1 hr to fix

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

            def find_associated_records_using_key(key, change, audit)
              auditable_class = find_auditable_type_class(audit)
              association_class = if key == 'owner_id'
                                    find_owner_class(key, change, audit)
                                  else
          Severity: Minor
          Found in app/helpers/audits_helper.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 find_owner_class has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
          Open

            def find_owner_class(key, change, audit)
              type = nil
              if audit.audited_changes.has_key?('owner_type')
                type = audit.audited_changes['owner_type']
                if audit.action == 'update'
          Severity: Minor
          Found in app/helpers/audits_helper.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 fetch_affected_locations(audit)
              base = (audit.locations.authorized(:view_locations) + (audit.locations & User.current.my_locations)).uniq
              return [] if base.empty?
          
              authorizer = Authorizer.new(User.current, base)
          Severity: Major
          Found in app/helpers/audits_helper.rb and 1 other location - About 1 hr to fix
          app/helpers/audits_helper.rb on lines 338..345

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

          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

            def fetch_affected_organizations(audit)
              base = (audit.organizations.authorized(:view_organizations) + (audit.organizations & User.current.my_organizations)).uniq
              return [] if base.empty?
          
              authorizer = Authorizer.new(User.current, base)
          Severity: Major
          Found in app/helpers/audits_helper.rb and 1 other location - About 1 hr to fix
          app/helpers/audits_helper.rb on lines 327..334

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

          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