app/models/rule_types/activity_rule_type.rb

Summary

Maintainability
A
2 hrs
Test Coverage
A
96%

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

    def available_variables_for_values
      var_names = []

      activity_level_states = package_states
      Analytics::Timeframe.all_variables_builders.each do |timeframe|

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.

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

    def available_variables
      var_names = []

      if package
        var_names.push(*package_states.map(&:code))

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.

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

    def refactor(formula, new_code)
      used_by = super 

      substitions = {
        formula.code+"_current_quarter_values" => new_code+"_current_quarter_values",

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.

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

    def used_by_formulas(formula)
      used = super

      formulas.each do |f| 
        if f.dependencies.include?("#{formula.code}_current_quarter_values")

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

    def available_variables
      var_names = []

      if package
        var_names.push(*package_states.map(&:code))

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

    def available_variables
      var_names = []

      if package
        var_names.push(*package_states.map(&:code))

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

    def fake_facts
      # in case we are in a clone packages a not there so go through long road package_states instead of states

      quarterly_facts = package.monthly? ? package_states.map { |state| ["#{state.code}_quarterly".to_sym, "10"] }.to_h : {}

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.

Method available_variables_for_values has 29 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    def available_variables_for_values
      var_names = []

      activity_level_states = package_states
      Analytics::Timeframe.all_variables_builders.each do |timeframe|
Severity: Minor
Found in app/models/rule_types/activity_rule_type.rb - About 1 hr to fix

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

        def available_variables
          var_names = []
    
          if package
            var_names.push(*package_states.map(&:code))
    Severity: Minor
    Found in app/models/rule_types/activity_rule_type.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 available_variables_for_values has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
    Open

        def available_variables_for_values
          var_names = []
    
          activity_level_states = package_states
          Analytics::Timeframe.all_variables_builders.each do |timeframe|
    Severity: Minor
    Found in app/models/rule_types/activity_rule_type.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

    Method used_by_formulas has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
    Open

        def used_by_formulas(formula)
          used = super
    
          formulas.each do |f| 
            if f.dependencies.include?("#{formula.code}_current_quarter_values")
    Severity: Minor
    Found in app/models/rule_types/activity_rule_type.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

    Do not write to stdout. Use Rails's logger if you want to log.
    Open

              puts "refactoring formula #{used_by_formula.id} : #{used_by_formula.code} := #{used_by_formula.expression} to #{new_expression}"

    This cop checks for the use of output calls like puts and print

    Example:

    # bad
    puts 'A debug message'
    pp 'A debug message'
    print 'A debug message'
    
    # good
    Rails.logger.debug 'A debug message'

    Use safe navigation (&.) instead of checking if an object exists before calling the method.
    Open

          if package.package_rule
            package.package_rule.formulas.each do |f|
              if f.dependencies.include?("#{formula.code}_values")
                used.push(f) 
              end

    This cop transforms usages of a method call safeguarded by a non nil check for the variable whose method is being called to safe navigation (&.). If there is a method chain, all of the methods in the chain need to be checked for safety, and all of the methods will need to be changed to use safe navigation. We have limited the cop to not register an offense for method chains that exceed 2 methods.

    Configuration option: ConvertCodeThatCanStartToReturnNil The default for this is false. When configured to true, this will check for code in the format !foo.nil? && foo.bar. As it is written, the return of this code is limited to false and whatever the return of the method is. If this is converted to safe navigation, foo&.bar can start returning nil as well as what the method returns.

    Example:

    # bad
    foo.bar if foo
    foo.bar.baz if foo
    foo.bar(param1, param2) if foo
    foo.bar { |e| e.something } if foo
    foo.bar(param) { |e| e.something } if foo
    
    foo.bar if !foo.nil?
    foo.bar unless !foo
    foo.bar unless foo.nil?
    
    foo && foo.bar
    foo && foo.bar.baz
    foo && foo.bar(param1, param2)
    foo && foo.bar { |e| e.something }
    foo && foo.bar(param) { |e| e.something }
    
    # good
    foo&.bar
    foo&.bar&.baz
    foo&.bar(param1, param2)
    foo&.bar { |e| e.something }
    foo&.bar(param) { |e| e.something }
    foo && foo.bar.baz.qux # method chain with more than 2 methods
    foo && foo.nil? # method that `nil` responds to
    
    # Method calls that do not use `.`
    foo && foo < bar
    foo < bar if foo
    
    # This could start returning `nil` as well as the return of the method
    foo.nil? || foo.bar
    !foo || foo.bar
    
    # Methods that are used on assignment, arithmetic operation or
    # comparison should not be converted to use safe navigation
    foo.baz = bar if foo
    foo.baz + bar if foo
    foo.bar > 2 if foo

    Trailing whitespace detected.
    Open

          formulas.each do |f| 

    This cop looks for trailing whitespace in the source code.

    Example:

    # The line in this example contains spaces after the 0.
    # bad
    x = 0
    
    # The line in this example ends directly after the 0.
    # good
    x = 0

    Surrounding space missing for operator +.
    Open

            formula.code+"_current_quarter_values" => new_code+"_current_quarter_values",

    Checks that operators have space around them, except for ** which should not have surrounding space.

    Example:

    # bad
    total = 3*4
    "apple"+"juice"
    my_number = 38/4
    a ** b
    
    # good
    total = 3 * 4
    "apple" + "juice"
    my_number = 38 / 4
    a**b

    Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
    Open

              if f.dependencies.include?("#{formula.code}_values")

    Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configured in the Metrics/LineLength cop. The tab size is configured in the IndentationWidth of the Layout/Tab cop.

    Example:

    # bad
    if condition
      do_stuff(bar)
    end
    
    unless qux.empty?
      Foo.do_something
    end
    
    # good
    do_stuff(bar) if condition
    Foo.do_something unless qux.empty?

    Line is too long. [115/100]
    Open

            var_names.push(*activity_level_states.map { |formula| "#{formula.code}_current_quarter_quarterly_values" })

    Surrounding space missing for operator +.
    Open

            formula.code+"_values" => new_code+"_values"

    Checks that operators have space around them, except for ** which should not have surrounding space.

    Example:

    # bad
    total = 3*4
    "apple"+"juice"
    my_number = 38/4
    a ** b
    
    # good
    total = 3 * 4
    "apple" + "juice"
    my_number = 38 / 4
    a**b

    Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
    Open

            if formula.dependencies.include?("#{f.code}_current_quarter_values")

    Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configured in the Metrics/LineLength cop. The tab size is configured in the IndentationWidth of the Layout/Tab cop.

    Example:

    # bad
    if condition
      do_stuff(bar)
    end
    
    unless qux.empty?
      Foo.do_something
    end
    
    # good
    do_stuff(bar) if condition
    Foo.do_something unless qux.empty?

    Trailing whitespace detected.
    Open

          formulas.each do |f| 

    This cop looks for trailing whitespace in the source code.

    Example:

    # The line in this example contains spaces after the 0.
    # bad
    x = 0
    
    # The line in this example ends directly after the 0.
    # good
    x = 0

    Trailing whitespace detected.
    Open

          used_by 

    This cop looks for trailing whitespace in the source code.

    Example:

    # The line in this example contains spaces after the 0.
    # bad
    x = 0
    
    # The line in this example ends directly after the 0.
    # good
    x = 0

    Line is too long. [111/100]
    Open

          # in case we are in a clone packages a not there so go through long road package_states instead of states

    Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
    Open

          if package.monthly?

    Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configured in the Metrics/LineLength cop. The tab size is configured in the IndentationWidth of the Layout/Tab cop.

    Example:

    # bad
    if condition
      do_stuff(bar)
    end
    
    unless qux.empty?
      Foo.do_something
    end
    
    # good
    do_stuff(bar) if condition
    Foo.do_something unless qux.empty?

    Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
    Open

            if f.dependencies.include?("#{formula.code}_current_quarter_values")

    Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configured in the Metrics/LineLength cop. The tab size is configured in the IndentationWidth of the Layout/Tab cop.

    Example:

    # bad
    if condition
      do_stuff(bar)
    end
    
    unless qux.empty?
      Foo.do_something
    end
    
    # good
    do_stuff(bar) if condition
    Foo.do_something unless qux.empty?

    Line is too long. [138/100]
    Open

              puts "refactoring formula #{used_by_formula.id} : #{used_by_formula.code} := #{used_by_formula.expression} to #{new_expression}"

    Surrounding space missing for operator +.
    Open

            formula.code+"_current_quarter_values" => new_code+"_current_quarter_values",

    Checks that operators have space around them, except for ** which should not have surrounding space.

    Example:

    # bad
    total = 3*4
    "apple"+"juice"
    my_number = 38/4
    a ** b
    
    # good
    total = 3 * 4
    "apple" + "juice"
    my_number = 38 / 4
    a**b

    Trailing whitespace detected.
    Open

          

    This cop looks for trailing whitespace in the source code.

    Example:

    # The line in this example contains spaces after the 0.
    # bad
    x = 0
    
    # The line in this example ends directly after the 0.
    # good
    x = 0

    Trailing whitespace detected.
    Open

                used.push(f) 

    This cop looks for trailing whitespace in the source code.

    Example:

    # The line in this example contains spaces after the 0.
    # bad
    x = 0
    
    # The line in this example ends directly after the 0.
    # good
    x = 0

    Trailing whitespace detected.
    Open

          used_by = super 

    This cop looks for trailing whitespace in the source code.

    Example:

    # The line in this example contains spaces after the 0.
    # bad
    x = 0
    
    # The line in this example ends directly after the 0.
    # good
    x = 0

    Surrounding space missing for operator +.
    Open

            formula.code+"_values" => new_code+"_values"

    Checks that operators have space around them, except for ** which should not have surrounding space.

    Example:

    # bad
    total = 3*4
    "apple"+"juice"
    my_number = 38/4
    a ** b
    
    # good
    total = 3 * 4
    "apple" + "juice"
    my_number = 38 / 4
    a**b

    Line is too long. [124/100]
    Open

          quarterly_facts = package.monthly? ? package_states.map { |state| ["#{state.code}_quarterly".to_sym, "10"] }.to_h : {}

    Method parameter must be at least 3 characters long.
    Open

        def push_window_values(result, formula, time_unit, i)

    This cop checks method parameter names for how descriptive they are. It is highly configurable.

    The MinNameLength config option takes an integer. It represents the minimum amount of characters the name must be. Its default is 3. The AllowNamesEndingInNumbers config option takes a boolean. When set to false, this cop will register offenses for names ending with numbers. Its default is false. The AllowedNames config option takes an array of whitelisted names that will never register an offense. The ForbiddenNames config option takes an array of blacklisted names that will always register an offense.

    Example:

    # bad
    def bar(varOne, varTwo)
      varOne + varTwo
    end
    
    # With `AllowNamesEndingInNumbers` set to false
    def foo(num1, num2)
      num1 * num2
    end
    
    # With `MinArgNameLength` set to number greater than 1
    def baz(a, b, c)
      do_stuff(a, b, c)
    end
    
    # good
    def bar(thud, fred)
      thud + fred
    end
    
    def foo(speed, distance)
      speed * distance
    end
    
    def baz(age_a, height_b, gender_c)
      do_stuff(age_a, height_b, gender_c)
    end

    There are no issues that match your filters.

    Category
    Status