ManageIQ/manageiq-smartstate

View on GitHub
lib/metadata/util/win32/ms-registry.rb

Summary

Maintainability
D
2 days
Test Coverage
B
80%

File ms-registry.rb has 468 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require 'binary_struct'
require 'miq_unicode'
require 'manageiq/gems/pending'
require 'util/miq-xml'
require 'util/xml/xml_hash'
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.rb - About 7 hrs to fix

Class MSRegHive has 29 methods (exceeds 20 allowed). Consider refactoring.
Open

class MSRegHive
  using ManageIQ::UnicodeString

  attr_reader :fileLoadTime, :fileParseTime, :digitalProductKeys, :xmlNode

Severity: Minor
Found in lib/metadata/util/win32/ms-registry.rb - About 3 hrs to fix

Cyclomatic complexity for parseRecordvk is too high. [15/11]
Open

  def parseRecordvk(offset, xmlNode, _fqName, _level)
    # $log.debug "parseRecordVK at offset #{offset}"
    vkHash = REGISTRY_STRUCT_VK.decode(read_buffer(offset, SIZEOF_REGISTRY_STRUCT_VK))
    vkHash[:data_type_display] = KEY_TYPES[vkHash[:data_type]]
    if vkHash[:name_length] == 0

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 validateRegFile has a Cognitive Complexity of 15 (exceeds 8 allowed). Consider refactoring.
Open

  def validateRegFile(fileName)
    t0 = Time.now
    # Do some basic file validation

    fileObj = @fs ? @fs : File
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.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 load_sections has a Cognitive Complexity of 15 (exceeds 8 allowed). Consider refactoring.
Open

  def load_sections(idx)
    if @hbin.key?(idx)
      @stats[:cache_hits] += 1 if DEBUG_FILE_READS
      # If the hash points to data return its index.  Otherwise the hash
      # will point to the index of the starting block of data
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.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 checkFilters has a Cognitive Complexity of 15 (exceeds 8 allowed). Consider refactoring.
Open

  def checkFilters(subKey, fqName, level)
    return true if @filter.nil?  # If there are no filters get out
    match = false
    # allNil = true
    alevel = level - 1
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.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 parseRecordnk has 28 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def parseRecordnk(offset, xmlNode, fqName, level)
    nkHash = REGISTRY_STRUCT_NK.decode(read_buffer(offset, SIZEOF_REGISTRY_STRUCT_NK))
    # Convert the type from hex to text
    nkHash[:type_display] = typeToString(nkHash[:type])
    # Get the keyname which is just beyond the structure
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.rb - About 1 hr to fix

Method parseRecordvk has a Cognitive Complexity of 13 (exceeds 8 allowed). Consider refactoring.
Open

  def parseRecordvk(offset, xmlNode, _fqName, _level)
    # $log.debug "parseRecordVK at offset #{offset}"
    vkHash = REGISTRY_STRUCT_VK.decode(read_buffer(offset, SIZEOF_REGISTRY_STRUCT_VK))
    vkHash[:data_type_display] = KEY_TYPES[vkHash[:data_type]]
    if vkHash[:name_length] == 0
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.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 parseRecordvk has 27 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def parseRecordvk(offset, xmlNode, _fqName, _level)
    # $log.debug "parseRecordVK at offset #{offset}"
    vkHash = REGISTRY_STRUCT_VK.decode(read_buffer(offset, SIZEOF_REGISTRY_STRUCT_VK))
    vkHash[:data_type_display] = KEY_TYPES[vkHash[:data_type]]
    if vkHash[:name_length] == 0
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.rb - About 1 hr to fix

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

  def initialize(path, hiveName, xmlNode, fs = "M:/", filter = nil)
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.rb - About 35 mins to fix

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

  def parseRecordnk(offset, xmlNode, fqName, level)
    nkHash = REGISTRY_STRUCT_NK.decode(read_buffer(offset, SIZEOF_REGISTRY_STRUCT_NK))
    # Convert the type from hex to text
    nkHash[:type_display] = typeToString(nkHash[:type])
    # Get the keyname which is just beyond the structure
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.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 getRegString has a Cognitive Complexity of 10 (exceeds 8 allowed). Consider refactoring.
Open

  def getRegString(vkHash, key_type)
    # $log.debug sprintf("data offset: (0x%X)  Length: [%d]", vkHash['data_offset']+REG_DATA_OFFSET, vkHash['data_length'])
    if (vkHash[:data_length] & 0x80000000) == 0
      vkHash[:data] = read_buffer(vkHash[:data_offset], vkHash[:data_length] - 1)
      begin
Severity: Minor
Found in lib/metadata/util/win32/ms-registry.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

Use sort_by(&:to_s) instead of sort { |a, b| a.to_s <=> b.to_s }.
Open

    hash.sort { |a, b| a.to_s <=> b.to_s }.each { |x, y| $log.debug "#{prefix}(#{x})\t\t= #{y}" }

This cop identifies places where sort { |a, b| a.foo <=> b.foo } can be replaced by sort_by(&:foo). This cop also checks max and min methods.

Example:

# bad
array.sort { |a, b| a.foo <=> b.foo }
array.max { |a, b| a.foo <=> b.foo }
array.min { |a, b| a.foo <=> b.foo }
array.sort { |a, b| a[:foo] <=> b[:foo] }

# good
array.sort_by(&:foo)
array.sort_by { |v| v.foo }
array.sort_by do |var|
  var.foo
end
array.max_by(&:foo)
array.min_by(&:foo)
array.sort_by { |a| a[:foo] }

Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
Open

    if i = vkHash[:data].index("\0")

Checks for assignments in the conditions of if/while/until.

AllowSafeAssignment option for safe assignment. By safe assignment we mean putting parentheses around an assignment to indicate "I know I'm using an assignment as a condition. It's not a mistake."

Safety:

This cop's autocorrection is unsafe because it assumes that the author meant to use an assignment result as a condition.

Example:

# bad
if some_var = true
  do_something
end

# good
if some_var == true
  do_something
end

Example: AllowSafeAssignment: true (default)

# good
if (some_var = true)
  do_something
end

Example: AllowSafeAssignment: false

# bad
if (some_var = true)
  do_something
end

Duplicate branch body detected.
Open

    when type == 4128 then :SUB

Checks that there are no repeated bodies within if/unless, case-when, case-in and rescue constructs.

With IgnoreLiteralBranches: true, branches are not registered as offenses if they return a basic literal value (string, symbol, integer, float, rational, complex, true, false, or nil), or return an array, hash, regexp or range that only contains one of the above basic literal values.

With IgnoreConstantBranches: true, branches are not registered as offenses if they return a constant value.

Example:

# bad
if foo
  do_foo
  do_something_else
elsif bar
  do_foo
  do_something_else
end

# good
if foo || bar
  do_foo
  do_something_else
end

# bad
case x
when foo
  do_foo
when bar
  do_foo
else
  do_something_else
end

# good
case x
when foo, bar
  do_foo
else
  do_something_else
end

# bad
begin
  do_something
rescue FooError
  handle_error
rescue BarError
  handle_error
end

# good
begin
  do_something
rescue FooError, BarError
  handle_error
end

Example: IgnoreLiteralBranches: true

# good
case size
when "small" then 100
when "medium" then 250
when "large" then 1000
else 250
end

Example: IgnoreConstantBranches: true

# good
case size
when "small" then SMALL_SIZE
when "medium" then MEDIUM_SIZE
when "large" then LARGE_SIZE
else MEDIUM_SIZE
end

There are no issues that match your filters.

Category
Status