Method set_form_locals
has a Cognitive Complexity of 65 (exceeds 5 allowed). Consider refactoring. Open
def set_form_locals
locals = {}
if x_active_tree == :diagnostics_tree
if @sb[:active_tab] == "diagnostics_cu_repair"
action_url = "cu_repair"
- Read upRead up
- Create a ticketCreate a ticket
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
File ops_controller.rb
has 743 lines of code (exceeds 400 allowed). Consider refactoring. Open
class OpsController < ApplicationController
# Methods for accordions
include Diagnostics
include OpsRbac
include Settings
- Create a ticketCreate a ticket
Class OpsController
has 42 methods (exceeds 20 allowed). Consider refactoring. Open
class OpsController < ApplicationController
# Methods for accordions
include Diagnostics
include OpsRbac
include Settings
- Create a ticketCreate a ticket
Method settings_replace_right_cell
has a Cognitive Complexity of 36 (exceeds 5 allowed). Consider refactoring. Open
def settings_replace_right_cell(nodetype, presenter)
case nodetype
when "ze" # zone edit
# when editing zone in settings tree
if @zone.id.blank?
- Read upRead up
- Create a ticketCreate a ticket
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_form_locals is too high. [39/11] Open
def set_form_locals
locals = {}
if x_active_tree == :diagnostics_tree
if @sb[:active_tab] == "diagnostics_cu_repair"
action_url = "cu_repair"
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
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 set_form_locals
has 83 lines of code (exceeds 25 allowed). Consider refactoring. Open
def set_form_locals
locals = {}
if x_active_tree == :diagnostics_tree
if @sb[:active_tab] == "diagnostics_cu_repair"
action_url = "cu_repair"
- Create a ticketCreate a ticket
Cyclomatic complexity for settings_replace_right_cell is too high. [24/11] Open
def settings_replace_right_cell(nodetype, presenter)
case nodetype
when "ze" # zone edit
# when editing zone in settings tree
if @zone.id.blank?
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
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 set_active_tab_and_node
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
def set_active_tab_and_node
if x_active_tree == :settings_tree
@sb[:active_tab] ||= "settings_server"
end
- Read upRead up
- Create a ticketCreate a ticket
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 change_tab
has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring. Open
def change_tab(new_tab_id = nil)
assert_privileges(x_active_tree == :settings_tree ? "ops_settings" : "ops_diagnostics")
@explorer = true
params[:miq_grid_checks] = []
- Read upRead up
- Create a ticketCreate a ticket
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 explorer is too high. [17/11] Open
def explorer
@explorer = true
@trees = []
return if perfmenu_click?
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
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 settings_replace_right_cell
has 59 lines of code (exceeds 25 allowed). Consider refactoring. Open
def settings_replace_right_cell(nodetype, presenter)
case nodetype
when "ze" # zone edit
# when editing zone in settings tree
if @zone.id.blank?
- Create a ticketCreate a ticket
Cyclomatic complexity for change_tab is too high. [15/11] Open
def change_tab(new_tab_id = nil)
assert_privileges(x_active_tree == :settings_tree ? "ops_settings" : "ops_diagnostics")
@explorer = true
params[:miq_grid_checks] = []
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
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 rbac_replace_right_cell
has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. Open
def rbac_replace_right_cell(nodetype, presenter)
if %w[accordion_select change_tab tree_select].include?(params[:action])
presenter.replace(:ops_tabs, r[:partial => "all_tabs"])
elsif nodetype == "group_seq"
presenter.update(:rbac_details, r[:partial => "ldap_seq_form"])
- Read upRead up
- Create a ticketCreate a ticket
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_active_tab is too high. [14/11] Open
def set_active_tab(nodetype)
node = nodetype.downcase.split("-")
case x_active_tree
when :settings_tree
case node[0]
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
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 explorer
has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring. Open
def explorer
@explorer = true
@trees = []
return if perfmenu_click?
- Read upRead up
- Create a ticketCreate a ticket
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_active_tab_and_node is too high. [13/11] Open
def set_active_tab_and_node
if x_active_tree == :settings_tree
@sb[:active_tab] ||= "settings_server"
end
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
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 get_node_info is too high. [12/11] Open
def get_node_info(treenodeid, show_list = true)
return if params[:cls_id] # no need to do get_node_info if redirected from show_product_update
@nodetype = valid_active_node(treenodeid).split("-").first
@show_list = show_list
if @replace_trees
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
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 change_tab
has 44 lines of code (exceeds 25 allowed). Consider refactoring. Open
def change_tab(new_tab_id = nil)
assert_privileges(x_active_tree == :settings_tree ? "ops_settings" : "ops_diagnostics")
@explorer = true
params[:miq_grid_checks] = []
- Create a ticketCreate a ticket
Method rbac_and_user_make_subarrays
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
def rbac_and_user_make_subarrays
if @set_filter_values.present?
temp1arr = []
@set_filter_values = @set_filter_values.flatten
temp_categories = @set_filter_values.dup
- Read upRead up
- Create a ticketCreate a ticket
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 handle_bottom_cell
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
def handle_bottom_cell(nodetype, presenter, locals)
# Handle bottom cell
if @pages || @in_a_form && locals[:action_url] != "rbac_tags_edit"
if @pages
presenter.hide(:form_buttons_div)
- Read upRead up
- Create a ticketCreate a ticket
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 explorer
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
def explorer
@explorer = true
@trees = []
return if perfmenu_click?
- Create a ticketCreate a ticket
Method custom_toolbar_explorer
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
def custom_toolbar_explorer
if x_tree
if @display == "main" && @record
Mixins::CustomButtons::Result.new(:single)
elsif @lastaction == "show_list"
- Read upRead up
- Create a ticketCreate a ticket
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 rbac_replace_right_cell
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
def rbac_replace_right_cell(nodetype, presenter)
if %w[accordion_select change_tab tree_select].include?(params[:action])
presenter.replace(:ops_tabs, r[:partial => "all_tabs"])
elsif nodetype == "group_seq"
presenter.update(:rbac_details, r[:partial => "ldap_seq_form"])
- Create a ticketCreate a ticket
Method set_active_tab
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
def set_active_tab(nodetype)
node = nodetype.downcase.split("-")
case x_active_tree
when :settings_tree
case node[0]
- Create a ticketCreate a ticket
Method rbac_and_user_make_subarrays
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
def rbac_and_user_make_subarrays
if @set_filter_values.present?
temp1arr = []
@set_filter_values = @set_filter_values.flatten
temp_categories = @set_filter_values.dup
- Create a ticketCreate a ticket
Method set_active_tab
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
def set_active_tab(nodetype)
node = nodetype.downcase.split("-")
case x_active_tree
when :settings_tree
case node[0]
- Read upRead up
- Create a ticketCreate a ticket
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 replace_right_cell
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def replace_right_cell(options = {})
nodetype, replace_trees = options.values_at(:nodetype, :replace_trees)
if params[:pressed] == "custom_button"
presenter = set_custom_button_dialog_presenter(options)
render :json => presenter.for_render
- Read upRead up
- Create a ticketCreate a ticket
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 deeply nested control flow statements. Open
elsif %w[rbac_role_add rbac_role_copy rbac_role_edit].include?(@sb[:action])
action_url = "rbac_role_edit"
record_id = @edit[:role_id] ? @edit[:role_id] : nil
elsif %w[rbac_group_add rbac_group_edit].include?(@sb[:action])
action_url = "rbac_group_edit"
- Create a ticketCreate a ticket
Avoid deeply nested control flow statements. Open
record_id = @edit[:user_id] ? @edit[:user_id] : nil
- Create a ticketCreate a ticket
Avoid deeply nested control flow statements. Open
if %w[ap_copy ap_edit ap_host_edit ap_vm_edit].include?(@sb[:action])
action_url = "ap_edit"
record_id = @edit[:scan_id] ? @edit[:scan_id] : nil
elsif %w[schedule_add schedule_edit].include?(@sb[:action])
action_url = "schedule_edit"
- Create a ticketCreate a ticket
Avoid deeply nested control flow statements. Open
elsif @sb[:active_tab] == "settings_tags" && @sb[:active_subtab] == "settings_co_categories" && @in_a_form
action_url = "category_edit"
record_id = @category.try(:id)
elsif @sb[:active_tab] == "settings_tags" && @sb[:active_subtab] == "settings_label_tag_mapping" && @in_a_form
action_url = "label_tag_mapping_edit"
- Create a ticketCreate a ticket
Method get_node_info
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
def get_node_info(treenodeid, show_list = true)
return if params[:cls_id] # no need to do get_node_info if redirected from show_product_update
@nodetype = valid_active_node(treenodeid).split("-").first
@show_list = show_list
if @replace_trees
- Read upRead up
- Create a ticketCreate a ticket
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 more than 3 levels of block nesting. Open
record_id = @edit[:scan_id] ? @edit[:scan_id] : nil
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for excessive nesting of conditional and looping constructs.
You can configure if blocks are considered using the CountBlocks
option. When set to false
(the default) blocks are not counted
towards the nesting level. Set to true
to count blocks as well.
The maximum level of nesting allowed is configurable.
Avoid more than 3 levels of block nesting. Open
record_id = @edit[:sched_id] ? @edit[:sched_id] : nil
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for excessive nesting of conditional and looping constructs.
You can configure if blocks are considered using the CountBlocks
option. When set to false
(the default) blocks are not counted
towards the nesting level. Set to true
to count blocks as well.
The maximum level of nesting allowed is configurable.
Avoid more than 3 levels of block nesting. Open
record_id = @edit[:zone_id] ? @edit[:zone_id] : nil
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for excessive nesting of conditional and looping constructs.
You can configure if blocks are considered using the CountBlocks
option. When set to false
(the default) blocks are not counted
towards the nesting level. Set to true
to count blocks as well.
The maximum level of nesting allowed is configurable.
Duplicate branch body detected. Open
elsif x_node.split("-").first == "svr" && my_server.id != active_id.to_i
# show only 4 tabs if not on current server node
@selected_server ||= MiqServer.find(@sb[:selected_server_id]) # Reread the server record
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that there are no repeated bodies
within if/unless
, case-when
, case-in
and rescue
constructs.
With IgnoreLiteralBranches: true
, branches are not registered
as offenses if they return a basic literal value (string, symbol,
integer, float, rational, complex, true
, false
, or nil
), or
return an array, hash, regexp or range that only contains one of
the above basic literal values.
With IgnoreConstantBranches: true
, branches are not registered
as offenses if they return a constant value.
Example:
# bad
if foo
do_foo
do_something_else
elsif bar
do_foo
do_something_else
end
# good
if foo || bar
do_foo
do_something_else
end
# bad
case x
when foo
do_foo
when bar
do_foo
else
do_something_else
end
# good
case x
when foo, bar
do_foo
else
do_something_else
end
# bad
begin
do_something
rescue FooError
handle_error
rescue BarError
handle_error
end
# good
begin
do_something
rescue FooError, BarError
handle_error
end
Example: IgnoreLiteralBranches: true
# good
case size
when "small" then 100
when "medium" then 250
when "large" then 1000
else 250
end
Example: IgnoreConstantBranches: true
# good
case size
when "small" then SMALL_SIZE
when "medium" then MEDIUM_SIZE
when "large" then LARGE_SIZE
else MEDIUM_SIZE
end
Wrap expressions with varying precedence with parentheses to avoid ambiguity. Open
if @pages || @in_a_form && locals[:action_url] != "rbac_tags_edit"
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Looks for expressions containing multiple binary operators
where precedence is ambiguous due to lack of parentheses. For example,
in 1 + 2 * 3
, the multiplication will happen before the addition, but
lexically it appears that the addition will happen first.
The cop does not consider unary operators (ie. !a
or -b
) or comparison
operators (ie. a =~ b
) because those are not ambiguous.
NOTE: Ranges are handled by Lint/AmbiguousRange
.
Example:
# bad
a + b * c
a || b && c
a ** b + c
# good (different precedence)
a + (b * c)
a || (b && c)
(a ** b) + c
# good (same precedence)
a + b + c
a * b / c % d
Duplicate branch body detected. Open
elsif %w[zone_delete].include?(params[:pressed])
presenter.replace(:ops_tabs, r[:partial => "all_tabs"])
else
tab = @sb[:active_tab] == 'settings_tags' ? @sb[:active_subtab] : @sb[:active_tab]
presenter[:update_partials][tab] = r[:partial => "#{tab}_tab"]
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that there are no repeated bodies
within if/unless
, case-when
, case-in
and rescue
constructs.
With IgnoreLiteralBranches: true
, branches are not registered
as offenses if they return a basic literal value (string, symbol,
integer, float, rational, complex, true
, false
, or nil
), or
return an array, hash, regexp or range that only contains one of
the above basic literal values.
With IgnoreConstantBranches: true
, branches are not registered
as offenses if they return a constant value.
Example:
# bad
if foo
do_foo
do_something_else
elsif bar
do_foo
do_something_else
end
# good
if foo || bar
do_foo
do_something_else
end
# bad
case x
when foo
do_foo
when bar
do_foo
else
do_something_else
end
# good
case x
when foo, bar
do_foo
else
do_something_else
end
# bad
begin
do_something
rescue FooError
handle_error
rescue BarError
handle_error
end
# good
begin
do_something
rescue FooError, BarError
handle_error
end
Example: IgnoreLiteralBranches: true
# good
case size
when "small" then 100
when "medium" then 250
when "large" then 1000
else 250
end
Example: IgnoreConstantBranches: true
# good
case size
when "small" then SMALL_SIZE
when "medium" then MEDIUM_SIZE
when "large" then LARGE_SIZE
else MEDIUM_SIZE
end