CLOSER-Cohorts/archivist

View on GitHub
lib/importers/xml/ddi/fragment_instance.rb

Summary

Maintainability
C
1 day
Test Coverage

Class FragmentInstance has 22 methods (exceeds 20 allowed). Consider refactoring.
Open

  class FragmentInstance

    attr_accessor :questions, :errors

    def initialize(xml_fragment, instrument)
Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.rb - About 2 hrs to fix

Method process has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
Open

    def process
      ActiveRecord::Base.transaction do
        parsed_fragment = parse
        raise ActiveRecord::Rollback unless valid?
        create_categories(parsed_fragment[:categories])
Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.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 create_categories has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
Open

    def create_categories(categories=[])
      categories.reverse.each do | category_hash |
        urn = category_hash.fetch(:urn, '')
        parsed_urn = parse_urn(urn)
        if @instrument.prefix != parsed_urn[:instrument_prefix]
Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.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 parse has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

    def parse
      hash = {}
      matchers = { question_item: './QuestionItem', category: './Category', code_list: './CodeList', instruction: './Instruction'}

      @node.xpath('//Fragment').each do | n |
Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.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 create_codes has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

    def create_codes(codes, code_list)
      codes.reverse.each do | code_hash |
        code_hash[:code_list_id] = code_list.id
        urn = code_hash.fetch(:urn, '')
        category_urn = code_hash.delete(:category_urn)
Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.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 create_instructions has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

    def create_instructions(instructions=[])
      instructions.each do | instruction_hash |
        urn = instruction_hash.fetch(:urn, '')
        parsed_urn = parse_urn(urn)
        if @instrument.prefix != parsed_urn[:instrument_prefix]
Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.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 create_code_lists has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

    def create_code_lists(code_lists=[])
      code_lists.reverse.each do | code_list_hash |
        urn = code_list_hash.fetch(:urn, '')
        codes = code_list_hash.delete(:codes)
        parsed_urn = parse_urn(urn)
Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.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 create_question_items has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

    def create_question_items(question_items=[])
      question_items.each do | question_item_hash |
        urn = question_item_hash.fetch(:urn, '')
        parsed_urn = parse_urn(urn)

Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.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 @instrument.prefix != parsed_urn[:instrument_prefix]
          @urn_to_object_mapping[urn] = @instrument.code_lists.create(code_list_hash)
        else
          existing_code_list = @instrument.code_lists.find_by(ddi_slug: parsed_urn[:id])
          if existing_code_list
Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.rb and 1 other location - About 45 mins to fix
lib/importers/xml/ddi/fragment_instance.rb on lines 149..157

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

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 @instrument.prefix != parsed_urn[:instrument_prefix]
          @urn_to_object_mapping[urn] = @instrument.codes.create(code_hash)
        else
          existing_code = @instrument.codes.find_by(ddi_slug: parsed_urn[:id])
          if existing_code
Severity: Minor
Found in lib/importers/xml/ddi/fragment_instance.rb and 1 other location - About 45 mins to fix
lib/importers/xml/ddi/fragment_instance.rb on lines 127..135

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

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

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

            return

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

There are no issues that match your filters.

Category
Status