ManageIQ/manageiq-gems-pending

View on GitHub
lib/gems/pending/util/xml/xml_patch.rb

Summary

Maintainability
C
1 day
Test Coverage
B
83%

Method miq_patch_elements has a Cognitive Complexity of 33 (exceeds 5 allowed). Consider refactoring.
Open

  def miq_patch_elements(xml, patch, node, direction, stats)
    miq_patch_element_logging(node, direction)
    patch.root.elements[node.to_s].each_element do |e|
      path = e.elements['path'].elements[1]
      data = e.elements['data'].elements[1]
Severity: Minor
Found in lib/gems/pending/util/xml/xml_patch.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

Cyclomatic complexity for miq_patch_elements is too high. [12/11]
Open

  def miq_patch_elements(xml, patch, node, direction, stats)
    miq_patch_element_logging(node, direction)
    patch.root.elements[node.to_s].each_element do |e|
      path = e.elements['path'].elements[1]
      data = e.elements['data'].elements[1]

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 miq_patch_elements has 37 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def miq_patch_elements(xml, patch, node, direction, stats)
    miq_patch_element_logging(node, direction)
    patch.root.elements[node.to_s].each_element do |e|
      path = e.elements['path'].elements[1]
      data = e.elements['data'].elements[1]
Severity: Minor
Found in lib/gems/pending/util/xml/xml_patch.rb - About 1 hr to fix

Method miq_find_diff_element2 has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
Open

  def miq_find_diff_element2(searchNode, xml)
    # Loop over each element to find matching elements at the current level
    xml.each_element do |dataNode|
      comp_rc = miq_same_element(searchNode, dataNode)
      if comp_rc.zero?
Severity: Minor
Found in lib/gems/pending/util/xml/xml_patch.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 miq_patch_process_element has 9 arguments (exceeds 4 allowed). Consider refactoring.
Open

  def miq_patch_process_element(ele, data, path, compare_roots, direction, parent_node, node, xml, stats)
Severity: Major
Found in lib/gems/pending/util/xml/xml_patch.rb - About 1 hr to fix

Method miq_patch_process_element has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
Open

  def miq_patch_process_element(ele, data, path, compare_roots, direction, parent_node, node, xml, stats)
    if compare_roots == true
      ele = xml.root
    else
      if node == :updates || direction == XML_DIFF_DEL
Severity: Minor
Found in lib/gems/pending/util/xml/xml_patch.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

Avoid parameter lists longer than 5 parameters. [9/5]
Open

  def miq_patch_process_element(ele, data, path, compare_roots, direction, parent_node, node, xml, stats)

Checks for methods with too many parameters.

The maximum number of parameters is configurable. Keyword arguments can optionally be excluded from the total count, as they add less complexity than positional or optional parameters.

Any number of arguments for initialize method inside a block of Struct.new and Data.define like this is always allowed:

Struct.new(:one, :two, :three, :four, :five, keyword_init: true) do
  def initialize(one:, two:, three:, four:, five:)
  end
end

This is because checking the number of arguments of the initialize method does not make sense.

NOTE: Explicit block argument &block is not counted to prevent erroneous change that is avoided by making block argument implicit.

Example: Max: 3

# good
def foo(a, b, c = 1)
end

Example: Max: 2

# bad
def foo(a, b, c = 1)
end

Example: CountKeywordArgs: true (default)

# counts keyword args towards the maximum

# bad (assuming Max is 3)
def foo(a, b, c, d: 1)
end

# good (assuming Max is 3)
def foo(a, b, c: 1)
end

Example: CountKeywordArgs: false

# don't count keyword args towards the maximum

# good (assuming Max is 3)
def foo(a, b, c, d: 1)
end

This cop also checks for the maximum number of optional parameters. This can be configured using the MaxOptionalParameters config option.

Example: MaxOptionalParameters: 3 (default)

# good
def foo(a = 1, b = 2, c = 3)
end

Example: MaxOptionalParameters: 2

# bad
def foo(a = 1, b = 2, c = 3)
end

Method miq_patch_elements has 5 arguments (exceeds 4 allowed). Consider refactoring.
Open

  def miq_patch_elements(xml, patch, node, direction, stats)
Severity: Minor
Found in lib/gems/pending/util/xml/xml_patch.rb - About 35 mins to fix

Method miq_patch_update_element has 5 arguments (exceeds 4 allowed). Consider refactoring.
Open

  def miq_patch_update_element(element, data, compare_roots, direction, stats)
Severity: Minor
Found in lib/gems/pending/util/xml/xml_patch.rb - About 35 mins to fix

There are no issues that match your filters.

Category
Status