ManageIQ/azure-armrest

View on GitHub
lib/azure/armrest/storage/managed_storage_helper.rb

Summary

Maintainability
A
3 hrs
Test Coverage
C
71%

Method read has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
Open

  def read(sas_url, options = {})
    # The same restrictions that apply to the StorageAccount method also apply here.
    range = options[:range] if options[:range]
    range ||= options[:start_byte]..options[:end_byte] if options[:start_byte] && options[:end_byte]
    range ||= options[:start_byte]..options[:start_byte] + options[:length] - 1 if options[:start_byte] && options[:length]
Severity: Minor
Found in lib/azure/armrest/storage/managed_storage_helper.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 read is too high. [14/11]
Open

  def read(sas_url, options = {})
    # The same restrictions that apply to the StorageAccount method also apply here.
    range = options[:range] if options[:range]
    range ||= options[:start_byte]..options[:end_byte] if options[:start_byte] && options[:end_byte]
    range ||= options[:start_byte]..options[:start_byte] + options[:length] - 1 if options[:start_byte] && options[:length]

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

  def read(sas_url, options = {})
    # The same restrictions that apply to the StorageAccount method also apply here.
    range = options[:range] if options[:range]
    range ||= options[:start_byte]..options[:end_byte] if options[:start_byte] && options[:end_byte]
    range ||= options[:start_byte]..options[:start_byte] + options[:length] - 1 if options[:start_byte] && options[:length]
Severity: Minor
Found in lib/azure/armrest/storage/managed_storage_helper.rb - About 1 hr to fix

    The use of Kernel#open is a serious security risk.
    Open

        managed_disk = open(disk_name, resource_group, options)

    Checks for the use of Kernel#open and URI.open with dynamic data.

    Kernel#open and URI.open enable not only file access but also process invocation by prefixing a pipe symbol (e.g., open("| ls")). So, it may lead to a serious security risk by using variable input to the argument of Kernel#open and URI.open. It would be better to use File.open, IO.popen or URI.parse#open explicitly.

    NOTE: open and URI.open with literal strings are not flagged by this cop.

    Safety:

    This cop could register false positives if open is redefined in a class and then used without a receiver in that class.

    Example:

    # bad
    open(something)
    open("| #{something}")
    URI.open(something)
    
    # good
    File.open(something)
    IO.popen(something)
    URI.parse(something).open
    
    # good (literal strings)
    open("foo.text")
    open("| foo")
    URI.open("http://example.com")

    There are no issues that match your filters.

    Category
    Status