ManageIQ/manageiq-ui-classic

View on GitHub
app/controllers/chargeback_rate_controller.rb

Summary

Maintainability
D
2 days
Test Coverage
B
87%

Cyclomatic complexity for rate_save_add is too high. [20/11]
Open

  def rate_save_add
    # TODO: rename chargeback_rates_edit and chargeback_rates_new to chargeback_rate_edit and chargeback_rate_new
    assert_privileges(params[:id] ? 'chargeback_rates_edit' : 'chargeback_rates_new')
    id = params[:button] == "save" ? params[:id] : "new"
    return unless load_edit("cbrate_edit__#{id}")

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. Blocks that are calls to builtin iteration methods (e.g. `ary.map{...}) also add one, others are ignored.

def each_child_node(*types)               # count begins: 1
  unless block_given?                     # unless: +1
    return to_enum(__method__, *types)

  children.each do |child|                # each{}: +1
    next unless child.is_a?(Node)         # unless: +1

    yield child if types.empty? ||        # if: +1, ||: +1
                   types.include?(child.type)
  end

  self
end                                       # total: 6

Class ChargebackRateController has 24 methods (exceeds 20 allowed). Consider refactoring.
Open

class ChargebackRateController < ApplicationController
  before_action :check_privileges
  before_action :get_session_data
  after_action :cleanup_action
  after_action :set_session_data
Severity: Minor
Found in app/controllers/chargeback_rate_controller.rb - About 2 hrs to fix

Method rate_save_add has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
Open

  def rate_save_add
    # TODO: rename chargeback_rates_edit and chargeback_rates_new to chargeback_rate_edit and chargeback_rate_new
    assert_privileges(params[:id] ? 'chargeback_rates_edit' : 'chargeback_rates_new')
    id = params[:button] == "save" ? params[:id] : "new"
    return unless load_edit("cbrate_edit__#{id}")
Severity: Minor
Found in app/controllers/chargeback_rate_controller.rb - About 2 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

Method set_rate_details has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
Open

  def set_rate_details
    @edit[:new][:details] = []
    tiers = []
    @rate ||= ChargebackRate.new
    rate_details = @rate.chargeback_rate_details
Severity: Minor
Found in app/controllers/chargeback_rate_controller.rb - About 2 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

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

  def cb_rate_set_record_vars
    @rate_details = []
    @rate_tiers = []
    @edit[:new][:details].each_with_index do |detail, detail_index|
      rate_detail = detail[:id] ? ChargebackRateDetail.find(detail[:id]) : ChargebackRateDetail.new
Severity: Minor
Found in app/controllers/chargeback_rate_controller.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

Cyclomatic complexity for set_rate_details is too high. [12/11]
Open

  def set_rate_details
    @edit[:new][:details] = []
    tiers = []
    @rate ||= ChargebackRate.new
    rate_details = @rate.chargeback_rate_details

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. Blocks that are calls to builtin iteration methods (e.g. `ary.map{...}) also add one, others are ignored.

def each_child_node(*types)               # count begins: 1
  unless block_given?                     # unless: +1
    return to_enum(__method__, *types)

  children.each do |child|                # each{}: +1
    next unless child.is_a?(Node)         # unless: +1

    yield child if types.empty? ||        # if: +1, ||: +1
                   types.include?(child.type)
  end

  self
end                                       # total: 6

Cyclomatic complexity for cb_rate_get_form_vars is too high. [12/11]
Open

  def cb_rate_get_form_vars
    @edit[:new][:description] = params[:description] if params[:description]
    if params[:rate_type]
      @edit[:new][:rate_type] = params[:rate_type]
      set_rate_details

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. Blocks that are calls to builtin iteration methods (e.g. `ary.map{...}) also add one, others are ignored.

def each_child_node(*types)               # count begins: 1
  unless block_given?                     # unless: +1
    return to_enum(__method__, *types)

  children.each do |child|                # each{}: +1
    next unless child.is_a?(Node)         # unless: +1

    yield child if types.empty? ||        # if: +1, ||: +1
                   types.include?(child.type)
  end

  self
end                                       # total: 6

Method rate_save_add has 43 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def rate_save_add
    # TODO: rename chargeback_rates_edit and chargeback_rates_new to chargeback_rate_edit and chargeback_rate_new
    assert_privileges(params[:id] ? 'chargeback_rates_edit' : 'chargeback_rates_new')
    id = params[:button] == "save" ? params[:id] : "new"
    return unless load_edit("cbrate_edit__#{id}")
Severity: Minor
Found in app/controllers/chargeback_rate_controller.rb - About 1 hr to fix

Method cb_rate_get_form_vars has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
Open

  def cb_rate_get_form_vars
    @edit[:new][:description] = params[:description] if params[:description]
    if params[:rate_type]
      @edit[:new][:rate_type] = params[:rate_type]
      set_rate_details
Severity: Minor
Found in app/controllers/chargeback_rate_controller.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

Method set_rate_details has 35 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  def set_rate_details
    @edit[:new][:details] = []
    tiers = []
    @rate ||= ChargebackRate.new
    rate_details = @rate.chargeback_rate_details
Severity: Minor
Found in app/controllers/chargeback_rate_controller.rb - About 1 hr to fix

Method assert_privileges_for_edit has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
Open

  def assert_privileges_for_edit
    feature = case params[:button]
              when "cancel"
                params[:id] ? 'chargeback_rates_new' : 'chargeback_rates_edit'
              when "save", "add"
Severity: Minor
Found in app/controllers/chargeback_rate_controller.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

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

  def delete
    # TODO: this will be unnecessary after `chargeback_rates_delete` gets renamed to `chargeback_rate_delete`
    assert_privileges("chargeback_rates_delete")
    rates = []
    if !params[:id] # showing a list
Severity: Minor
Found in app/controllers/chargeback_rate_controller.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 rate_reset_or_set has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

  def rate_reset_or_set
    # TODO: rename chargeback_rates_edit to chargeback_rate_edit
    assert_privileges('chargeback_rates_edit') if params[:button] == "reset"
    @rate = new_rate_edit? ? ChargebackRate.new : ChargebackRate.find(params[:id])
    if params[:pressed] == 'chargeback_rates_edit' && @rate.default?
Severity: Minor
Found in app/controllers/chargeback_rate_controller.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 edit has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

  def edit
    assert_privileges_for_edit
    @_params[:pressed] ||= 'chargeback_rates_edit'

    @_params[:pressed] ||= 'chargeback_rates_edit'
Severity: Minor
Found in app/controllers/chargeback_rate_controller.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

Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
Open

      %i[per_time per_unit sub_metric].each do |measure|

Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
Open

        %i[fixed_rate variable_rate start finish].each do |field|

Similar blocks of code found in 2 locations. Consider refactoring.
Open

  def rate_cancel
    if params[:id]
      flash_msg = _("Edit of Chargeback Rate \"%{name}\" was cancelled by the user") % {:name => session[:edit][:new][:description]}
    else
      flash_msg = _("Add of new Chargeback Rate was cancelled by the user")
Severity: Minor
Found in app/controllers/chargeback_rate_controller.rb and 1 other location - About 35 mins to fix
app/controllers/miq_policy_controller/policies.rb on lines 4..12

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 41.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

Similar blocks of code found in 2 locations. Consider refactoring.
Open

    case params[:button]
    when "cancel"
      rate_cancel
    when "save", "add"
      rate_save_add
Severity: Minor
Found in app/controllers/chargeback_rate_controller.rb and 1 other location - About 25 mins to fix
app/controllers/miq_alert_controller.rb on lines 82..91

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 35.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

There are no issues that match your filters.

Category
Status