lib/previous_searches.rb

Summary

Maintainability
C
7 hrs
Test Coverage

Assignment Branch Condition size for short_search_title_from_params is too high. [64.83/15]
Open

    def short_search_title_from_params
      zoom_class = zoom_class_from_controller(params[:controller_name_for_zoom_class])
      plural_item_type = zoom_class_plural_humanize(zoom_class)
      title_parts = []
      title_parts << libt(:search_terms, search_terms: params[:search_terms]) if params[:action] == 'for'
Severity: Minor
Found in lib/previous_searches.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

Method short_search_title_from_params has a Cognitive Complexity of 28 (exceeds 5 allowed). Consider refactoring.
Open

    def short_search_title_from_params
      zoom_class = zoom_class_from_controller(params[:controller_name_for_zoom_class])
      plural_item_type = zoom_class_plural_humanize(zoom_class)
      title_parts = []
      title_parts << libt(:search_terms, search_terms: params[:search_terms]) if params[:action] == 'for'
Severity: Minor
Found in lib/previous_searches.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 save_current_search is too high. [33.27/15]
Open

    def save_current_search
      title, url = short_search_title_from_params, current_request_url
      return if @search.nil? || url.blank?

      if logged_in?
Severity: Minor
Found in lib/previous_searches.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

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

    def save_current_search
      title, url = short_search_title_from_params, current_request_url
      return if @search.nil? || url.blank?

      if logged_in?
Severity: Minor
Found in lib/previous_searches.rb by rubocop

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

    def short_search_title_from_params
      zoom_class = zoom_class_from_controller(params[:controller_name_for_zoom_class])
      plural_item_type = zoom_class_plural_humanize(zoom_class)
      title_parts = []
      title_parts << libt(:search_terms, search_terms: params[:search_terms]) if params[:action] == 'for'
Severity: Minor
Found in lib/previous_searches.rb by rubocop

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

    def short_search_title_from_params
      zoom_class = zoom_class_from_controller(params[:controller_name_for_zoom_class])
      plural_item_type = zoom_class_plural_humanize(zoom_class)
      title_parts = []
      title_parts << libt(:search_terms, search_terms: params[:search_terms]) if params[:action] == 'for'
Severity: Minor
Found in lib/previous_searches.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 short_search_title_from_params is too high. [16/7]
Open

    def short_search_title_from_params
      zoom_class = zoom_class_from_controller(params[:controller_name_for_zoom_class])
      plural_item_type = zoom_class_plural_humanize(zoom_class)
      title_parts = []
      title_parts << libt(:search_terms, search_terms: params[:search_terms]) if params[:action] == 'for'
Severity: Minor
Found in lib/previous_searches.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 move_session_searches_to_current_user is too high. [20.32/15]
Open

    def move_session_searches_to_current_user
      return unless logged_in? && session[:searches].is_a?(Array)
      # session[:searches] is ordered from recent -> oldest
      # reverse it so that when they are entered, their id's
      # are in the correct order
Severity: Minor
Found in lib/previous_searches.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

Method save_current_search has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
Open

    def save_current_search
      title, url = short_search_title_from_params, current_request_url
      return if @search.nil? || url.blank?

      if logged_in?
Severity: Minor
Found in lib/previous_searches.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

Perceived complexity for save_current_search is too high. [9/7]
Open

    def save_current_search
      title, url = short_search_title_from_params, current_request_url
      return if @search.nil? || url.blank?

      if logged_in?
Severity: Minor
Found in lib/previous_searches.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

Method move_session_searches_to_current_user has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

    def move_session_searches_to_current_user
      return unless logged_in? && session[:searches].is_a?(Array)
      # session[:searches] is ordered from recent -> oldest
      # reverse it so that when they are entered, their id's
      # are in the correct order
Severity: Minor
Found in lib/previous_searches.rb - About 45 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 clear_users_previous_searches has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

    def clear_users_previous_searches(id = nil)
      if logged_in?
        id ? current_user.searches.find(id).destroy : current_user.searches.destroy_all
      else
        id ? session[:searches].delete_at(id) : session[:searches] = []
Severity: Minor
Found in lib/previous_searches.rb - About 45 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 using rescue in its modifier form.
Open

      params = params.split('&').delete_if { |param| param =~ /=$/ }.sort.join('&') rescue nil
Severity: Minor
Found in lib/previous_searches.rb by rubocop

This cop checks for uses of rescue in its modifier form.

Example:

# bad
some_method rescue handle_error

# good
begin
  some_method
rescue
  handle_error
end

Avoid using rescue in its modifier form.
Open

      url = url.gsub(/\/$/, '') rescue nil
Severity: Minor
Found in lib/previous_searches.rb by rubocop

This cop checks for uses of rescue in its modifier form.

Example:

# bad
some_method rescue handle_error

# good
begin
  some_method
rescue
  handle_error
end

Use %r around regular expression.
Open

      url = url.gsub(/\/$/, '') rescue nil
Severity: Minor
Found in lib/previous_searches.rb by rubocop

This cop enforces using // or %r around regular expressions.

Example: EnforcedStyle: slashes (default)

# bad
snake_case = %r{^[\dA-Z_]+$}

# bad
regex = %r{
  foo
  (bar)
  (baz)
}x

# good
snake_case = /^[\dA-Z_]+$/

# good
regex = /
  foo
  (bar)
  (baz)
/x

Example: EnforcedStyle: percent_r

# bad
snake_case = /^[\dA-Z_]+$/

# bad
regex = /
  foo
  (bar)
  (baz)
/x

# good
snake_case = %r{^[\dA-Z_]+$}

# good
regex = %r{
  foo
  (bar)
  (baz)
}x

Example: EnforcedStyle: mixed

# bad
snake_case = %r{^[\dA-Z_]+$}

# bad
regex = /
  foo
  (bar)
  (baz)
/x

# good
snake_case = /^[\dA-Z_]+$/

# good
regex = %r{
  foo
  (bar)
  (baz)
}x

Example: AllowInnerSlashes: false (default)

# If `false`, the cop will always recommend using `%r` if one or more
# slashes are found in the regexp string.

# bad
x =~ /home\//

# good
x =~ %r{home/}

Example: AllowInnerSlashes: true

# good
x =~ /home\//

Use the return of the conditional for variable assignment and comparison.
Open

      if logged_in?
        @previous_searches ||=
          current_user.searches.collect do |s|
            { id: s.id, title: s.title, url: s.url }
          end
Severity: Minor
Found in lib/previous_searches.rb by rubocop

Redundant curly braces around a hash parameter.
Open

          session[:searches].unshift({ title: title, url: url })
Severity: Minor
Found in lib/previous_searches.rb by rubocop

This cop checks for braces around the last parameter in a method call if the last parameter is a hash. It supports braces, no_braces and context_dependent styles.

Example: EnforcedStyle: braces

# The `braces` style enforces braces around all method
# parameters that are hashes.

# bad
some_method(x, y, a: 1, b: 2)

# good
some_method(x, y, {a: 1, b: 2})

Example: EnforcedStyle: no_braces (default)

# The `no_braces` style checks that the last parameter doesn't
# have braces around it.

# bad
some_method(x, y, {a: 1, b: 2})

# good
some_method(x, y, a: 1, b: 2)

Example: EnforcedStyle: context_dependent

# The `context_dependent` style checks that the last parameter
# doesn't have braces around it, but requires braces if the
# second to last parameter is also a hash literal.

# bad
some_method(x, y, {a: 1, b: 2})
some_method(x, y, {a: 1, b: 2}, a: 1, b: 2)

# good
some_method(x, y, a: 1, b: 2)
some_method(x, y, {a: 1, b: 2}, {a: 1, b: 2})

Do not use parallel assignment.
Open

      title, url = short_search_title_from_params, current_request_url
Severity: Minor
Found in lib/previous_searches.rb by rubocop

Checks for simple usages of parallel assignment. This will only complain when the number of variables being assigned matched the number of assigning variables.

Example:

# bad
a, b, c = 1, 2, 3
a, b, c = [1, 2, 3]

# good
one, two = *foo
a, b = foo()
a, b = b, a

a = 1
b = 2
c = 3

There are no issues that match your filters.

Category
Status