ManageIQ/manageiq-smartstate

View on GitHub
lib/metadata/MIQExtract/MIQExtract.rb

Summary

Maintainability
C
1 day
Test Coverage
F
29%

Method initialize has a Cognitive Complexity of 28 (exceeds 8 allowed). Consider refactoring.
Open

  def initialize(filename, ost = nil) # TODO: Always pass in MiqVm
    ost ||= OpenStruct.new
    @ost = ost
    @dataDir = ost.config.try(:dataDir)
    ost.scanData = {} if ost.scanData.nil?
Severity: Minor
Found in lib/metadata/MIQExtract/MIQExtract.rb - 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

Method getProfileData has a Cognitive Complexity of 23 (exceeds 8 allowed). Consider refactoring.
Open

  def getProfileData(&blk)
    # First check for registry keys
    if @systemFs.guestOS == "Windows"
      reg_filters = @scanProfiles.get_registry_filters

Severity: Minor
Found in lib/metadata/MIQExtract/MIQExtract.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

Cyclomatic complexity for initialize is too high. [18/11]
Open

  def initialize(filename, ost = nil) # TODO: Always pass in MiqVm
    ost ||= OpenStruct.new
    @ost = ost
    @dataDir = ost.config.try(:dataDir)
    ost.scanData = {} if ost.scanData.nil?

Checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one. Blocks that are calls to builtin iteration methods (e.g. `ary.map{...}) also add one, others are ignored.

def each_child_node(*types)               # count begins: 1
  unless block_given?                     # unless: +1
    return to_enum(__method__, *types)

  children.each do |child|                # each{}: +1
    next unless child.is_a?(Node)         # unless: +1

    yield child if types.empty? ||        # if: +1, ||: +1
                   types.include?(child.type)
  end

  self
end                                       # total: 6

Cyclomatic complexity for extract is too high. [13/11]
Open

  def extract(category, &blk)
    xml = nil
    Array.wrap(category.presence).each do |c|
      c = c.downcase
      xml = case c

Checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one. Blocks that are calls to builtin iteration methods (e.g. `ary.map{...}) also add one, others are ignored.

def each_child_node(*types)               # count begins: 1
  unless block_given?                     # unless: +1
    return to_enum(__method__, *types)

  children.each do |child|                # each{}: +1
    next unless child.is_a?(Node)         # unless: +1

    yield child if types.empty? ||        # if: +1, ||: +1
                   types.include?(child.type)
  end

  self
end                                       # total: 6

Method initialize has 44 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def initialize(filename, ost = nil) # TODO: Always pass in MiqVm
    ost ||= OpenStruct.new
    @ost = ost
    @dataDir = ost.config.try(:dataDir)
    ost.scanData = {} if ost.scanData.nil?
Severity: Minor
Found in lib/metadata/MIQExtract/MIQExtract.rb - About 1 hr to fix

Method close has a Cognitive Complexity of 10 (exceeds 8 allowed). Consider refactoring.
Open

  def close
    return if @externalMount
    # Call Unmount command if drive was succesfully mounted
    unless @target.nil?
      $log.debug "Unmounting..."
Severity: Minor
Found in lib/metadata/MIQExtract/MIQExtract.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 more than 3 levels of block nesting.
Open

          filters[hive.downcase] = [] if filters[hive.downcase].nil?

Checks for excessive nesting of conditional and looping constructs.

You can configure if blocks are considered using the CountBlocks option. When set to false (the default) blocks are not counted towards the nesting level. Set to true to count blocks as well.

The maximum level of nesting allowed is configurable.

Check block argument explicitly instead of using block_given?.
Open

        yield({:msg => 'Scanning Profile-Registry'}) if block_given?

Do not return from an ensure block.
Open

    return xml

Checks for return from an ensure block. return from an ensure block is a dangerous code smell as it will take precedence over any exception being raised, and the exception will be silently thrown away as if it were rescued.

If you want to rescue some (or all) exceptions, best to do it explicitly

Example:

# bad

def foo
  do_something
ensure
  cleanup
  return self
end

Example:

# good

def foo
  do_something
  self
ensure
  cleanup
end

# also good

def foo
  begin
    do_something
  rescue SomeException
    # Let's ignore this exception
  end
  self
ensure
  cleanup
end

There are no issues that match your filters.

Category
Status