lib/xml_helpers.rb

Summary

Maintainability
C
1 day
Test Coverage

Module has too many lines. [107/100]
Open

module XmlHelpers
  unless included_modules.include? XmlHelpers

    def appropriate_protocol_for(item)
      protocol = 'http'
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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

Method xml_for_related_items has a Cognitive Complexity of 32 (exceeds 5 allowed). Consider refactoring.
Open

    def xml_for_related_items(xml, item, passed_request = nil)
      # comments are the only zoom class without content_item_relations
      return if item.is_a?(Comment)
      protocol = appropriate_protocol_for(item)
      host = !passed_request.nil? ? passed_request[:host] : request.host
Severity: Minor
Found in lib/xml_helpers.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

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

    def xml_for_related_items(xml, item, passed_request = nil)
      # comments are the only zoom class without content_item_relations
      return if item.is_a?(Comment)
      protocol = appropriate_protocol_for(item)
      host = !passed_request.nil? ? passed_request[:host] : request.host
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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.

Assignment Branch Condition size for xml_for_related_items is too high. [39.56/15]
Open

    def xml_for_related_items(xml, item, passed_request = nil)
      # comments are the only zoom class without content_item_relations
      return if item.is_a?(Comment)
      protocol = appropriate_protocol_for(item)
      host = !passed_request.nil? ? passed_request[:host] : request.host
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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

Assignment Branch Condition size for xml_for_media_content_file is too high. [32.7/15]
Open

    def xml_for_media_content_file(xml, item, passed_request = nil)
      # right now StillImage is the only class with thumbnails
      # this may change in the future for videos and documents
      # possibly even audio if there are samples
      # at that point, refactor accordingly
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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

Assignment Branch Condition size for xml_for_thumbnail_image_file is too high. [27.46/15]
Open

    def xml_for_thumbnail_image_file(xml, item, passed_request = nil)
      # right now StillImage is the only class with thumbnails
      # this may change in the future for videos and documents
      # possibly even audio if there are samples
      # at that point, refactor accordingly
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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. [20/10]
Open

    def xml_for_media_content_file(xml, item, passed_request = nil)
      # right now StillImage is the only class with thumbnails
      # this may change in the future for videos and documents
      # possibly even audio if there are samples
      # at that point, refactor accordingly
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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 xml_for_related_items is too high. [11/6]
Open

    def xml_for_related_items(xml, item, passed_request = nil)
      # comments are the only zoom class without content_item_relations
      return if item.is_a?(Comment)
      protocol = appropriate_protocol_for(item)
      host = !passed_request.nil? ? passed_request[:host] : request.host
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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 xml_for_related_items is too high. [12/7]
Open

    def xml_for_related_items(xml, item, passed_request = nil)
      # comments are the only zoom class without content_item_relations
      return if item.is_a?(Comment)
      protocol = appropriate_protocol_for(item)
      host = !passed_request.nil? ? passed_request[:host] : request.host
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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

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

    def xml_enclosure_for_item_with_file(xml, item, host, protocol = nil)
      if (item.respond_to?(:public_filename) && !item.public_filename.blank?) ||
         (item.respond_to?(:original_file) && !item.original_file.blank?)
        protocol = protocol || 'http'
        args = {
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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.

Assignment Branch Condition size for xml_enclosure_for_item_with_file is too high. [19.39/15]
Open

    def xml_enclosure_for_item_with_file(xml, item, host, protocol = nil)
      if (item.respond_to?(:public_filename) && !item.public_filename.blank?) ||
         (item.respond_to?(:original_file) && !item.original_file.blank?)
        protocol = protocol || 'http'
        args = {
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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 xml_for_media_content_file has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
Open

    def xml_for_media_content_file(xml, item, passed_request = nil)
      # right now StillImage is the only class with thumbnails
      # this may change in the future for videos and documents
      # possibly even audio if there are samples
      # at that point, refactor accordingly
Severity: Minor
Found in lib/xml_helpers.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 xml_enclosure_for_item_with_file is too high. [7/6]
Open

    def xml_enclosure_for_item_with_file(xml, item, host, protocol = nil)
      if (item.respond_to?(:public_filename) && !item.public_filename.blank?) ||
         (item.respond_to?(:original_file) && !item.original_file.blank?)
        protocol = protocol || 'http'
        args = {
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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 xml_for_media_content_file is too high. [8/7]
Open

    def xml_for_media_content_file(xml, item, passed_request = nil)
      # right now StillImage is the only class with thumbnails
      # this may change in the future for videos and documents
      # possibly even audio if there are samples
      # at that point, refactor accordingly
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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

Cyclomatic complexity for xml_for_media_content_file is too high. [7/6]
Open

    def xml_for_media_content_file(xml, item, passed_request = nil)
      # right now StillImage is the only class with thumbnails
      # this may change in the future for videos and documents
      # possibly even audio if there are samples
      # at that point, refactor accordingly
Severity: Minor
Found in lib/xml_helpers.rb by rubocop

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.

Method xml_for_related_items has 36 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    def xml_for_related_items(xml, item, passed_request = nil)
      # comments are the only zoom class without content_item_relations
      return if item.is_a?(Comment)
      protocol = appropriate_protocol_for(item)
      host = !passed_request.nil? ? passed_request[:host] : request.host
Severity: Minor
Found in lib/xml_helpers.rb - About 1 hr to fix

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

        def xml_for_thumbnail_image_file(xml, item, passed_request = nil)
          # right now StillImage is the only class with thumbnails
          # this may change in the future for videos and documents
          # possibly even audio if there are samples
          # at that point, refactor accordingly
    Severity: Minor
    Found in lib/xml_helpers.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

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

        def xml_enclosure_for_item_with_file(xml, item, host, protocol = nil)
          if (item.respond_to?(:public_filename) && !item.public_filename.blank?) ||
             (item.respond_to?(:original_file) && !item.original_file.blank?)
            protocol = protocol || 'http'
            args = {
    Severity: Minor
    Found in lib/xml_helpers.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 more than 3 levels of block nesting.
    Open

                totals_hash[class_name.tableize.to_sym] = class_total if class_total > 0
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

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

    Avoid more than 3 levels of block nesting.
    Open

              totals_hash[:topics] = total if total > 0
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

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

    Avoid more than 3 levels of block nesting.
    Open

                options.delete(:conditions) if item.private
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

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

    Use the return of the conditional for variable assignment and comparison.
    Open

          if item.class.name == 'StillImage'
            the_url = "#{host}#{item.original_file.public_filename}"
          else
            the_url = "#{host}#{item.public_filename}"
          end
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    Convert if nested inside else to elsif.
    Open

              totals_hash[:topics] = total if total > 0
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    If the else branch of a conditional consists solely of an if node, it can be combined with the else to become an elsif. This helps to keep the nesting level from getting too deep.

    Example:

    # bad
    if condition_a
      action_a
    else
      if condition_b
        action_b
      else
        action_c
      end
    end
    
    # good
    if condition_a
      action_a
    elsif condition_b
      action_b
    else
      action_c
    end

    Use total.positive? instead of total > 0.
    Open

              totals_hash[:topics] = total if total > 0
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    This cop checks for usage of comparison operators (==, >, <) to test numbers as zero, positive, or negative. These can be replaced by their respective predicate methods. The cop can also be configured to do the reverse.

    The cop disregards #nonzero? as it its value is truthy or falsey, but not true and false, and thus not always interchangeable with != 0.

    The cop ignores comparisons to global variables, since they are often populated with objects which can be compared with integers, but are not themselves Interger polymorphic.

    Example: EnforcedStyle: predicate (default)

    # bad
    
    foo == 0
    0 > foo
    bar.baz > 0
    
    # good
    
    foo.zero?
    foo.negative?
    bar.baz.positive?

    Example: EnforcedStyle: comparison

    # bad
    
    foo.zero?
    foo.negative?
    bar.baz.positive?
    
    # good
    
    foo == 0
    0 > foo
    bar.baz > 0

    Do not use unless with else. Rewrite these with the positive case first.
    Open

          unless item.is_a?(StillImage)
            # original is not available for download to public viewers
            # so skip it, unless we are buildig a private search index record
            # which we can tell by seeing if the version is private
            # (in which case we are building the private search index)
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    This cop looks for unless expressions with else clauses.

    Example:

    # bad
    unless foo_bar.nil?
      # do something...
    else
      # do a different thing...
    end
    
    # good
    if foo_bar.present?
      # do something...
    else
      # do a different thing...
    end

    Useless assignment to variable - protocol.
    Open

          protocol = appropriate_protocol_for(item)
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    This cop checks for every useless assignment to local variable in every scope. The basic idea for this cop was from the warning of ruby -cw:

    assigned but unused variable - foo

    Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.

    Example:

    # bad
    
    def some_method
      some_var = 1
      do_something
    end

    Example:

    # good
    
    def some_method
      some_var = 1
      do_something(some_var)
    end

    Use class_total.positive? instead of class_total > 0.
    Open

                totals_hash[class_name.tableize.to_sym] = class_total if class_total > 0
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    This cop checks for usage of comparison operators (==, >, <) to test numbers as zero, positive, or negative. These can be replaced by their respective predicate methods. The cop can also be configured to do the reverse.

    The cop disregards #nonzero? as it its value is truthy or falsey, but not true and false, and thus not always interchangeable with != 0.

    The cop ignores comparisons to global variables, since they are often populated with objects which can be compared with integers, but are not themselves Interger polymorphic.

    Example: EnforcedStyle: predicate (default)

    # bad
    
    foo == 0
    0 > foo
    bar.baz > 0
    
    # good
    
    foo.zero?
    foo.negative?
    bar.baz.positive?

    Example: EnforcedStyle: comparison

    # bad
    
    foo.zero?
    foo.negative?
    bar.baz.positive?
    
    # good
    
    foo == 0
    0 > foo
    bar.baz > 0

    Use a guard clause instead of wrapping the code inside a conditional expression.
    Open

          unless totals_hash.blank?
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    Use a guard clause instead of wrapping the code inside a conditional expression

    Example:

    # bad
    def test
      if something
        work
      end
    end
    
    # good
    def test
      return unless something
      work
    end
    
    # also good
    def test
      work if something
    end
    
    # bad
    if something
      raise 'exception'
    else
      ok
    end
    
    # good
    raise 'exception' if something
    ok

    Favor if over unless for negative conditions.
    Open

          return unless !item.already_at_blank_version?
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    Checks for uses of if with a negated condition. Only ifs without else are considered. There are three different styles:

    - both
    - prefix
    - postfix

    Example: EnforcedStyle: both (default)

    # enforces `unless` for `prefix` and `postfix` conditionals
    
    # bad
    
    if !foo
      bar
    end
    
    # good
    
    unless foo
      bar
    end
    
    # bad
    
    bar if !foo
    
    # good
    
    bar unless foo

    Example: EnforcedStyle: prefix

    # enforces `unless` for just `prefix` conditionals
    
    # bad
    
    if !foo
      bar
    end
    
    # good
    
    unless foo
      bar
    end
    
    # good
    
    bar if !foo

    Example: EnforcedStyle: postfix

    # enforces `unless` for just `postfix` conditionals
    
    # bad
    
    bar if !foo
    
    # good
    
    bar unless foo
    
    # good
    
    if !foo
      bar
    end

    Useless assignment to variable - protocol.
    Open

          protocol = protocol || appropriate_protocol_for(item)
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    This cop checks for every useless assignment to local variable in every scope. The basic idea for this cop was from the warning of ruby -cw:

    assigned but unused variable - foo

    Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.

    Example:

    # bad
    
    def some_method
      some_var = 1
      do_something
    end

    Example:

    # good
    
    def some_method
      some_var = 1
      do_something(some_var)
    end

    Use total.positive? instead of total > 0.
    Open

          if total > 0
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    This cop checks for usage of comparison operators (==, >, <) to test numbers as zero, positive, or negative. These can be replaced by their respective predicate methods. The cop can also be configured to do the reverse.

    The cop disregards #nonzero? as it its value is truthy or falsey, but not true and false, and thus not always interchangeable with != 0.

    The cop ignores comparisons to global variables, since they are often populated with objects which can be compared with integers, but are not themselves Interger polymorphic.

    Example: EnforcedStyle: predicate (default)

    # bad
    
    foo == 0
    0 > foo
    bar.baz > 0
    
    # good
    
    foo.zero?
    foo.negative?
    bar.baz.positive?

    Example: EnforcedStyle: comparison

    # bad
    
    foo.zero?
    foo.negative?
    bar.baz.positive?
    
    # good
    
    foo == 0
    0 > foo
    bar.baz > 0

    Use self-assignment shorthand ||=.
    Open

          protocol = protocol || appropriate_protocol_for(item)
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    This cop enforces the use the shorthand for self-assignment.

    Example:

    # bad
    x = x + 1
    
    # good
    x += 1

    Use self-assignment shorthand ||=.
    Open

            protocol = protocol || 'http'
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    This cop enforces the use the shorthand for self-assignment.

    Example:

    # bad
    x = x + 1
    
    # good
    x += 1

    Useless assignment to variable - host.
    Open

          host = !passed_request.nil? ? passed_request[:host] : request.host
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    This cop checks for every useless assignment to local variable in every scope. The basic idea for this cop was from the warning of ruby -cw:

    assigned but unused variable - foo

    Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.

    Example:

    # bad
    
    def some_method
      some_var = 1
      do_something
    end

    Example:

    # good
    
    def some_method
      some_var = 1
      do_something(some_var)
    end

    Favor if over unless for negative conditions.
    Open

          return unless !item.already_at_blank_version?
    Severity: Minor
    Found in lib/xml_helpers.rb by rubocop

    Checks for uses of if with a negated condition. Only ifs without else are considered. There are three different styles:

    - both
    - prefix
    - postfix

    Example: EnforcedStyle: both (default)

    # enforces `unless` for `prefix` and `postfix` conditionals
    
    # bad
    
    if !foo
      bar
    end
    
    # good
    
    unless foo
      bar
    end
    
    # bad
    
    bar if !foo
    
    # good
    
    bar unless foo

    Example: EnforcedStyle: prefix

    # enforces `unless` for just `prefix` conditionals
    
    # bad
    
    if !foo
      bar
    end
    
    # good
    
    unless foo
      bar
    end
    
    # good
    
    bar if !foo

    Example: EnforcedStyle: postfix

    # enforces `unless` for just `postfix` conditionals
    
    # bad
    
    bar if !foo
    
    # good
    
    bar unless foo
    
    # good
    
    if !foo
      bar
    end

    There are no issues that match your filters.

    Category
    Status