theforeman/foreman

View on GitHub
app/models/host/base.rb

Summary

Maintainability
F
3 days
Test Coverage

Class Base has 58 methods (exceeds 20 allowed). Consider refactoring.
Open

  class Base < ApplicationRecord
    KERNEL_RELEASE_FACTS = ['kernelrelease', 'ansible_kernel', 'kernel::release', 'uname::release']

    prepend Foreman::STI
    include Authorizable
Severity: Major
Found in app/models/host/base.rb - About 1 day to fix

    File base.rb has 511 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    module Host
      class Base < ApplicationRecord
        KERNEL_RELEASE_FACTS = ['kernelrelease', 'ansible_kernel', 'kernel::release', 'uname::release']
    
        prepend Foreman::STI
    Severity: Major
    Found in app/models/host/base.rb - About 1 day to fix

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

          def set_interfaces(parser)
            # if host has no information in primary interface we try to match it and update it
            # instead of creating new interface, suggested primary interface mac and identifier
            # is saved to primary interface so we match it in updating code below
            if !managed? && primary_interface.mac.blank? && primary_interface.identifier.blank?
      Severity: Minor
      Found in app/models/host/base.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 build_required_interfaces has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

          def build_required_interfaces(attrs = {})
            if primary_interface.nil?
              if interfaces.empty?
                attrs[:type] ||= 'Nic::Managed'
                interfaces.build(attrs.merge(primary: true))
      Severity: Minor
      Found in app/models/host/base.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 set_interface has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
      Open

          def set_interface(attributes, name, iface)
            # update bond.attached_interfaces when interface is in the list and identifier has changed
            update_bonds(iface, name, attributes) if iface.identifier != name && !iface.virtual? && iface.persisted?
            attributes = attributes.clone
            iface.mac = attributes.delete(:macaddress)
      Severity: Minor
      Found in app/models/host/base.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 set_interfaces has 29 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          def set_interfaces(parser)
            # if host has no information in primary interface we try to match it and update it
            # instead of creating new interface, suggested primary interface mac and identifier
            # is saved to primary interface so we match it in updating code below
            if !managed? && primary_interface.mac.blank? && primary_interface.identifier.blank?
      Severity: Minor
      Found in app/models/host/base.rb - About 1 hr to fix

        Method build_values_for_primary_interface! has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

            def build_values_for_primary_interface!(values_for_primary_interface, attributes)
              unless attributes.nil?
                values_for_primary_interface[:name] = NameGenerator.new.next_random_name unless attributes.has_key?(:name)
                PRIMARY_INTERFACE_ATTRIBUTES.each do |attr|
                  values_for_primary_interface[attr] = attributes.delete(attr) if attributes.has_key?(attr)
        Severity: Minor
        Found in app/models/host/base.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_interface_by_flag has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
        Open

            def get_interface_by_flag(flag)
              if new_record?
                interfaces.detect(&flag)
              else
                cache = "@#{flag}_interface"
        Severity: Minor
        Found in app/models/host/base.rb - About 55 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 parse_ip_address has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
        Open

            def parse_ip_address(address, ignore_link_local: true)
              return nil unless address
        
              begin
                addr = IPAddr.new(address)
        Severity: Minor
        Found in app/models/host/base.rb - About 45 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_interface_scope has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
        Open

            def get_interface_scope(name, attributes, base = interfaces)
              case interface_class(name).to_s
                # we search bonds based on identifiers, e.g. ubuntu sets random MAC after each reboot se we can't
                # rely on mac
                when 'Nic::Bond', 'Nic::Bridge'
        Severity: Minor
        Found in app/models/host/base.rb - About 45 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 uniq_interfaces_identifiers has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
        Open

            def uniq_interfaces_identifiers
              success = true
              identifiers = []
              relevant_interfaces = interfaces.select { |i| !i.marked_for_destruction? }
              relevant_interfaces.each do |interface|
        Severity: Minor
        Found in app/models/host/base.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 update_bonds has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
        Open

            def update_bonds(iface, name, attributes)
              bond_interfaces.each do |bond|
                next unless bond.children_mac_addresses.include?(attributes['macaddress'])
                next if bond.attached_devices_identifiers.include? name
                update_bond bond, iface, name
        Severity: Minor
        Found in app/models/host/base.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 update_subnet_from_facts has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

            def update_subnet_from_facts(iface, keep_subnet)
              return if Setting[:update_subnets_from_facts] == 'none' || keep_subnet
              return if Setting[:update_subnets_from_facts] == 'provision' && !iface.provision
              iface.subnet = Subnet.subnet_for(iface.ip) if iface.ip_changed? && !iface.matches_subnet?(:ip, :subnet)
              iface.subnet6 = Subnet.subnet_for(iface.ip6) if iface.ip6_changed? && !iface.matches_subnet?(:ip6, :subnet6)
        Severity: Minor
        Found in app/models/host/base.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 taxonomy_conditions has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

            def self.taxonomy_conditions
              conditions = {}
              if Organization.current.nil? && User.current.present? && !User.current.admin?
                conditions[:organization_id] = User.current.organization_and_child_ids
              else
        Severity: Minor
        Found in app/models/host/base.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 import_host has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

            def self.import_host(hostname, certname = nil)
              raise(::Foreman::Exception.new("Invalid Hostname, must be a String")) unless hostname.is_a?(String)
        
              # downcase everything
              hostname.try(:downcase!)
        Severity: Minor
        Found in app/models/host/base.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

              if Location.current.nil? && User.current.present? && !User.current.admin?
                conditions[:location_id] = User.current.location_and_child_ids
              else
                conditions[:location_id] = Location.current&.subtree_ids
              end
        Severity: Minor
        Found in app/models/host/base.rb and 1 other location - About 15 mins to fix
        app/models/host/base.rb on lines 55..59

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

        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 Organization.current.nil? && User.current.present? && !User.current.admin?
                conditions[:organization_id] = User.current.organization_and_child_ids
              else
                conditions[:organization_id] = Organization.current&.subtree_ids
              end
        Severity: Minor
        Found in app/models/host/base.rb and 1 other location - About 15 mins to fix
        app/models/host/base.rb on lines 60..64

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

        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