lib/workers/dfc_xml_importer_worker.rb

Summary

Maintainability
D
2 days
Test Coverage

Method records_pre_processor has a Cognitive Complexity of 95 (exceeds 5 allowed). Consider refactoring.
Open

  def records_pre_processor
    path_to_records_file_output = @import_dir_path + '/records.xml'
    path_to_dfc_xml_file = @import_dir_path + '/records.dfc.xml'
    path_to_dfc_convertor_log = @import_dir_path + '/convert.log'

Severity: Minor
Found in lib/workers/dfc_xml_importer_worker.rb - About 1 day 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

Assignment Branch Condition size for records_pre_processor is too high. [77.82/15]
Open

  def records_pre_processor
    path_to_records_file_output = @import_dir_path + '/records.xml'
    path_to_dfc_xml_file = @import_dir_path + '/records.dfc.xml'
    path_to_dfc_convertor_log = @import_dir_path + '/convert.log'

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Method has too many lines. [52/10]
Open

  def records_pre_processor
    path_to_records_file_output = @import_dir_path + '/records.xml'
    path_to_dfc_xml_file = @import_dir_path + '/records.dfc.xml'
    path_to_dfc_convertor_log = @import_dir_path + '/convert.log'

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Cyclomatic complexity for records_pre_processor is too high. [19/6]
Open

  def records_pre_processor
    path_to_records_file_output = @import_dir_path + '/records.xml'
    path_to_dfc_xml_file = @import_dir_path + '/records.dfc.xml'
    path_to_dfc_convertor_log = @import_dir_path + '/convert.log'

This cop 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.

Perceived complexity for records_pre_processor is too high. [18/7]
Open

  def records_pre_processor
    path_to_records_file_output = @import_dir_path + '/records.xml'
    path_to_dfc_xml_file = @import_dir_path + '/records.dfc.xml'
    path_to_dfc_convertor_log = @import_dir_path + '/convert.log'

This cop tries to produce a complexity score that's a measure of the complexity the reader experiences when looking at a method. For that reason it considers when nodes as something that doesn't add as much complexity as an if or a &&. Except if it's one of those special case/when constructs where there's no expression after case. Then the cop treats it as an if/elsif/elsif... and lets all the when nodes count. In contrast to the CyclomaticComplexity cop, this cop considers else nodes as adding complexity.

Example:

def my_method                   # 1
  if cond                       # 1
    case var                    # 2 (0.8 + 4 * 0.2, rounded)
    when 1 then func_one
    when 2 then func_two
    when 3 then func_three
    when 4..10 then func_other
    end
  else                          # 1
    do_something until a && b   # 2
  end                           # ===
end                             # 7 complexity points

Block has too many lines. [47/25]
Open

    File.open(path_to_dfc_convertor_log, 'w') do |log|
      # we don't need a separate trimming of fat from the xml file
      # as Nokogiri does that for use in XML building process
      @skip_trimming = true

This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.

Method records_pre_processor has 52 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def records_pre_processor
    path_to_records_file_output = @import_dir_path + '/records.xml'
    path_to_dfc_xml_file = @import_dir_path + '/records.dfc.xml'
    path_to_dfc_convertor_log = @import_dir_path + '/convert.log'

Severity: Major
Found in lib/workers/dfc_xml_importer_worker.rb - About 2 hrs to fix

    Block has too many lines. [39/25]
    Open

            Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
              xml.records do
                rows.each do |row|
                  fields = {}
    
    

    This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.

    Block has too many lines. [37/25]
    Open

              xml.records do
                rows.each do |row|
                  fields = {}
    
                  row.search('field').each do |field|

    This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.

    Block has too many lines. [35/25]
    Open

                rows.each do |row|
                  fields = {}
    
                  row.search('field').each do |field|
                    value = field.inner_text.strip

    This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.

    Unused method argument - args. If it's necessary, use _ or _args as an argument name to indicate that it won't be used. You can also write as create(*) if you want the method to accept any arguments but don't care about them.
    Open

      def create(args = nil)

    This cop checks for unused method arguments.

    Example:

    # bad
    
    def some_method(used, unused, _unused_but_allowed)
      puts used
    end

    Example:

    # good
    
    def some_method(used, _unused, _unused_but_allowed)
      puts used
    end

    Non-local exit from iterator, without return value. next, break, Array#find, Array#any?, etc. is preferred.
    Open

          return if File.exist?(path_to_records_file_output)

    This cop checks for non-local exits from iterators without a return value. It registers an offense under these conditions:

    • No value is returned,
    • the block is preceded by a method chain,
    • the block has arguments,
    • the method which receives the block is not define_method or define_singleton_method,
    • the return is not contained in an inner scope, e.g. a lambda or a method definition.

    Example:

    class ItemApi
      rescue_from ValidationError do |e| # non-iteration block with arg
        return { message: 'validation error' } unless e.errors # allowed
        error_array = e.errors.map do |error| # block with method chain
          return if error.suppress? # warned
          return "#{error.param}: invalid" unless error.message # allowed
          "#{error.param}: #{error.message}"
        end
        { message: 'validation error', errors: error_array }
      end
    
      def update_items
        transaction do # block without arguments
          return unless update_necessary? # allowed
          find_each do |item| # block without method chain
            return if item.stock == 0 # false-negative...
            item.update!(foobar: true)
          end
        end
      end
    end

    Avoid the use of Perl-style backrefs.
    Open

                      xml.Record_Identifier($1.strip.to_i) if fields['Filename'] =~ /(\d+)/

    This cop looks for uses of Perl-style regexp match backreferences like $1, $2, etc.

    Example:

    # bad
    puts $1
    
    # good
    puts Regexp.last_match(1)

    Avoid the use of Perl-style backrefs.
    Open

                      title_parts << "Part #{$1.strip}" if filename_without_ext =~ /([a-z])$/

    This cop looks for uses of Perl-style regexp match backreferences like $1, $2, etc.

    Example:

    # bad
    puts $1
    
    # good
    puts Regexp.last_match(1)

    Do not use parallel assignment.
    Open

                    title_parts, filename_without_ext = [], fields['Filename'].split('.').first

    Checks for simple usages of parallel assignment. This will only complain when the number of variables being assigned matched the number of assigning variables.

    Example:

    # bad
    a, b, c = 1, 2, 3
    a, b, c = [1, 2, 3]
    
    # good
    one, two = *foo
    a, b = foo()
    a, b = b, a
    
    a = 1
    b = 2
    c = 3

    Avoid the use of Perl-style backrefs.
    Open

                      title_parts << "Part #{$1.strip}" if filename_without_ext =~ /([a-z])$/

    This cop looks for uses of Perl-style regexp match backreferences like $1, $2, etc.

    Example:

    # bad
    puts $1
    
    # good
    puts Regexp.last_match(1)

    There are no issues that match your filters.

    Category
    Status