ece517-p3/expertiza

View on GitHub
app/helpers/questionnaire_helper.rb

Summary

Maintainability
D
1 day
Test Coverage

Method get_questions_from_csv has a Cognitive Complexity of 62 (exceeds 5 allowed). Consider refactoring.
Open

  def self.get_questions_from_csv(questionnaire, file)
    questions = []
    custom_rubric = questionnaire.section == "Custom"

    CSV::Reader.parse(file) do |row|
Severity: Minor
Found in app/helpers/questionnaire_helper.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 get_questions_from_csv is too high. [43.3/15]
Open

  def self.get_questions_from_csv(questionnaire, file)
    questions = []
    custom_rubric = questionnaire.section == "Custom"

    CSV::Reader.parse(file) do |row|
Severity: Minor
Found in app/helpers/questionnaire_helper.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

Cyclomatic complexity for get_questions_from_csv is too high. [18/6]
Open

  def self.get_questions_from_csv(questionnaire, file)
    questions = []
    custom_rubric = questionnaire.section == "Custom"

    CSV::Reader.parse(file) do |row|
Severity: Minor
Found in app/helpers/questionnaire_helper.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 get_questions_from_csv is too high. [16/7]
Open

  def self.get_questions_from_csv(questionnaire, file)
    questions = []
    custom_rubric = questionnaire.section == "Custom"

    CSV::Reader.parse(file) do |row|
Severity: Minor
Found in app/helpers/questionnaire_helper.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

Assignment Branch Condition size for adjust_advice_size is too high. [23.15/15]
Open

  def self.adjust_advice_size(questionnaire, question)
    # now we only support question advices for scored questions
    if question.is_a?(ScoredQuestion)

      max = questionnaire.max_question_score
Severity: Minor
Found in app/helpers/questionnaire_helper.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 create_questionnaire_csv is too high. [21.75/15]
Open

  def self.create_questionnaire_csv(questionnaire, _user_name)
    csv_data = CSV.generate do |csv|
      for question in questionnaire.questions
        # Each row is formatted as follows
        # Question, question advice (from high score to low), type, weight
Severity: Minor
Found in app/helpers/questionnaire_helper.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

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

  def self.adjust_advice_size(questionnaire, question)
    # now we only support question advices for scored questions
    if question.is_a?(ScoredQuestion)

      max = questionnaire.max_question_score
Severity: Minor
Found in app/helpers/questionnaire_helper.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 get_questions_from_csv has 41 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def self.get_questions_from_csv(questionnaire, file)
    questions = []
    custom_rubric = questionnaire.section == "Custom"

    CSV::Reader.parse(file) do |row|
Severity: Minor
Found in app/helpers/questionnaire_helper.rb - About 1 hr to fix

    Method adjust_advice_size has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

      def self.adjust_advice_size(questionnaire, question)
        # now we only support question advices for scored questions
        if question.is_a?(ScoredQuestion)
    
          max = questionnaire.max_question_score
    Severity: Minor
    Found in app/helpers/questionnaire_helper.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

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

        CSV::Reader.parse(file) do |row|
          unless row.empty?
            i = 0
            score = questionnaire.max_question_score
            q = Question.new
    Severity: Minor
    Found in app/helpers/questionnaire_helper.rb by rubocop

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

      def self.create_questionnaire_csv(questionnaire, _user_name)
        csv_data = CSV.generate do |csv|
          for question in questionnaire.questions
            # Each row is formatted as follows
            # Question, question advice (from high score to low), type, weight
    Severity: Minor
    Found in app/helpers/questionnaire_helper.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

    Avoid more than 3 levels of block nesting.
    Open

                  a = QuestionAdvice.new(score: score, advice: cell.strip) if custom_rubric
    Severity: Minor
    Found in app/helpers/questionnaire_helper.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

                  q_type.q_type = cell.strip if custom_rubric
    Severity: Minor
    Found in app/helpers/questionnaire_helper.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

                  q_type.parameters = cell.strip if cell
    Severity: Minor
    Found in app/helpers/questionnaire_helper.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.

    Prefer each over for.
    Open

          for question in questionnaire.questions
    Severity: Minor
    Found in app/helpers/questionnaire_helper.rb by rubocop

    This cop looks for uses of the for keyword, or each method. The preferred alternative is set in the EnforcedStyle configuration parameter. An each call with a block on a single line is always allowed, however.

    Useless assignment to variable - a.
    Open

                  a = QuestionAdvice.new(score: score, advice: cell.strip) if custom_rubric
    Severity: Minor
    Found in app/helpers/questionnaire_helper.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 a guard clause instead of wrapping the code inside a conditional expression.
    Open

        if question.is_a?(ScoredQuestion)
    Severity: Minor
    Found in app/helpers/questionnaire_helper.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

    end at 43, 0 is not aligned with def at 12, 2.
    Open

    end
    Severity: Minor
    Found in app/helpers/questionnaire_helper.rb by rubocop

    This cop checks whether the end keywords of method definitions are aligned properly.

    Two modes are supported through the EnforcedStyleAlignWith configuration parameter. If it's set to start_of_line (which is the default), the end shall be aligned with the start of the line where the def keyword is. If it's set to def, the end shall be aligned with the def keyword.

    Example: EnforcedStyleAlignWith: startofline (default)

    # bad
    
    private def foo
                end
    
    # good
    
    private def foo
    end

    Example: EnforcedStyleAlignWith: def

    # bad
    
    private def foo
                end
    
    # good
    
    private def foo
            end

    Prefer each over for.
    Open

          for i in (questionnaire.min_question_score..questionnaire.max_question_score)
    Severity: Minor
    Found in app/helpers/questionnaire_helper.rb by rubocop

    This cop looks for uses of the for keyword, or each method. The preferred alternative is set in the EnforcedStyle configuration parameter. An each call with a block on a single line is always allowed, however.

    Prefer each over for.
    Open

            for advice in question.question_advices.sort {|x, y| y.score <=> x.score }
    Severity: Minor
    Found in app/helpers/questionnaire_helper.rb by rubocop

    This cop looks for uses of the for keyword, or each method. The preferred alternative is set in the EnforcedStyle configuration parameter. An each call with a block on a single line is always allowed, however.

    There are no issues that match your filters.

    Category
    Status