Render path contains parameter value Open
render action: params[:source_form]
- Read upRead up
- Exclude checks
When a call to render
uses a dynamically generated path, template name, file name, or action, there is the possibility that a user can access templates that should be restricted. The issue may be worse if those templates execute code or modify the database.
This warning is shown whenever the path to be rendered is not a static string or symbol.
These warnings are often false positives, however, because it can be difficult to manipulate Rails' assumptions about paths to perform malicious behavior. Reports of dynamic render paths should be checked carefully to see if they can actually be manipulated maliciously by the user.
Possible unprotected redirect Open
redirect_to "/#{@site_basket.urlified_name}"
- Read upRead up
- Exclude checks
Unvalidated redirects and forwards are #10 on the OWASP Top Ten.
Redirects which rely on user-supplied values can be used to "spoof" websites or hide malicious links in otherwise harmless-looking URLs. They can also allow access to restricted areas of a site if the destination is not validated.
Brakeman will raise warnings whenever redirect_to
appears to be used with a user-supplied value that may allow them to change the :host
option.
For example,
redirect_to params.merge(:action => :home)
will create a warning like
Possible unprotected redirect near line 46: redirect_to(params)
This is because params
could contain :host => 'evilsite.com'
which would redirect away from your site and to a malicious site.
If the first argument to redirect_to
is a hash, then adding :only_path => true
will limit the redirect to the current host. Another option is to specify the host explicitly.
redirect_to params.merge(:only_path => true)
redirect_to params.merge(:host => 'myhost.com')
If the first argument is a string, then it is possible to parse the string and extract the path:
redirect_to URI.parse(some_url).path
If the URL does not contain a protocol (e.g., http://
), then you will probably get unexpected results, as redirect_to
will prepend the current host name and a protocol.
Unsafe reflection method constantize called with parameter value Open
@item = @item_class.constantize.new
- Read upRead up
- Exclude checks
Brakeman reports on several cases of remote code execution, in which a user is able to control and execute code in ways unintended by application authors.
The obvious form of this is the use of eval
with user input.
However, Brakeman also reports on dangerous uses of send
, constantize
, and other methods which allow creation of arbitrary objects or calling of arbitrary methods.
Possible unprotected redirect Open
redirect_to "/#{@basket.urlified_name}/"
- Read upRead up
- Exclude checks
Unvalidated redirects and forwards are #10 on the OWASP Top Ten.
Redirects which rely on user-supplied values can be used to "spoof" websites or hide malicious links in otherwise harmless-looking URLs. They can also allow access to restricted areas of a site if the destination is not validated.
Brakeman will raise warnings whenever redirect_to
appears to be used with a user-supplied value that may allow them to change the :host
option.
For example,
redirect_to params.merge(:action => :home)
will create a warning like
Possible unprotected redirect near line 46: redirect_to(params)
This is because params
could contain :host => 'evilsite.com'
which would redirect away from your site and to a malicious site.
If the first argument to redirect_to
is a hash, then adding :only_path => true
will limit the redirect to the current host. Another option is to specify the host explicitly.
redirect_to params.merge(:only_path => true)
redirect_to params.merge(:host => 'myhost.com')
If the first argument is a string, then it is possible to parse the string and extract the path:
redirect_to URI.parse(some_url).path
If the URL does not contain a protocol (e.g., http://
), then you will probably get unexpected results, as redirect_to
will prepend the current host name and a protocol.
Possible unprotected redirect Open
redirect_to "/#{@basket.urlified_name}"
- Read upRead up
- Exclude checks
Unvalidated redirects and forwards are #10 on the OWASP Top Ten.
Redirects which rely on user-supplied values can be used to "spoof" websites or hide malicious links in otherwise harmless-looking URLs. They can also allow access to restricted areas of a site if the destination is not validated.
Brakeman will raise warnings whenever redirect_to
appears to be used with a user-supplied value that may allow them to change the :host
option.
For example,
redirect_to params.merge(:action => :home)
will create a warning like
Possible unprotected redirect near line 46: redirect_to(params)
This is because params
could contain :host => 'evilsite.com'
which would redirect away from your site and to a malicious site.
If the first argument to redirect_to
is a hash, then adding :only_path => true
will limit the redirect to the current host. Another option is to specify the host explicitly.
redirect_to params.merge(:only_path => true)
redirect_to params.merge(:host => 'myhost.com')
If the first argument is a string, then it is possible to parse the string and extract the path:
redirect_to URI.parse(some_url).path
If the URL does not contain a protocol (e.g., http://
), then you will probably get unexpected results, as redirect_to
will prepend the current host name and a protocol.
Unprotected mass assignment Open
@basket = Basket.new(params[:basket])
- Read upRead up
- Exclude checks
Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash.
Example:
User.new(params[:user])
Unfortunately, if there is a user field called admin
which controls administrator access, now any user can make themselves an administrator.
attr_accessible
and attr_protected
can be used to limit mass assignment. However, Brakeman will warn unless attr_accessible
is used, or mass assignment is completely disabled.
There are two different mass assignment warnings which can arise. The first is when mass assignment actually occurs, such as the example above. This results in a warning like
Unprotected mass assignment near line 61: User.new(params[:user])
The other warning is raised whenever a model is found which does not use attr_accessible
. This produces generic warnings like
Mass assignment is not restricted using attr_accessible
with a list of affected models.
In Rails 3.1 and newer, mass assignment can easily be disabled:
config.active_record.whitelist_attributes = true
Unfortunately, it can also easily be bypassed:
User.new(params[:user], :without_protection => true)
Brakeman will warn on uses of without_protection
.
Unprotected mass assignment Open
if @basket.update_attributes(params[:basket])
- Read upRead up
- Exclude checks
Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash.
Example:
User.new(params[:user])
Unfortunately, if there is a user field called admin
which controls administrator access, now any user can make themselves an administrator.
attr_accessible
and attr_protected
can be used to limit mass assignment. However, Brakeman will warn unless attr_accessible
is used, or mass assignment is completely disabled.
There are two different mass assignment warnings which can arise. The first is when mass assignment actually occurs, such as the example above. This results in a warning like
Unprotected mass assignment near line 61: User.new(params[:user])
The other warning is raised whenever a model is found which does not use attr_accessible
. This produces generic warnings like
Mass assignment is not restricted using attr_accessible
with a list of affected models.
In Rails 3.1 and newer, mass assignment can easily be disabled:
config.active_record.whitelist_attributes = true
Unfortunately, it can also easily be bypassed:
User.new(params[:user], :without_protection => true)
Brakeman will warn on uses of without_protection
.
Class has too many lines. [400/100] Open
class BasketsController < ApplicationController
permit 'site_admin or admin of :current_basket', only: %i[
edit update homepage_options destroy
add_index_topic appearance update_appearance
set_settings]
- Read upRead up
- Exclude checks
This cop checks if the length a class exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Assignment Branch Condition size for update is too high. [96.51/15] Open
def update
params[:source_form] ||= 'edit'
params[:basket] ||= {}
@basket = Basket.find(params[:id])
- Read upRead up
- Exclude checks
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
Assignment Branch Condition size for prepare_and_validate_profile_for is too high. [62.06/15] Open
def prepare_and_validate_profile_for(form_type)
# this var is used in form helpers
@form_type = form_type
# we don't run this method is we don't have profile rules
- Read upRead up
- Exclude checks
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
Assignment Branch Condition size for current_value_of is too high. [56.26/15] Open
def current_value_of(name, skip_posted_values = false, form_type = nil)
form_type ||= @form_type
value = nil
- Read upRead up
- Exclude checks
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. [50/10] Open
def update
params[:source_form] ||= 'edit'
params[:basket] ||= {}
@basket = Basket.find(params[:id])
- Read upRead up
- Exclude checks
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.
File baskets_controller.rb
has 402 lines of code (exceeds 250 allowed). Consider refactoring. Open
class BasketsController < ApplicationController
permit 'site_admin or admin of :current_basket', only: %i[
edit update homepage_options destroy
add_index_topic appearance update_appearance
set_settings]
Assignment Branch Condition size for create is too high. [48.19/15] Open
def create
convert_text_fields_to_boolean
# if an site admin makes a basket, make sure the basket is instantly approved
params[:basket][:status] =
- Read upRead up
- Exclude checks
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. [40/10] Open
def current_value_of(name, skip_posted_values = false, form_type = nil)
form_type ||= @form_type
value = nil
- Read upRead up
- Exclude checks
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. [37/10] Open
def create
convert_text_fields_to_boolean
# if an site admin makes a basket, make sure the basket is instantly approved
params[:basket][:status] =
- Read upRead up
- Exclude checks
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.
Assignment Branch Condition size for choose_type is too high. [37.7/15] Open
def choose_type
# give the user the option to add the item to any place the have access to
@basket_list = []
if @site_admin
@basket_list = Basket.list_as_names_and_urlified_names
- Read upRead up
- Exclude checks
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 current_value_of
has a Cognitive Complexity of 28 (exceeds 5 allowed). Consider refactoring. Open
def current_value_of(name, skip_posted_values = false, form_type = nil)
form_type ||= @form_type
value = nil
- Read upRead up
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 has too many lines. [29/10] Open
def choose_type
# give the user the option to add the item to any place the have access to
@basket_list = []
if @site_admin
@basket_list = Basket.list_as_names_and_urlified_names
- Read upRead up
- Exclude checks
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.
Class BasketsController
has 28 methods (exceeds 20 allowed). Consider refactoring. Open
class BasketsController < ApplicationController
permit 'site_admin or admin of :current_basket', only: %i[
edit update homepage_options destroy
add_index_topic appearance update_appearance
set_settings]
Cyclomatic complexity for current_value_of is too high. [20/6] Open
def current_value_of(name, skip_posted_values = false, form_type = nil)
form_type ||= @form_type
value = nil
- Read upRead up
- Exclude checks
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 current_value_of is too high. [21/7] Open
def current_value_of(name, skip_posted_values = false, form_type = nil)
form_type ||= @form_type
value = nil
- Read upRead up
- Exclude checks
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 has too many lines. [23/10] Open
def prepare_and_validate_profile_for(form_type)
# this var is used in form helpers
@form_type = form_type
# we don't run this method is we don't have profile rules
- Read upRead up
- Exclude checks
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.
Assignment Branch Condition size for update_appearance is too high. [27.66/15] Open
def update_appearance
@basket = Basket.find(params[:id])
do_not_sanitize = (params[:settings][:do_not_sanitize_footer_content] == 'true')
original_html = params[:settings][:additional_footer_content]
sanitized_html = original_html
- Read upRead up
- Exclude checks
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
Assignment Branch Condition size for allowed_field? is too high. [26.25/15] Open
def allowed_field?(name)
return true if profile_rules.blank? # no profile mapping
return true if params[:show_all_fields] && @site_admin
return true if profile_rules[@form_type.to_s]['rule_type'] == 'all'
return true if Basket::NESTED_FIELDS.include?(name)
- Read upRead up
- Exclude checks
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 update
has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring. Open
def update
params[:source_form] ||= 'edit'
params[:basket] ||= {}
@basket = Basket.find(params[:id])
- Read upRead up
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 has too many lines. [20/10] Open
def destroy
@basket = Basket.find(params[:id])
# dependent destroy isn't sufficient
# to delete zoom items from the zoom_db
- Read upRead up
- Exclude checks
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.
Assignment Branch Condition size for render_item_form is too high. [25.08/15] Open
def render_item_form
@new_item_basket = params[:new_item_basket]
@new_item_controller = params[:new_item_controller]
@relate_to_item = params[:relate_to_item]
@relate_to_type = params[:relate_to_type]
- Read upRead up
- Exclude checks
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. [18/10] Open
def list_baskets(per_page = 10)
@listing_type =
if !params[:type].blank? && @site_admin
params[:type]
else
- Read upRead up
- Exclude checks
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 destroy
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
def destroy
@basket = Basket.find(params[:id])
# dependent destroy isn't sufficient
# to delete zoom items from the zoom_db
- Read upRead up
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 prepare_and_validate_profile_for
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
def prepare_and_validate_profile_for(form_type)
# this var is used in form helpers
@form_type = form_type
# we don't run this method is we don't have profile rules
- Read upRead up
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 create
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
def create
convert_text_fields_to_boolean
# if an site admin makes a basket, make sure the basket is instantly approved
params[:basket][:status] =
- Read upRead up
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 update is too high. [13/7] Open
def update
params[:source_form] ||= 'edit'
params[:basket] ||= {}
@basket = Basket.find(params[:id])
- Read upRead up
- Exclude checks
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 update is too high. [12/6] Open
def update
params[:source_form] ||= 'edit'
params[:basket] ||= {}
@basket = Basket.find(params[:id])
- Read upRead up
- Exclude checks
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 create is too high. [12/7] Open
def create
convert_text_fields_to_boolean
# if an site admin makes a basket, make sure the basket is instantly approved
params[:basket][:status] =
- Read upRead up
- Exclude checks
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 has too many lines. [14/10] Open
def update_appearance
@basket = Basket.find(params[:id])
do_not_sanitize = (params[:settings][:do_not_sanitize_footer_content] == 'true')
original_html = params[:settings][:additional_footer_content]
sanitized_html = original_html
- Read upRead up
- Exclude checks
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.
Perceived complexity for prepare_and_validate_profile_for is too high. [11/7] Open
def prepare_and_validate_profile_for(form_type)
# this var is used in form helpers
@form_type = form_type
# we don't run this method is we don't have profile rules
- Read upRead up
- Exclude checks
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 prepare_and_validate_profile_for is too high. [10/6] Open
def prepare_and_validate_profile_for(form_type)
# this var is used in form helpers
@form_type = form_type
# we don't run this method is we don't have profile rules
- Read upRead up
- Exclude checks
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 has too many lines. [14/10] Open
def render_item_form
@new_item_basket = params[:new_item_basket]
@new_item_controller = params[:new_item_controller]
@relate_to_item = params[:relate_to_item]
@relate_to_type = params[:relate_to_type]
- Read upRead up
- Exclude checks
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 create is too high. [9/6] Open
def create
convert_text_fields_to_boolean
# if an site admin makes a basket, make sure the basket is instantly approved
params[:basket][:status] =
- Read upRead up
- Exclude checks
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.
Assignment Branch Condition size for add_index_topic is too high. [18.55/15] Open
def add_index_topic
@topic = Topic.find(params[:topic])
@basket = Basket.find(params[:index_for_basket])
@successful = @basket.update_index_topic(@topic)
if @successful
- Read upRead up
- Exclude checks
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
Cyclomatic complexity for allowed_field? is too high. [9/6] Open
def allowed_field?(name)
return true if profile_rules.blank? # no profile mapping
return true if params[:show_all_fields] && @site_admin
return true if profile_rules[@form_type.to_s]['rule_type'] == 'all'
return true if Basket::NESTED_FIELDS.include?(name)
- Read upRead up
- Exclude checks
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 update
has 50 lines of code (exceeds 25 allowed). Consider refactoring. Open
def update
params[:source_form] ||= 'edit'
params[:basket] ||= {}
@basket = Basket.find(params[:id])
Assignment Branch Condition size for destroy is too high. [17.23/15] Open
def destroy
@basket = Basket.find(params[:id])
# dependent destroy isn't sufficient
# to delete zoom items from the zoom_db
- Read upRead up
- Exclude checks
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
Perceived complexity for allowed_field? is too high. [9/7] Open
def allowed_field?(name)
return true if profile_rules.blank? # no profile mapping
return true if params[:show_all_fields] && @site_admin
return true if profile_rules[@form_type.to_s]['rule_type'] == 'all'
return true if Basket::NESTED_FIELDS.include?(name)
- Read upRead up
- Exclude checks
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 has too many lines. [12/10] Open
def add_index_topic
@topic = Topic.find(params[:topic])
@basket = Basket.find(params[:index_for_basket])
@successful = @basket.update_index_topic(@topic)
if @successful
- Read upRead up
- Exclude checks
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 destroy is too high. [7/6] Open
def destroy
@basket = Basket.find(params[:id])
# dependent destroy isn't sufficient
# to delete zoom items from the zoom_db
- Read upRead up
- Exclude checks
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 destroy is too high. [8/7] Open
def destroy
@basket = Basket.find(params[:id])
# dependent destroy isn't sufficient
# to delete zoom items from the zoom_db
- Read upRead up
- Exclude checks
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 current_value_of
has 40 lines of code (exceeds 25 allowed). Consider refactoring. Open
def current_value_of(name, skip_posted_values = false, form_type = nil)
form_type ||= @form_type
value = nil
Method create
has 37 lines of code (exceeds 25 allowed). Consider refactoring. Open
def create
convert_text_fields_to_boolean
# if an site admin makes a basket, make sure the basket is instantly approved
params[:basket][:status] =
Method choose_type
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
def choose_type
# give the user the option to add the item to any place the have access to
@basket_list = []
if @site_admin
@basket_list = Basket.list_as_names_and_urlified_names
Method choose_type
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
def choose_type
# give the user the option to add the item to any place the have access to
@basket_list = []
if @site_admin
@basket_list = Basket.list_as_names_and_urlified_names
- Read upRead up
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_settings
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
def set_settings
return unless params[:settings]
# create a hash with setting keys and values for usage later
basket_settings = {}
- Read upRead up
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 allowed_field?
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
def allowed_field?(name)
return true if profile_rules.blank? # no profile mapping
return true if params[:show_all_fields] && @site_admin
return true if profile_rules[@form_type.to_s]['rule_type'] == 'all'
return true if Basket::NESTED_FIELDS.include?(name)
- Read upRead up
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 too many return
statements within this method. Open
return true if profile_rules[@form_type.to_s]['allowed'] &&
Avoid too many return
statements within this method. Open
return false if profile_rules[@form_type.to_s]['rule_type'] == 'none'
Method update_appearance
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
def update_appearance
@basket = Basket.find(params[:id])
do_not_sanitize = (params[:settings][:do_not_sanitize_footer_content] == 'true')
original_html = params[:settings][:additional_footer_content]
sanitized_html = original_html
- Read upRead up
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
next unless profile_rules[type.to_s] && profile_rules[type.to_s]['values']
- Read upRead up
- Exclude checks
This cop 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.
FIXME found Open
# EOIN: FIXME: verify is not in rails3 but we do need to limit the HTTP verbs in routing. This will need to be addressed before we go live
- Exclude checks
Closing array brace must be on the line after the last array element when opening brace is on a separate line from the first array element. Open
set_settings]
- Read upRead up
- Exclude checks
This cop checks that the closing brace in an array literal is either on the same line as the last array element, or a new line.
When using the symmetrical
(default) style:
If an array's opening brace is on the same line as the first element of the array, then the closing brace should be on the same line as the last element of the array.
If an array's opening brace is on the line above the first element of the array, then the closing brace should be on the line below the last element of the array.
When using the new_line
style:
The closing brace of a multi-line array literal must be on the line after the last element of the array.
When using the same_line
style:
The closing brace of a multi-line array literal must be on the same line as the last element of the array.
Example: EnforcedStyle: symmetrical (default)
# bad
[ :a,
:b
]
# bad
[
:a,
:b ]
# good
[ :a,
:b ]
# good
[
:a,
:b
]
Example: EnforcedStyle: new_line
# bad
[
:a,
:b ]
# bad
[ :a,
:b ]
# good
[ :a,
:b
]
# good
[
:a,
:b
]
Example: EnforcedStyle: same_line
# bad
[ :a,
:b
]
# bad
[
:a,
:b
]
# good
[
:a,
:b ]
# good
[ :a,
:b ]
Use safe navigation (&.
) instead of checking if an object exists before calling the method. Open
return true if profile_rules[@form_type.to_s]['allowed'] &&
profile_rules[@form_type.to_s]['allowed'].include?(name.to_s)
- Read upRead up
- Exclude checks
This cop transforms usages of a method call safeguarded by a non nil
check for the variable whose method is being called to
safe navigation (&.
).
Configuration option: ConvertCodeThatCanStartToReturnNil
The default for this is false
. When configured to true
, this will
check for code in the format !foo.nil? && foo.bar
. As it is written,
the return of this code is limited to false
and whatever the return
of the method is. If this is converted to safe navigation,
foo&.bar
can start returning nil
as well as what the method
returns.
Example:
# bad
foo.bar if foo
foo.bar(param1, param2) if foo
foo.bar { |e| e.something } if foo
foo.bar(param) { |e| e.something } if foo
foo.bar if !foo.nil?
foo.bar unless !foo
foo.bar unless foo.nil?
foo && foo.bar
foo && foo.bar(param1, param2)
foo && foo.bar { |e| e.something }
foo && foo.bar(param) { |e| e.something }
# good
foo&.bar
foo&.bar(param1, param2)
foo&.bar { |e| e.something }
foo&.bar(param) { |e| e.something }
foo.nil? || foo.bar
!foo || foo.bar
# Methods that `nil` will `respond_to?` should not be converted to
# use safe navigation
foo.to_i if foo
end
at 83, 36 is not aligned with if
at 79, 6. Open
end
- Read upRead up
- Exclude checks
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 a guard clause instead of wrapping the code inside a conditional expression. Open
if @successful
- Read upRead up
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
Example:
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
end
at 564, 25 is not aligned with if
at 560, 6. Open
end
- Read upRead up
- Exclude checks
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)
%w
-literals should be delimited by [
and ]
. Open
@default_sorting[:direction], %w(name created_at)
- Read upRead up
- Exclude checks
This cop enforces the consistent usage of %
-literal delimiters.
Specify the 'default' key to set all preferred delimiters at once. You can continue to specify individual preferred delimiters to override the default.
Example:
# Style/PercentLiteralDelimiters:
# PreferredDelimiters:
# default: '[]'
# '%i': '()'
# good
%w[alpha beta] + %i(gamma delta)
# bad
%W(alpha #{beta})
# bad
%I(alpha beta)
Closing array brace must be on the line after the last array element when opening brace is on a separate line from the first array element. Open
zoom_class_controller(zoom_class)]
- Read upRead up
- Exclude checks
This cop checks that the closing brace in an array literal is either on the same line as the last array element, or a new line.
When using the symmetrical
(default) style:
If an array's opening brace is on the same line as the first element of the array, then the closing brace should be on the same line as the last element of the array.
If an array's opening brace is on the line above the first element of the array, then the closing brace should be on the line below the last element of the array.
When using the new_line
style:
The closing brace of a multi-line array literal must be on the line after the last element of the array.
When using the same_line
style:
The closing brace of a multi-line array literal must be on the same line as the last element of the array.
Example: EnforcedStyle: symmetrical (default)
# bad
[ :a,
:b
]
# bad
[
:a,
:b ]
# good
[ :a,
:b ]
# good
[
:a,
:b
]
Example: EnforcedStyle: new_line
# bad
[
:a,
:b ]
# bad
[ :a,
:b ]
# good
[ :a,
:b
]
# good
[
:a,
:b
]
Example: EnforcedStyle: same_line
# bad
[ :a,
:b
]
# bad
[
:a,
:b
]
# good
[
:a,
:b ]
# good
[ :a,
:b ]
end
at 519, 21 is not aligned with if
at 510, 8. Open
end
- Read upRead up
- Exclude checks
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 a guard clause instead of wrapping the code inside a conditional expression. Open
unless current_user_can_add_or_request_basket?
- Read upRead up
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
Example:
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
Use next
to skip iteration. Open
if zoom_class != 'Comment'
- Read upRead up
- Exclude checks
Use next
to skip iteration instead of a condition at the end.
Example: EnforcedStyle: skipmodifierifs (default)
# bad
[1, 2].each do |a|
if a == 1
puts a
end
end
# good
[1, 2].each do |a|
next unless a == 1
puts a
end
# good
[1, 2].each do |o|
puts o unless o == 1
end
Example: EnforcedStyle: always
# With `always` all conditions at the end of an iteration needs to be
# replaced by next - with `skip_modifier_ifs` the modifier if like
# this one are ignored: `[1, 2].each { |a| return 'yes' if a == 1 }`
# bad
[1, 2].each do |o|
puts o unless o == 1
end
# bad
[1, 2].each do |a|
if a == 1
puts a
end
end
# good
[1, 2].each do |a|
next unless a == 1
puts a
end
Use safe navigation (&.
) instead of checking if an object exists before calling the method. Open
value = params[:settings][name] if params[:settings] && params[:settings].key?(name)
- Read upRead up
- Exclude checks
This cop transforms usages of a method call safeguarded by a non nil
check for the variable whose method is being called to
safe navigation (&.
).
Configuration option: ConvertCodeThatCanStartToReturnNil
The default for this is false
. When configured to true
, this will
check for code in the format !foo.nil? && foo.bar
. As it is written,
the return of this code is limited to false
and whatever the return
of the method is. If this is converted to safe navigation,
foo&.bar
can start returning nil
as well as what the method
returns.
Example:
# bad
foo.bar if foo
foo.bar(param1, param2) if foo
foo.bar { |e| e.something } if foo
foo.bar(param) { |e| e.something } if foo
foo.bar if !foo.nil?
foo.bar unless !foo
foo.bar unless foo.nil?
foo && foo.bar
foo && foo.bar(param1, param2)
foo && foo.bar { |e| e.something }
foo && foo.bar(param) { |e| e.something }
# good
foo&.bar
foo&.bar(param1, param2)
foo&.bar { |e| e.something }
foo&.bar(param) { |e| e.something }
foo.nil? || foo.bar
!foo || foo.bar
# Methods that `nil` will `respond_to?` should not be converted to
# use safe navigation
foo.to_i if foo
Use a guard clause instead of wrapping the code inside a conditional expression. Open
if @successful
- Read upRead up
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
Example:
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
Use safe navigation (&.
) instead of checking if an object exists before calling the method. Open
value = params[:basket][name] if params[:basket] && params[:basket].key?(name)
- Read upRead up
- Exclude checks
This cop transforms usages of a method call safeguarded by a non nil
check for the variable whose method is being called to
safe navigation (&.
).
Configuration option: ConvertCodeThatCanStartToReturnNil
The default for this is false
. When configured to true
, this will
check for code in the format !foo.nil? && foo.bar
. As it is written,
the return of this code is limited to false
and whatever the return
of the method is. If this is converted to safe navigation,
foo&.bar
can start returning nil
as well as what the method
returns.
Example:
# bad
foo.bar if foo
foo.bar(param1, param2) if foo
foo.bar { |e| e.something } if foo
foo.bar(param) { |e| e.something } if foo
foo.bar if !foo.nil?
foo.bar unless !foo
foo.bar unless foo.nil?
foo && foo.bar
foo && foo.bar(param1, param2)
foo && foo.bar { |e| e.something }
foo && foo.bar(param) { |e| e.something }
# good
foo&.bar
foo&.bar(param1, param2)
foo&.bar { |e| e.something }
foo&.bar(param) { |e| e.something }
foo.nil? || foo.bar
!foo || foo.bar
# Methods that `nil` will `respond_to?` should not be converted to
# use safe navigation
foo.to_i if foo
Convert if
nested inside else
to elsif
. Open
if form_type.is_a?(Array)
- Read upRead up
- Exclude checks
If the else
branch of a conditional consists solely of an if
node,
it can be combined with the else
to become an elsif
.
This helps to keep the nesting level from getting too deep.
Example:
# bad
if condition_a
action_a
else
if condition_b
action_b
else
action_c
end
end
# good
if condition_a
action_a
elsif condition_b
action_b
else
action_c
end
end
at 595, 38 is not aligned with case
at 590, 8. Open
end
- Read upRead up
- Exclude checks
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)
Avoid multi-line ternary operators, use if
or unless
instead. Open
current_user_is?(all_baskets_hash[basket_urlified_name.to_sym][:privacy], all_baskets_hash[basket_urlified_name.to_sym][:basket]) \
? [basket_hash[:basket_name], basket_urlified_name.to_s] \
: nil
- Read upRead up
- Exclude checks
This cop checks for multi-line ternary op expressions.
Example:
# bad
a = cond ?
b : c
a = cond ? b :
c
a = cond ?
b :
c
# good
a = cond ? b : c
a =
if cond
b
else
c
end