ikuseiGmbH/smart-village-app-cms

View on GitHub
app/services/importer/waste_calendar.rb

Summary

Maintainability
B
5 hrs
Test Coverage

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

  def perform
    @tour_data.each do |tour_data_line|
      @waste_types.each do |waste_type_key, _waste_type|
        next if @tour_assignment[waste_type_key].blank?
        next if @address_assignment[waste_type_key].blank?
Severity: Minor
Found in app/services/importer/waste_calendar.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

Assignment Branch Condition size for perform is too high. [28.5/15]
Open

  def perform
    @tour_data.each do |tour_data_line|
      @waste_types.each do |waste_type_key, _waste_type|
        next if @tour_assignment[waste_type_key].blank?
        next if @address_assignment[waste_type_key].blank?

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 and https://en.wikipedia.org/wiki/ABC_Software_Metric.

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

  def perform
    @tour_data.each do |tour_data_line|
      @waste_types.each do |waste_type_key, _waste_type|
        next if @tour_assignment[waste_type_key].blank?
        next if @address_assignment[waste_type_key].blank?

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

    def send_single_pick_up_time(date, waste_type, street, zip, city)
      results = @smart_village.query <<~GRAPHQL
        mutation {
          createWastePickUpTime(
            pickupDate: "#{date}",

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.

Complex method Importer::WasteCalendar#perform (43.6)
Open

  def perform
    @tour_data.each do |tour_data_line|
      @waste_types.each do |waste_type_key, _waste_type|
        next if @tour_assignment[waste_type_key].blank?
        next if @address_assignment[waste_type_key].blank?

Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.

You can read more about ABC metrics or the flog tool

Cyclomatic complexity for perform is too high. [10/6]
Open

  def perform
    @tour_data.each do |tour_data_line|
      @waste_types.each do |waste_type_key, _waste_type|
        next if @tour_assignment[waste_type_key].blank?
        next if @address_assignment[waste_type_key].blank?

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

  def perform
    @tour_data.each do |tour_data_line|
      @waste_types.each do |waste_type_key, _waste_type|
        next if @tour_assignment[waste_type_key].blank?
        next if @address_assignment[waste_type_key].blank?

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

Importer::WasteCalendar#perform has approx 17 statements
Open

  def perform

A method with Too Many Statements is any method that has a large number of lines.

Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

So the following method would score +6 in Reek's statement-counting algorithm:

def parse(arg, argv, &error)
  if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
    return nil, block, nil                                         # +1
  end
  opt = (val = parse_arg(val, &error))[1]                          # +2
  val = conv_arg(*val)                                             # +3
  if opt and !arg
    argv.shift                                                     # +4
  else
    val[0] = nil                                                   # +5
  end
  val                                                              # +6
end

(You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

Importer::WasteCalendar#initialize has 6 parameters
Open

  def initialize(

A Long Parameter List occurs when a method has a lot of parameters.

Example

Given

class Dummy
  def long_list(foo,bar,baz,fling,flung)
    puts foo,bar,baz,fling,flung
  end
end

Reek would report the following warning:

test.rb -- 1 warning:
  [2]:Dummy#long_list has 5 parameters (LongParameterList)

A common solution to this problem would be the introduction of parameter objects.

Importer::WasteCalendar#send_single_pick_up_time has 5 parameters
Open

    def send_single_pick_up_time(date, waste_type, street, zip, city)

A Long Parameter List occurs when a method has a lot of parameters.

Example

Given

class Dummy
  def long_list(foo,bar,baz,fling,flung)
    puts foo,bar,baz,fling,flung
  end
end

Reek would report the following warning:

test.rb -- 1 warning:
  [2]:Dummy#long_list has 5 parameters (LongParameterList)

A common solution to this problem would be the introduction of parameter objects.

Importer::WasteCalendar has at least 6 instance variables
Open

class Importer::WasteCalendar

Too Many Instance Variables is a special case of LargeClass.

Example

Given this configuration

TooManyInstanceVariables:
  max_instance_variables: 3

and this code:

class TooManyInstanceVariables
  def initialize
    @arg_1 = :dummy
    @arg_2 = :dummy
    @arg_3 = :dummy
    @arg_4 = :dummy
  end
end

Reek would emit the following warning:

test.rb -- 5 warnings:
  [1]:TooManyInstanceVariables has at least 4 instance variables (TooManyInstanceVariables)

Importer::WasteCalendar#perform contains iterators nested 3 deep
Open

        @address_data.each do |address_data_line|

A Nested Iterator occurs when a block contains another block.

Example

Given

class Duck
  class << self
    def duck_names
      %i!tick trick track!.each do |surname|
        %i!duck!.each do |last_name|
          puts "full name is #{surname} #{last_name}"
        end
      end
    end
  end
end

Reek would report the following warning:

test.rb -- 1 warning:
  [5]:Duck#duck_names contains iterators nested 2 deep (NestedIterators)

Method send_single_pick_up_time has 5 arguments (exceeds 4 allowed). Consider refactoring.
Open

    def send_single_pick_up_time(date, waste_type, street, zip, city)
Severity: Minor
Found in app/services/importer/waste_calendar.rb - About 35 mins to fix

    Importer::WasteCalendar#perform calls '@address_assignment[waste_type_key]' 2 times
    Open

            next if @address_assignment[waste_type_key].blank?
    
            date = tour_data_line.fetch(@tour_assignment["date"], "")
            next if date.blank?
    
    

    Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

    Reek implements a check for Duplicate Method Call.

    Example

    Here's a very much simplified and contrived example. The following method will report a warning:

    def double_thing()
      @other.thing + @other.thing
    end

    One quick approach to silence Reek would be to refactor the code thus:

    def double_thing()
      thing = @other.thing
      thing + thing
    end

    A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

    class Other
      def double_thing()
        thing + thing
      end
    end

    The approach you take will depend on balancing other factors in your code.

    Importer::WasteCalendar#perform calls '@tour_assignment[waste_type_key]' 2 times
    Open

            next if @tour_assignment[waste_type_key].blank?
            next if @address_assignment[waste_type_key].blank?
    
            date = tour_data_line.fetch(@tour_assignment["date"], "")
            next if date.blank?

    Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

    Reek implements a check for Duplicate Method Call.

    Example

    Here's a very much simplified and contrived example. The following method will report a warning:

    def double_thing()
      @other.thing + @other.thing
    end

    One quick approach to silence Reek would be to refactor the code thus:

    def double_thing()
      thing = @other.thing
      thing + thing
    end

    A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

    class Other
      def double_thing()
        thing + thing
      end
    end

    The approach you take will depend on balancing other factors in your code.

    Importer::WasteCalendar has no descriptive comment
    Open

    class Importer::WasteCalendar

    Classes and modules are the units of reuse and release. It is therefore considered good practice to annotate every class and module with a brief comment outlining its responsibilities.

    Example

    Given

    class Dummy
      # Do things...
    end

    Reek would emit the following warning:

    test.rb -- 1 warning:
      [1]:Dummy has no descriptive comment (IrresponsibleModule)

    Fixing this is simple - just an explaining comment:

    # The Dummy class is responsible for ...
    class Dummy
      # Do things...
    end

    Importer::WasteCalendar#address_assignment is a writable attribute
    Open

      attr_accessor :smart_village, :waste_types, :address_data, :tour_data, :address_assignment,

    A class that publishes a setter for an instance variable invites client classes to become too intimate with its inner workings, and in particular with its representation of state.

    The same holds to a lesser extent for getters, but Reek doesn't flag those.

    Example

    Given:

    class Klass
      attr_accessor :dummy
    end

    Reek would emit the following warning:

    reek test.rb
    
    test.rb -- 1 warning:
      [2]:Klass declares the writable attribute dummy (Attribute)

    Importer::WasteCalendar#smart_village is a writable attribute
    Open

      attr_accessor :smart_village, :waste_types, :address_data, :tour_data, :address_assignment,

    A class that publishes a setter for an instance variable invites client classes to become too intimate with its inner workings, and in particular with its representation of state.

    The same holds to a lesser extent for getters, but Reek doesn't flag those.

    Example

    Given:

    class Klass
      attr_accessor :dummy
    end

    Reek would emit the following warning:

    reek test.rb
    
    test.rb -- 1 warning:
      [2]:Klass declares the writable attribute dummy (Attribute)

    Importer::WasteCalendar#tour_data is a writable attribute
    Open

      attr_accessor :smart_village, :waste_types, :address_data, :tour_data, :address_assignment,

    A class that publishes a setter for an instance variable invites client classes to become too intimate with its inner workings, and in particular with its representation of state.

    The same holds to a lesser extent for getters, but Reek doesn't flag those.

    Example

    Given:

    class Klass
      attr_accessor :dummy
    end

    Reek would emit the following warning:

    reek test.rb
    
    test.rb -- 1 warning:
      [2]:Klass declares the writable attribute dummy (Attribute)

    Importer::WasteCalendar#waste_types is a writable attribute
    Open

      attr_accessor :smart_village, :waste_types, :address_data, :tour_data, :address_assignment,

    A class that publishes a setter for an instance variable invites client classes to become too intimate with its inner workings, and in particular with its representation of state.

    The same holds to a lesser extent for getters, but Reek doesn't flag those.

    Example

    Given:

    class Klass
      attr_accessor :dummy
    end

    Reek would emit the following warning:

    reek test.rb
    
    test.rb -- 1 warning:
      [2]:Klass declares the writable attribute dummy (Attribute)

    Importer::WasteCalendar#tour_assignment is a writable attribute
    Open

                    :tour_assignment

    A class that publishes a setter for an instance variable invites client classes to become too intimate with its inner workings, and in particular with its representation of state.

    The same holds to a lesser extent for getters, but Reek doesn't flag those.

    Example

    Given:

    class Klass
      attr_accessor :dummy
    end

    Reek would emit the following warning:

    reek test.rb
    
    test.rb -- 1 warning:
      [2]:Klass declares the writable attribute dummy (Attribute)

    Importer::WasteCalendar#address_data is a writable attribute
    Open

      attr_accessor :smart_village, :waste_types, :address_data, :tour_data, :address_assignment,

    A class that publishes a setter for an instance variable invites client classes to become too intimate with its inner workings, and in particular with its representation of state.

    The same holds to a lesser extent for getters, but Reek doesn't flag those.

    Example

    Given:

    class Klass
      attr_accessor :dummy
    end

    Reek would emit the following warning:

    reek test.rb
    
    test.rb -- 1 warning:
      [2]:Klass declares the writable attribute dummy (Attribute)

    Avoid parameter lists longer than 5 parameters. [6/5]
    Open

      def initialize(
        smart_village:,
        waste_types:,
        address_data:,
        tour_data:,

    This cop checks for methods with too many parameters. The maximum number of parameters is configurable. Keyword arguments can optionally be excluded from the total count.

    Missing top-level class documentation comment.
    Open

    class Importer::WasteCalendar

    This cop checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, or constant definitions.

    The documentation requirement is annulled if the class or module has a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the same for all its children.

    Example:

    # bad
    class Person
      # ...
    end
    
    # good
    # Description/Explanation of Person class
    class Person
      # ...
    end

    There are no issues that match your filters.

    Category
    Status