efigence/cache_key_for

View on GitHub
lib/cache_key_for/cache_key_for_helper.rb

Summary

Maintainability
B
6 hrs
Test Coverage

Assignment Branch Condition size for cache_key_for is too high. [76.44/15]
Open

  def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})
    # 1) paginated scope - `maximum/max` database query on page(2) does not work
    # 2) Array doesn't respond to `total_pages`
    max_updated_at = if scoped_collection.respond_to?(:total_pages) || scoped_collection.class == Array
      scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max

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

  def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})
    # 1) paginated scope - `maximum/max` database query on page(2) does not work
    # 2) Array doesn't respond to `total_pages`
    max_updated_at = if scoped_collection.respond_to?(:total_pages) || scoped_collection.class == Array
      scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max

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

  def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})
    # 1) paginated scope - `maximum/max` database query on page(2) does not work
    # 2) Array doesn't respond to `total_pages`
    max_updated_at = if scoped_collection.respond_to?(:total_pages) || scoped_collection.class == Array
      scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max
Severity: Minor
Found in lib/cache_key_for/cache_key_for_helper.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

Perceived complexity for cache_key_for is too high. [19/7]
Open

  def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})
    # 1) paginated scope - `maximum/max` database query on page(2) does not work
    # 2) Array doesn't respond to `total_pages`
    max_updated_at = if scoped_collection.respond_to?(:total_pages) || scoped_collection.class == Array
      scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max

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

  def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})
    # 1) paginated scope - `maximum/max` database query on page(2) does not work
    # 2) Array doesn't respond to `total_pages`
    max_updated_at = if scoped_collection.respond_to?(:total_pages) || scoped_collection.class == Array
      scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max

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

  def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})
    # 1) paginated scope - `maximum/max` database query on page(2) does not work
    # 2) Array doesn't respond to `total_pages`
    max_updated_at = if scoped_collection.respond_to?(:total_pages) || scoped_collection.class == Array
      scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max
Severity: Minor
Found in lib/cache_key_for/cache_key_for_helper.rb - About 1 hr to fix

    Method cache_key_for has 6 arguments (exceeds 4 allowed). Consider refactoring.
    Open

      def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})
    Severity: Minor
    Found in lib/cache_key_for/cache_key_for_helper.rb - About 45 mins to fix

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

        def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})

      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.

      Line is too long. [150/120]
      Open

          digest = Digest::SHA1.hexdigest("#{ids_string}-#{max_updated_at}-#{count}-#{request.subdomains.join('.')}-#{request.path}-#{flat_request_params}")

      Align else with if.
      Open

          else

      This cops checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def keyword, but there are special cases when they should follow the same rules as the alignment of end.

      Example:

      # bad
      if something
        code
       else
        code
      end
      
      # bad
      if something
        code
       elsif something
        code
      end
      
      # good
      if something
        code
      else
        code
      end

      end at 92, 4 is not aligned with if at 81, 26.
      Open

          end

      This cop checks whether the end keywords are aligned properly.

      Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

      If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

      If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

      If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

      Example: EnforcedStyleAlignWith: keyword (default)

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
                 end

      Example: EnforcedStyleAlignWith: variable

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
      end

      Example: EnforcedStyleAlignWith: startofline

      # bad
      
      variable = if true
          end
      
      # good
      
      puts(if true
      end)

      Line is too long. [121/120]
      Open

      #     - cache cache_key_for(@service.datacenters, 'datacenters', current_company.cache_key, Time.now.utc.to_date.to_s) do

      Line is too long. [152/120]
      Open

      # app_name:views/en/datacenters/5bd92bd352e7726d02175752913014711f5d412e/companies/1-20150619101645935901000/2015-06-26/7a6f89a738006a69c1d1e0214e147bab

      Line is too long. [129/120]
      Open

      # * file differentiation and file changes are handled separately by Rails' `cache` helper (md5 is added at the end of cache path)

      Line is too long. [133/120]
      Open

      # * allows to set default page and per_page or sort order or any param in `default_params` to avoid multiple different default caches

      Line is too long. [157/120]
      Open

          # puts "generated cache key digest base: #{ids_string}-#{max_updated_at}-#{count}-#{request.subdomains.join('.')}-#{request.path}-#{flat_request_params}"

      Redundant else-clause.
      Open

          else

      Checks for empty else-clauses, possibly including comments and/or an explicit nil depending on the EnforcedStyle.

      Example: EnforcedStyle: empty

      # warn only on empty else
      
      # bad
      if condition
        statement
      else
      end
      
      # good
      if condition
        statement
      else
        nil
      end
      
      # good
      if condition
        statement
      else
        statement
      end
      
      # good
      if condition
        statement
      end

      Example: EnforcedStyle: nil

      # warn on else with nil in it
      
      # bad
      if condition
        statement
      else
        nil
      end
      
      # good
      if condition
        statement
      else
      end
      
      # good
      if condition
        statement
      else
        statement
      end
      
      # good
      if condition
        statement
      end

      Example: EnforcedStyle: both (default)

      # warn on empty else and else with nil in it
      
      # bad
      if condition
        statement
      else
        nil
      end
      
      # bad
      if condition
        statement
      else
      end
      
      # good
      if condition
        statement
      else
        statement
      end
      
      # good
      if condition
        statement
      end

      Align else with if.
      Open

          else

      This cops checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def keyword, but there are special cases when they should follow the same rules as the alignment of end.

      Example:

      # bad
      if something
        code
       else
        code
      end
      
      # bad
      if something
        code
       elsif something
        code
      end
      
      # good
      if something
        code
      else
        code
      end

      Use %w or %W for an array of words.
      Open

          blacklist_params = ['utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign']

      This cop can check for array literals made up of word-like strings, that are not using the %w() syntax.

      Alternatively, it can check for uses of the %w() syntax, in projects which do not want to include that syntax.

      Configuration option: MinSize If set, arrays with fewer elements than this value will not trigger the cop. For example, a MinSize of 3 will not enforce a style on an array of 2 or fewer elements.

      Example: EnforcedStyle: percent (default)

      # good
      %w[foo bar baz]
      
      # bad
      ['foo', 'bar', 'baz']

      Example: EnforcedStyle: brackets

      # good
      ['foo', 'bar', 'baz']
      
      # bad
      %w[foo bar baz]

      Align elsif with if.
      Open

          elsif scoped_collection.respond_to?(:max) # not paginated Mongoid::Criteria

      This cops checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def keyword, but there are special cases when they should follow the same rules as the alignment of end.

      Example:

      # bad
      if something
        code
       else
        code
      end
      
      # bad
      if something
        code
       elsif something
        code
      end
      
      # good
      if something
        code
      else
        code
      end

      Use 2 (not -20) spaces for indentation.
      Open

            if whitelist_params.empty?

      This cops checks for indentation that doesn't use the specified number of spaces.

      See also the IndentationConsistency cop which is the companion to this one.

      Example:

      # bad
      class A
       def test
        puts 'hello'
       end
      end
      
      # good
      class A
        def test
          puts 'hello'
        end
      end

      Example: IgnoredPatterns: ['^\s*module']

      # bad
      module A
      class B
        def test
        puts 'hello'
        end
      end
      end
      
      # good
      module A
      class B
        def test
          puts 'hello'
        end
      end
      end

      end at 69, 4 is not aligned with if at 63, 12.
      Open

          end

      This cop checks whether the end keywords are aligned properly.

      Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

      If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

      If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

      If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

      Example: EnforcedStyleAlignWith: keyword (default)

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
                 end

      Example: EnforcedStyleAlignWith: variable

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
      end

      Example: EnforcedStyleAlignWith: startofline

      # bad
      
      variable = if true
          end
      
      # good
      
      puts(if true
      end)

      Use 2 (not -6) spaces for indentation.
      Open

            scoped_collection.total_count

      This cops checks for indentation that doesn't use the specified number of spaces.

      See also the IndentationConsistency cop which is the companion to this one.

      Example:

      # bad
      class A
       def test
        puts 'hello'
       end
      end
      
      # good
      class A
        def test
          puts 'hello'
        end
      end

      Example: IgnoredPatterns: ['^\s*module']

      # bad
      module A
      class B
        def test
        puts 'hello'
        end
      end
      end
      
      # good
      module A
      class B
        def test
          puts 'hello'
        end
      end
      end

      end at 62, 4 is not aligned with if at 49, 21.
      Open

          end

      This cop checks whether the end keywords are aligned properly.

      Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

      If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

      If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

      If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

      Example: EnforcedStyleAlignWith: keyword (default)

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
                 end

      Example: EnforcedStyleAlignWith: variable

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
      end

      Example: EnforcedStyleAlignWith: startofline

      # bad
      
      variable = if true
          end
      
      # good
      
      puts(if true
      end)

      Line is too long. [179/120]
      Open

      # * core concept of Russian Doll Caching is touching: `belongs_to :some_model, touch: true`, but if you do not use `touch`, you can still cache safely like this (example in haml):

      Avoid using {...} for multi-line blocks.
      Open

            end.map { |k, v|

      Check for uses of braces or do/end around single line or multi-line blocks.

      Example: EnforcedStyle: linecountbased (default)

      # bad - single line block
      items.each do |item| item / 5 end
      
      # good - single line block
      items.each { |item| item / 5 }
      
      # bad - multi-line block
      things.map { |thing|
        something = thing.some_method
        process(something)
      }
      
      # good - multi-line block
      things.map do |thing|
        something = thing.some_method
        process(something)
      end

      Example: EnforcedStyle: semantic

      # Prefer `do...end` over `{...}` for procedural blocks.
      
      # return value is used/assigned
      # bad
      foo = map do |x|
        x
      end
      puts (map do |x|
        x
      end)
      
      # return value is not used out of scope
      # good
      map do |x|
        x
      end
      
      # Prefer `{...}` over `do...end` for functional blocks.
      
      # return value is not used out of scope
      # bad
      each { |x|
        x
      }
      
      # return value is used/assigned
      # good
      foo = map { |x|
        x
      }
      map { |x|
        x
      }.inspect

      Example: EnforcedStyle: bracesforchaining

      # bad
      words.each do |word|
        word.flip.flop
      end.join("-")
      
      # good
      words.each { |word|
        word.flip.flop
      }.join("-")

      Align elsif with if.
      Open

          elsif scoped_collection.respond_to?(:maximum) # not paginated ActiveRecord::Relation

      This cops checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def keyword, but there are special cases when they should follow the same rules as the alignment of end.

      Example:

      # bad
      if something
        code
       else
        code
      end
      
      # bad
      if something
        code
       elsif something
        code
      end
      
      # good
      if something
        code
      else
        code
      end

      Use 2 (not -15) spaces for indentation.
      Open

            scoped_collection.to_a.map { |i| i.updated_at ? i.updated_at.utc.to_f : 0 }.max

      This cops checks for indentation that doesn't use the specified number of spaces.

      See also the IndentationConsistency cop which is the companion to this one.

      Example:

      # bad
      class A
       def test
        puts 'hello'
       end
      end
      
      # good
      class A
        def test
          puts 'hello'
        end
      end

      Example: IgnoredPatterns: ['^\s*module']

      # bad
      module A
      class B
        def test
        puts 'hello'
        end
      end
      end
      
      # good
      module A
      class B
        def test
          puts 'hello'
        end
      end
      end

      Use 2 (not -11) spaces for indentation.
      Open

            begin
              scoped_collection.ids

      This cops checks for indentation that doesn't use the specified number of spaces.

      See also the IndentationConsistency cop which is the companion to this one.

      Example:

      # bad
      class A
       def test
        puts 'hello'
       end
      end
      
      # good
      class A
        def test
          puts 'hello'
        end
      end

      Example: IgnoredPatterns: ['^\s*module']

      # bad
      module A
      class B
        def test
        puts 'hello'
        end
      end
      end
      
      # good
      module A
      class B
        def test
          puts 'hello'
        end
      end
      end

      Align elsif with if.
      Open

          elsif scoped_collection.respond_to?(:total_entries) # will_paginate

      This cops checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def keyword, but there are special cases when they should follow the same rules as the alignment of end.

      Example:

      # bad
      if something
        code
       else
        code
      end
      
      # bad
      if something
        code
       elsif something
        code
      end
      
      # good
      if something
        code
      else
        code
      end

      Align else with if.
      Open

          else # Array or not paginated scope

      This cops checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def keyword, but there are special cases when they should follow the same rules as the alignment of end.

      Example:

      # bad
      if something
        code
       else
        code
      end
      
      # bad
      if something
        code
       elsif something
        code
      end
      
      # good
      if something
        code
      else
        code
      end

      Prefer single-quoted strings when you don't need string interpolation or special symbols.
      Open

              Rails.logger.debug "[CacheKeyForHelper] Fallback to array (ActiveRecord::EagerLoadPolymorphicError)"

      Checks if uses of quotes match the configured preference.

      Example: EnforcedStyle: single_quotes (default)

      # bad
      "No special symbols"
      "No string interpolation"
      "Just text"
      
      # good
      'No special symbols'
      'No string interpolation'
      'Just text'
      "Wait! What's #{this}!"

      Example: EnforcedStyle: double_quotes

      # bad
      'Just some text'
      'No special chars or interpolation'
      
      # good
      "Just some text"
      "No special chars or interpolation"
      "Every string in #{project} uses double_quotes"

      Line is too long. [142/120]
      Open

        def cache_key_for(scoped_collection, collection_prefix, cache_owner_cache_key = '', suffix = '', whitelist_params = [], default_params = {})

      Prefer single-quoted strings when you don't need string interpolation or special symbols.
      Open

              Rails.logger.debug "[CacheKeyForHelper] Fallback to array (ActiveRecord::EagerLoadPolymorphicError)"

      Checks if uses of quotes match the configured preference.

      Example: EnforcedStyle: single_quotes (default)

      # bad
      "No special symbols"
      "No string interpolation"
      "Just text"
      
      # good
      'No special symbols'
      'No string interpolation'
      'Just text'
      "Wait! What's #{this}!"

      Example: EnforcedStyle: double_quotes

      # bad
      'Just some text'
      'No special chars or interpolation'
      
      # good
      "Just some text"
      "No special chars or interpolation"
      "Every string in #{project} uses double_quotes"

      end at 79, 4 is not aligned with if at 70, 17.
      Open

          end

      This cop checks whether the end keywords are aligned properly.

      Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

      If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

      If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

      If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

      Example: EnforcedStyleAlignWith: keyword (default)

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
                 end

      Example: EnforcedStyleAlignWith: variable

      # bad
      
      variable = if true
          end
      
      # good
      
      variable = if true
      end

      Example: EnforcedStyleAlignWith: startofline

      # bad
      
      variable = if true
          end
      
      # good
      
      puts(if true
      end)

      There are no issues that match your filters.

      Category
      Status