Module has too many lines. [535/100] Open
module ExtendedContent
CLASSES_WITH_SUMMARIES = ['Topic', 'Document']
unless included_modules.include? ExtendedContent
- Read upRead up
- Exclude checks
This cop checks if the length a module exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Method convert_extended_content_to_xml
has a Cognitive Complexity of 59 (exceeds 5 allowed). Consider refactoring. Open
def convert_extended_content_to_xml(params_hash)
return '' if params_hash.blank?
builder = Nokogiri::XML::Builder.new
builder.root do |xml|
- 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
File extended_content.rb
has 540 lines of code (exceeds 250 allowed). Consider refactoring. Open
require 'rexml/document'
require 'builder'
require 'xmlsimple'
# ExtendedContent provides a way to access additional, extended content directly on a model. (ExtendedContent is included in all
Method has too many lines. [56/10] Open
def convert_extended_content_to_xml(params_hash)
return '' if params_hash.blank?
builder = Nokogiri::XML::Builder.new
builder.root do |xml|
- 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 convert_extended_content_to_xml is too high. [59.89/15] Open
def convert_extended_content_to_xml(params_hash)
return '' if params_hash.blank?
builder = Nokogiri::XML::Builder.new
builder.root do |xml|
- 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. [42/10] Open
def structured_extended_content=(hash)
hash_for_conversion =
hash.inject({}) do |result, field|
# Extract the name of the field
field_param_name = field.delete(field.first)
- 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 structured_extended_content= is too high. [44.1/15] Open
def structured_extended_content=(hash)
hash_for_conversion =
hash.inject({}) do |result, field|
# Extract the name of the field
field_param_name = field.delete(field.first)
- 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 validate_extended_content_multiple_values is too high. [42.74/15] Open
def validate_extended_content_multiple_values(extended_field_mapping, values)
all_values_blank =
values.all? do |v|
v = v['value'] if v.is_a?(Hash) && v['value']
v.to_s.blank?
- 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 validate_extended_content_single_value is too high. [39.33/15] Open
def validate_extended_content_single_value(extended_field_mapping, value)
# Handle required fields here..
no_map_enabled = (%w(map map_address).member?(extended_field_mapping.extended_field.ftype) && (!value || value['no_map'] == '1'))
no_year_provided = (extended_field_mapping.extended_field.ftype == 'year' && (!value || value['value'].blank?))
if extended_field_mapping.required &&
- 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. [32/10] Open
def replace_value_for(extended_field_element_name, value, field = nil)
# Fetch the existing data from XML
sandpit_data = structured_extended_content
# Replace the value we're changing
- 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 convert_value_from_structured_hash is too high. [35.24/15] Open
def convert_value_from_structured_hash(value_array, extended_field)
# If the extended field is a choice, make sure it's values properly indexed in XML.
if ['autocomplete', 'choice'].member?(extended_field.ftype)
# gives some flexibility when value is being swapped in from add-ons (read translations)
value_array = [value_array] if value_array.is_a?(String)
- 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 replace_value_for is too high. [35.01/15] Open
def replace_value_for(extended_field_element_name, value, field = nil)
# Fetch the existing data from XML
sandpit_data = structured_extended_content
# Replace the value we're changing
- 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. [27/10] Open
def validate_extended_content_multiple_values(extended_field_mapping, values)
all_values_blank =
values.all? do |v|
v = v['value'] if v.is_a?(Hash) && v['value']
v.to_s.blank?
- 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 array_of_values
has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring. Open
def array_of_values(hash)
# there is one instant where we just want to return the hash
# if it has a label, we want a hash of label and value
if value_label_hash?(hash) || hash.keys.include?('circa')
hash.keys.each { |key| hash.delete(key) unless %w(value label circa).include?(key) }
- 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 convert_value_from_structured_hash
has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring. Open
def convert_value_from_structured_hash(value_array, extended_field)
# If the extended field is a choice, make sure it's values properly indexed in XML.
if ['autocomplete', 'choice'].member?(extended_field.ftype)
# gives some flexibility when value is being swapped in from add-ons (read translations)
value_array = [value_array] if value_array.is_a?(String)
- 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 reader_for
has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring. Open
def reader_for(extended_field_element_name, field = nil)
values = structured_extended_content[extended_field_element_name].to_a
values = hashes_to_arrays(values).to_a
if values.size == 1
if field && field.ftype == 'year'
- 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 structured_extended_content=
has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring. Open
def structured_extended_content=(hash)
hash_for_conversion =
hash.inject({}) do |result, field|
# Extract the name of the field
field_param_name = field.delete(field.first)
- 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
Assignment Branch Condition size for validate_extended_topic_type_field_content is too high. [30.95/15] Open
def validate_extended_topic_type_field_content(extended_field_mapping, value)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if value.blank?
# when labels are passed back, values may be a hash wrapped in an array
- 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. [24/10] Open
def convert_value_from_structured_hash(value_array, extended_field)
# If the extended field is a choice, make sure it's values properly indexed in XML.
if ['autocomplete', 'choice'].member?(extended_field.ftype)
# gives some flexibility when value is being swapped in from add-ons (read translations)
value_array = [value_array] if value_array.is_a?(String)
- 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 replace_value_for
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
def replace_value_for(extended_field_element_name, value, field = nil)
# Fetch the existing data from XML
sandpit_data = structured_extended_content
# Replace the value we're changing
- 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 convert_extended_content_to_xml is too high. [20/7] Open
def convert_extended_content_to_xml(params_hash)
return '' if params_hash.blank?
builder = Nokogiri::XML::Builder.new
builder.root do |xml|
- 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 convert_extended_content_to_xml is too high. [18/6] Open
def convert_extended_content_to_xml(params_hash)
return '' if params_hash.blank?
builder = Nokogiri::XML::Builder.new
builder.root do |xml|
- 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. [21/10] Open
def xml_attributes
extended_content_hash = xml_attributes_without_position
ordered_hash = {}
position = 1
- 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. [21/10] Open
def validate_extended_topic_type_field_content(extended_field_mapping, value)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if value.blank?
# when labels are passed back, values may be a hash wrapped in an array
- 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 recursively_convert_values
has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring. Open
def recursively_convert_values(key, value = nil)
if value.is_a?(Hash) && !value.empty?
value = array_of_values(value).reject { |questionable_value| questionable_value.nil? }
value = value.first if value.size == 1
[key, value.blank? ? nil : value]
- 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 validate_extended_content_single_value(extended_field_mapping, value)
# Handle required fields here..
no_map_enabled = (%w(map map_address).member?(extended_field_mapping.extended_field.ftype) && (!value || value['no_map'] == '1'))
no_year_provided = (extended_field_mapping.extended_field.ftype == 'year' && (!value || value['value'].blank?))
if extended_field_mapping.required &&
- 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 validate_extended_content_multiple_values
has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring. Open
def validate_extended_content_multiple_values(extended_field_mapping, values)
all_values_blank =
values.all? do |v|
v = v['value'] if v.is_a?(Hash) && v['value']
v.to_s.blank?
- 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 convert_value_from_structured_hash is too high. [16/7] Open
def convert_value_from_structured_hash(value_array, extended_field)
# If the extended field is a choice, make sure it's values properly indexed in XML.
if ['autocomplete', 'choice'].member?(extended_field.ftype)
# gives some flexibility when value is being swapped in from add-ons (read translations)
value_array = [value_array] if value_array.is_a?(String)
- 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 convert_value_from_structured_hash is too high. [14/6] Open
def convert_value_from_structured_hash(value_array, extended_field)
# If the extended field is a choice, make sure it's values properly indexed in XML.
if ['autocomplete', 'choice'].member?(extended_field.ftype)
# gives some flexibility when value is being swapped in from add-ons (read translations)
value_array = [value_array] if value_array.is_a?(String)
- 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. [18/10] Open
def reader_for(extended_field_element_name, field = nil)
values = structured_extended_content[extended_field_element_name].to_a
values = hashes_to_arrays(values).to_a
if values.size == 1
if field && field.ftype == 'year'
- 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 validate_extended_content_single_value is too high. [14/6] Open
def validate_extended_content_single_value(extended_field_mapping, value)
# Handle required fields here..
no_map_enabled = (%w(map map_address).member?(extended_field_mapping.extended_field.ftype) && (!value || value['no_map'] == '1'))
no_year_provided = (extended_field_mapping.extended_field.ftype == 'year' && (!value || value['value'].blank?))
if extended_field_mapping.required &&
- 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 validate_extended_content_single_value is too high. [15/7] Open
def validate_extended_content_single_value(extended_field_mapping, value)
# Handle required fields here..
no_map_enabled = (%w(map map_address).member?(extended_field_mapping.extended_field.ftype) && (!value || value['no_map'] == '1'))
no_year_provided = (extended_field_mapping.extended_field.ftype == 'year' && (!value || value['value'].blank?))
if extended_field_mapping.required &&
- 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
Block has too many lines. [51/25] Open
builder.root do |xml|
all_field_mappings.collect do |field_to_xml|
# we should not generate extended field content for mappings that
# are private_only but are submitted for a public version
next if field_to_xml.private_only? && respond_to?(:private) && !private?
- Read upRead up
- Exclude checks
This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.
Perceived complexity for structured_extended_content= is too high. [14/7] Open
def structured_extended_content=(hash)
hash_for_conversion =
hash.inject({}) do |result, field|
# Extract the name of the field
field_param_name = field.delete(field.first)
- 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. [17/10] Open
def structured_extended_content
convert_xml_to_key_value_hash.inject({}) do |hash, field|
field_name = field.delete(field.first)
field_name_root = field_name.gsub('_multiple', '')
- 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 reader_for is too high. [22.65/15] Open
def reader_for(extended_field_element_name, field = nil)
values = structured_extended_content[extended_field_element_name].to_a
values = hashes_to_arrays(values).to_a
if values.size == 1
if field && field.ftype == 'year'
- 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 array_of_values is too high. [22.85/15] Open
def array_of_values(hash)
# there is one instant where we just want to return the hash
# if it has a label, we want a hash of label and value
if value_label_hash?(hash) || hash.keys.include?('circa')
hash.keys.each { |key| hash.delete(key) unless %w(value label circa).include?(key) }
- 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. [17/10] Open
def array_of_values(hash)
# there is one instant where we just want to return the hash
# if it has a label, we want a hash of label and value
if value_label_hash?(hash) || hash.keys.include?('circa')
hash.keys.each { |key| hash.delete(key) unless %w(value label circa).include?(key) }
- 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. [17/10] Open
def validate_extended_choice_field_content(extended_field_mapping, values)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if values.blank?
valid_choice_values = extended_field_mapping.extended_field.choices.collect { |c| c.value }
- 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 validate_extended_topic_type_field_content
has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring. Open
def validate_extended_topic_type_field_content(extended_field_mapping, value)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if value.blank?
# when labels are passed back, values may be a hash wrapped in an array
- 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
Assignment Branch Condition size for structured_extended_content is too high. [21.21/15] Open
def structured_extended_content
convert_xml_to_key_value_hash.inject({}) do |hash, field|
field_name = field.delete(field.first)
field_name_root = field_name.gsub('_multiple', '')
- 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 replace_value_for is too high. [12/6] Open
def replace_value_for(extended_field_element_name, value, field = nil)
# Fetch the existing data from XML
sandpit_data = structured_extended_content
# Replace the value we're changing
- 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 validate_extended_content_multiple_values is too high. [13/7] Open
def validate_extended_content_multiple_values(extended_field_mapping, values)
all_values_blank =
values.all? do |v|
v = v['value'] if v.is_a?(Hash) && v['value']
v.to_s.blank?
- 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 validate_extended_content_multiple_values is too high. [12/6] Open
def validate_extended_content_multiple_values(extended_field_mapping, values)
all_values_blank =
values.all? do |v|
v = v['value'] if v.is_a?(Hash) && v['value']
v.to_s.blank?
- 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 replace_value_for is too high. [13/7] Open
def replace_value_for(extended_field_element_name, value, field = nil)
# Fetch the existing data from XML
sandpit_data = structured_extended_content
# Replace the value we're changing
- 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
Block has too many lines. [49/25] Open
all_field_mappings.collect do |field_to_xml|
# we should not generate extended field content for mappings that
# are private_only but are submitted for a public version
next if field_to_xml.private_only? && respond_to?(:private) && !private?
- Read upRead up
- Exclude checks
This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.
Method structured_extended_content
has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring. Open
def structured_extended_content
convert_xml_to_key_value_hash.inject({}) do |hash, field|
field_name = field.delete(field.first)
field_name_root = field_name.gsub('_multiple', '')
- 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 array_of_values is too high. [12/7] Open
def array_of_values(hash)
# there is one instant where we just want to return the hash
# if it has a label, we want a hash of label and value
if value_label_hash?(hash) || hash.keys.include?('circa')
hash.keys.each { |key| hash.delete(key) unless %w(value label circa).include?(key) }
- 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 structured_extended_content= is too high. [11/6] Open
def structured_extended_content=(hash)
hash_for_conversion =
hash.inject({}) do |result, field|
# Extract the name of the field
field_param_name = field.delete(field.first)
- 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. [15/10] Open
def method_missing(symbol, *args, &block)
# Construct some information we need
method_name = symbol.to_s
method_root = method_name.gsub(/[^\w]/, '')
- 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 convert_extended_content_to_xml
has 56 lines of code (exceeds 25 allowed). Consider refactoring. Open
def convert_extended_content_to_xml(params_hash)
return '' if params_hash.blank?
builder = Nokogiri::XML::Builder.new
builder.root do |xml|
Perceived complexity for validate_extended_topic_type_field_content is too high. [11/7] Open
def validate_extended_topic_type_field_content(extended_field_mapping, value)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if value.blank?
# when labels are passed back, values may be a hash wrapped in an array
- 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 validate_extended_topic_type_field_content is too high. [10/6] Open
def validate_extended_topic_type_field_content(extended_field_mapping, value)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if value.blank?
# when labels are passed back, values may be a hash wrapped in an array
- 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 validate_extended_choice_field_content is too high. [19.87/15] Open
def validate_extended_choice_field_content(extended_field_mapping, values)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if values.blank?
valid_choice_values = extended_field_mapping.extended_field.choices.collect { |c| c.value }
- 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 array_of_values is too high. [10/6] Open
def array_of_values(hash)
# there is one instant where we just want to return the hash
# if it has a label, we want a hash of label and value
if value_label_hash?(hash) || hash.keys.include?('circa')
hash.keys.each { |key| hash.delete(key) unless %w(value label circa).include?(key) }
- 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 validate is too high. [19.67/15] Open
def validate
all_field_mappings.each do |mapping|
field = mapping.extended_field
if field.multiple?
- 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 method_missing
has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. Open
def method_missing(symbol, *args, &block)
# Construct some information we need
method_name = symbol.to_s
method_root = method_name.gsub(/[^\w]/, '')
- 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 reader_for is too high. [10/7] Open
def reader_for(extended_field_element_name, field = nil)
values = structured_extended_content[extended_field_element_name].to_a
values = hashes_to_arrays(values).to_a
if values.size == 1
if field && field.ftype == 'year'
- 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. [13/10] Open
def validate_extended_year_field_content(extended_field_mapping, values)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if values.blank?
# the values passed in should form an array
return I18n.t(
- 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 xml_attributes is too high. [18.63/15] Open
def xml_attributes
extended_content_hash = xml_attributes_without_position
ordered_hash = {}
position = 1
- 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 reader_for is too high. [9/6] Open
def reader_for(extended_field_element_name, field = nil)
values = structured_extended_content[extended_field_element_name].to_a
values = hashes_to_arrays(values).to_a
if values.size == 1
if field && field.ftype == 'year'
- 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. [13/10] Open
def hashes_to_arrays(values)
values.collect do |value|
if value.is_a?(Hash) && value.keys.include?('value') && value.keys.include?('label')
if value['label'] == value['value']
value['label']
- 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 method_missing is too high. [17.55/15] Open
def method_missing(symbol, *args, &block)
# Construct some information we need
method_name = symbol.to_s
method_root = method_name.gsub(/[^\w]/, '')
- 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. [12/10] Open
def validate
all_field_mappings.each do |mapping|
field = mapping.extended_field
if field.multiple?
- 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 recursively_convert_values is too high. [8/7] Open
def recursively_convert_values(key, value = nil)
if value.is_a?(Hash) && !value.empty?
value = array_of_values(value).reject { |questionable_value| questionable_value.nil? }
value = value.first if value.size == 1
[key, value.blank? ? nil : value]
- 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 method_missing is too high. [7/6] Open
def method_missing(symbol, *args, &block)
# Construct some information we need
method_name = symbol.to_s
method_root = method_name.gsub(/[^\w]/, '')
- 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 hashes_to_arrays is too high. [8/7] Open
def hashes_to_arrays(values)
values.collect do |value|
if value.is_a?(Hash) && value.keys.include?('value') && value.keys.include?('label')
if value['label'] == value['value']
value['label']
- 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 recursively_convert_values is too high. [7/6] Open
def recursively_convert_values(key, value = nil)
if value.is_a?(Hash) && !value.empty?
value = array_of_values(value).reject { |questionable_value| questionable_value.nil? }
value = value.first if value.size == 1
[key, value.blank? ? nil : value]
- 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 recursively_convert_values is too high. [16.28/15] Open
def recursively_convert_values(key, value = nil)
if value.is_a?(Hash) && !value.empty?
value = array_of_values(value).reject { |questionable_value| questionable_value.nil? }
value = value.first if value.size == 1
[key, value.blank? ? nil : value]
- 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. [11/10] Open
def remove_xml_fix(in_hash)
out_hash = {}
in_hash.each do |k, v|
new_k = tweaked_key(k)
- 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 validate_extended_content_single_value
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
def validate_extended_content_single_value(extended_field_mapping, value)
# Handle required fields here..
no_map_enabled = (%w(map map_address).member?(extended_field_mapping.extended_field.ftype) && (!value || value['no_map'] == '1'))
no_year_provided = (extended_field_mapping.extended_field.ftype == 'year' && (!value || value['value'].blank?))
if extended_field_mapping.required &&
- 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 structured_extended_content=
has 42 lines of code (exceeds 25 allowed). Consider refactoring. Open
def structured_extended_content=(hash)
hash_for_conversion =
hash.inject({}) do |result, field|
# Extract the name of the field
field_param_name = field.delete(field.first)
Method hashes_to_arrays
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
def hashes_to_arrays(values)
values.collect do |value|
if value.is_a?(Hash) && value.keys.include?('value') && value.keys.include?('label')
if value['label'] == value['value']
value['label']
- 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
Block has too many lines. [38/25] Open
hash.inject({}) do |result, field|
# Extract the name of the field
field_param_name = field.delete(field.first)
# Grab the extended field for this field name
- Read upRead up
- Exclude checks
This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.
Method replace_value_for
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
def replace_value_for(extended_field_element_name, value, field = nil)
# Fetch the existing data from XML
sandpit_data = structured_extended_content
# Replace the value we're changing
Method validate_extended_choice_field_content
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
def validate_extended_choice_field_content(extended_field_mapping, values)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if values.blank?
valid_choice_values = extended_field_mapping.extended_field.choices.collect { |c| c.value }
- 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 validate_extended_year_field_content
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
def validate_extended_year_field_content(extended_field_mapping, values)
# Allow nil values. If this is required, the nil value will be caught earlier.
return nil if values.blank?
# the values passed in should form an array
return I18n.t(
- 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 xml_attributes
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
def xml_attributes
extended_content_hash = xml_attributes_without_position
ordered_hash = {}
position = 1
- 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 validate_extended_content_multiple_values
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
def validate_extended_content_multiple_values(extended_field_mapping, values)
all_values_blank =
values.all? do |v|
v = v['value'] if v.is_a?(Hash) && v['value']
v.to_s.blank?
Block has too many lines. [32/25] Open
xml.safe_send("#{field_name}_multiple") do
hash_of_values = params_hash[field_name]
# Do not store empty values
hash_of_values = hash_of_values ? hash_of_values.reject { |k, v| v.blank? } : nil
- Read upRead up
- Exclude checks
This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.
Avoid deeply nested control flow statements. Open
value_array = value_array.first if value_array.is_a?(Array) && !extended_field.multiple?
Avoid deeply nested control flow statements. Open
if value.size == 1
value = value.first
end
Avoid deeply nested control flow statements. Open
elsif value_array.is_a?(Array) && value_label_hash?(value_array.first)
value_array
else
value_array.to_s
Avoid deeply nested control flow statements. Open
value_array = value_array.first if value_array.is_a?(Array)
Method append_new_multiple_value_for
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
def append_new_multiple_value_for(extended_field_element_name, additional_value, field = nil)
raise "Cannot add a new multiple value on #{extended_field_element_name} as it is not a multiple value field." \
unless field.nil? || field.multiple?
# to_a allows for current_values to be an empty array (and thus work with + operator)
- 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
unless value.to_s.blank? || (value.is_a?(Hash) && value.values.to_s.blank?)
xml.safe_send(key) do
extended_content_field_xml_tag(
xml: xml,
field: field_name,
- 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.
Avoid more than 3 levels of block nesting. Open
rescue
next
- 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.
Avoid more than 3 levels of block nesting. Open
values = field.first.is_a?(Array) || value_label_hash?(field.first) ? field : [field]
- 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.
Avoid more than 3 levels of block nesting. Open
values = values.first if values.is_a?(Array) && !field.multiple?
- 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.
Avoid more than 3 levels of block nesting. Open
field.multiple? ? append_new_multiple_value_for(method_root, args, field) : append_value_for(method_root, args, field)
- 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.
Avoid more than 3 levels of block nesting. Open
next if value.is_a?(Hash) && value['circa'] && value['value'].blank?
- 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.
Avoid more than 3 levels of block nesting. Open
values = values.first if values.is_a?(Array)
- 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.
Avoid more than 3 levels of block nesting. Open
if value.is_a?(Array)
if value.size == 1
value = value.first
end
end
- 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.
Use each_key
instead of keys.each
. Open
hash.keys.each { |key| hash.delete(key) unless %w(value label circa).include?(key) }
- Read upRead up
- Exclude checks
This cop checks for uses of each_key
and each_value
Hash methods.
Note: If you have an array of two-element arrays, you can put parentheses around the block arguments to indicate that you're not working with a hash, and suppress RuboCop offenses.
Example:
# bad
hash.keys.each { |k| p k }
hash.values.each { |v| p v }
hash.each { |k, _v| p k }
hash.each { |_k, v| p v }
# good
hash.each_key { |k| p k }
hash.each_value { |v| p v }
TODO found Open
# TODO: this has been copied and modified from extended_content_helpers, DRY up
- Exclude checks
TODO found Open
# # TODO: evaluate whether this works in PostgreSQL
- Exclude checks
Use ==
if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition. Open
if message = send(
- Read upRead up
- Exclude checks
This cop checks for assignments in the conditions of if/while/until.
Example:
# bad
if some_var = true
do_something
end
Example:
# good
if some_var == true
do_something
end
Unused block argument - v
. If it's necessary, use _
or _v
as an argument name to indicate that it won't be used. Open
value_pairs = extended_content_pairs.select { |k, v| k == field.label_for_params + '_multiple' }
- Read upRead up
- Exclude checks
This cop checks for unused block arguments.
Example:
# bad
do_something do |used, unused|
puts used
end
do_something do |bar|
puts :foo
end
define_method(:foo) do |bar|
puts :baz
end
Example:
#good
do_something do |used, _unused|
puts used
end
do_something do
puts :foo
end
define_method(:foo) do |_bar|
puts :baz
end
Unreachable code detected. Open
if force_new
@builder_xml = Nokogiri::XML::Builder.new
else
@builder_xml ||= Nokogiri::XML::Builder.new
end
- Read upRead up
- Exclude checks
This cop checks for unreachable code. The check are based on the presence of flow of control statement in non-final position in begin(implicit) blocks.
Example:
# bad
def some_method
return
do_something
end
# bad
def some_method
if cond
return
else
return
end
do_something
end
Example:
# good
def some_method
do_something
end
Shadowing outer local variable - field
. Open
if (field = all_fields.find { |field| method_root == field.label_for_params }) && !field.blank?
- Read upRead up
- Exclude checks
This cop looks for use of the same name as outer local variables
for block arguments or block local variables.
This is a mimic of the warning
"shadowing outer local variable - foo" from ruby -cw
.
Example:
# bad
def some_method
foo = 1
2.times do |foo| # shadowing outer `foo`
do_something(foo)
end
end
Example:
# good
def some_method
foo = 1
2.times do |bar|
do_something(bar)
end
end
Unused block argument - k
. If it's necessary, use _
or _k
as an argument name to indicate that it won't be used. Open
hash_of_values = hash_of_values ? hash_of_values.reject { |k, v| v.blank? } : nil
- Read upRead up
- Exclude checks
This cop checks for unused block arguments.
Example:
# bad
do_something do |used, unused|
puts used
end
do_something do |bar|
puts :foo
end
define_method(:foo) do |bar|
puts :baz
end
Example:
#good
do_something do |used, _unused|
puts used
end
do_something do
puts :foo
end
define_method(:foo) do |_bar|
puts :baz
end
Unused block argument - k
. If it's necessary, use _
or _k
as an argument name to indicate that it won't be used. Open
values = value_pairs.map { |k, v| v }.flatten
- Read upRead up
- Exclude checks
This cop checks for unused block arguments.
Example:
# bad
do_something do |used, unused|
puts used
end
do_something do |bar|
puts :foo
end
define_method(:foo) do |bar|
puts :baz
end
Example:
#good
do_something do |used, _unused|
puts used
end
do_something do
puts :foo
end
define_method(:foo) do |_bar|
puts :baz
end
Use a guard clause instead of wrapping the code inside a conditional expression. Open
unless values['value'] =~ /^[0-9]{4}$/
- 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
Favor a normal unless-statement over a modifier clause in a multiline statement. Open
return I18n.t(
'extended_content_lib.validate_extended_year_field_content.not_a_hash',
label: extended_field_mapping.extended_field_label,
class: values.class.name, value: values.inspect
) unless values.is_a?(Hash)
- Read upRead up
- Exclude checks
Checks for uses of if/unless modifiers with multiple-lines bodies.
Example:
# bad
{
result: 'this should not happen'
} unless cond
# good
{ result: 'ok' } if cond
%w
-literals should be delimited by [
and ]
. Open
if !matching_choice && %w(autocomplete choice).include?(field.ftype) && field.user_choice_addition?
- 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)
%w
-literals should be delimited by [
and ]
. Open
hash.keys.each { |key| hash.delete(key) unless %w(value label circa).include?(key) }
- 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)
Unused method argument - value
. If it's necessary, use _
or _value
as an argument name to indicate that it won't be used. You can also write as validate_extended_text_field_content(*)
if you want the method to accept any arguments but don't care about them. Open
def validate_extended_text_field_content(extended_field_mapping, value)
- Read upRead up
- Exclude checks
This cop checks for unused method arguments.
Example:
# bad
def some_method(used, unused, _unused_but_allowed)
puts used
end
Example:
# good
def some_method(used, _unused, _unused_but_allowed)
puts used
end
Favor modifier if
usage when having a single-line body. Another good alternative is the usage of control flow &&
/||
. Open
if value.size == 1
- Read upRead up
- Exclude checks
Checks for if and unless statements that would fit on one line
if written as a modifier if/unless. The maximum line length is
configured in the Metrics/LineLength
cop.
Example:
# bad
if condition
do_stuff(bar)
end
unless qux.empty?
Foo.do_something
end
# good
do_stuff(bar) if condition
Foo.do_something unless qux.empty?
Avoid the use of Perl-style backrefs. Open
$1 # special case: "position_1" -> "1"
- Read upRead up
- Exclude checks
This cop looks for uses of Perl-style regexp match backreferences like $1, $2, etc.
Example:
# bad
puts $1
# good
puts Regexp.last_match(1)
Avoid rescuing without specifying an error class. Open
rescue
- Read upRead up
- Exclude checks
This cop checks for rescuing StandardError
. There are two supported
styles implicit
and explicit
. This cop will not register an offense
if any error other than StandardError
is specified.
Example: EnforcedStyle: implicit
# `implicit` will enforce using `rescue` instead of
# `rescue StandardError`.
# bad
begin
foo
rescue StandardError
bar
end
# good
begin
foo
rescue
bar
end
# good
begin
foo
rescue OtherError
bar
end
# good
begin
foo
rescue StandardError, SecurityError
bar
end
Example: EnforcedStyle: explicit (default)
# `explicit` will enforce using `rescue StandardError`
# instead of `rescue`.
# bad
begin
foo
rescue
bar
end
# good
begin
foo
rescue StandardError
bar
end
# good
begin
foo
rescue OtherError
bar
end
# good
begin
foo
rescue StandardError, SecurityError
bar
end
Unused method argument - value
. If it's necessary, use _
or _value
as an argument name to indicate that it won't be used. You can also write as validate_extended_radio_field_content(*)
if you want the method to accept any arguments but don't care about them. Open
def validate_extended_radio_field_content(extended_field_mapping, value)
- Read upRead up
- Exclude checks
This cop checks for unused method arguments.
Example:
# bad
def some_method(used, unused, _unused_but_allowed)
puts used
end
Example:
# good
def some_method(used, _unused, _unused_but_allowed)
puts used
end
Use the return of the conditional for variable assignment and comparison. Open
if field.size > 1
# We're dealing with a multiple field value.
result[field_param_name] =
field.inject({}) do |multiple, value|
multiple[(field.index(value) + 1).to_s] = convert_value_from_structured_hash(value, extended_field)
- Exclude checks
Use !empty?
instead of size > 0
. Open
if (extended_field.multiple && field.size > 0) || field.size > 1
- Read upRead up
- Exclude checks
This cop checks for numeric comparisons that can be replaced by a predicate method, such as receiver.length == 0, receiver.length > 0, receiver.length != 0, receiver.length < 1 and receiver.size == 0 that can be replaced by receiver.empty? and !receiver.empty.
Example:
# bad
[1, 2, 3].length == 0
0 == "foobar".length
array.length < 1
{a: 1, b: 2}.length != 0
string.length > 0
hash.size > 0
# good
[1, 2, 3].empty?
"foobar".empty?
array.empty?
!{a: 1, b: 2}.empty?
!string.empty?
!hash.empty?
Unused method argument - extended_field_mapping
. If it's necessary, use _
or _extended_field_mapping
as an argument name to indicate that it won't be used. You can also write as validate_extended_text_field_content(*)
if you want the method to accept any arguments but don't care about them. Open
def validate_extended_text_field_content(extended_field_mapping, value)
- Read upRead up
- Exclude checks
This cop checks for unused method arguments.
Example:
# bad
def some_method(used, unused, _unused_but_allowed)
puts used
end
Example:
# good
def some_method(used, _unused, _unused_but_allowed)
puts used
end
Use field.size.positive?
instead of field.size > 0
. Open
if (extended_field.multiple && field.size > 0) || field.size > 1
- Read upRead up
- Exclude checks
This cop checks for usage of comparison operators (==
,
>
, <
) to test numbers as zero, positive, or negative.
These can be replaced by their respective predicate methods.
The cop can also be configured to do the reverse.
The cop disregards #nonzero?
as it its value is truthy or falsey,
but not true
and false
, and thus not always interchangeable with
!= 0
.
The cop ignores comparisons to global variables, since they are often
populated with objects which can be compared with integers, but are
not themselves Interger
polymorphic.
Example: EnforcedStyle: predicate (default)
# bad
foo == 0
0 > foo
bar.baz > 0
# good
foo.zero?
foo.negative?
bar.baz.positive?
Example: EnforcedStyle: comparison
# bad
foo.zero?
foo.negative?
bar.baz.positive?
# good
foo == 0
0 > foo
bar.baz > 0
Convert if
nested inside else
to elsif
. Open
if message = send(
- 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
Use !empty?
instead of size > 0
. Open
if form_fields.size > 0
- Read upRead up
- Exclude checks
This cop checks for numeric comparisons that can be replaced by a predicate method, such as receiver.length == 0, receiver.length > 0, receiver.length != 0, receiver.length < 1 and receiver.size == 0 that can be replaced by receiver.empty? and !receiver.empty.
Example:
# bad
[1, 2, 3].length == 0
0 == "foobar".length
array.length < 1
{a: 1, b: 2}.length != 0
string.length > 0
hash.size > 0
# good
[1, 2, 3].empty?
"foobar".empty?
array.empty?
!{a: 1, b: 2}.empty?
!string.empty?
!hash.empty?
Use the return of the conditional for variable assignment and comparison. Open
if (extended_field.multiple && field.size > 0) || field.size > 1
# We're dealing with a multiple field value.
result[field_param_name] =
field.inject({}) do |multiple, value|
multiple[(field.index(value) + 1).to_s] = convert_value_from_structured_hash(value, extended_field)
- Exclude checks
Use each_with_object
instead of inject
. Open
field.inject({}) do |multiple, value|
- Read upRead up
- Exclude checks
This cop looks for inject / reduce calls where the passed in object is returned at the end and so could be replaced by eachwithobject without the need to return the object at the end.
However, we can't replace with eachwithobject if the accumulator parameter is assigned to within the block.
Example:
# bad
[1, 2].inject({}) { |a, e| a[e] = e; a }
# good
[1, 2].each_with_object({}) { |e, a| a[e] = e }
Use each_with_object
instead of inject
. Open
value_array.inject({}) do |hash, value|
- Read upRead up
- Exclude checks
This cop looks for inject / reduce calls where the passed in object is returned at the end and so could be replaced by eachwithobject without the need to return the object at the end.
However, we can't replace with eachwithobject if the accumulator parameter is assigned to within the block.
Example:
# bad
[1, 2].inject({}) { |a, e| a[e] = e; a }
# good
[1, 2].each_with_object({}) { |e, a| a[e] = e }
Favor unless
over if
for negative conditions. Open
if !values_array.all? { |v| valid_choice_values.member?(v) }
I18n.t(
'extended_content_lib.validate_extended_choice_field_content.must_be_valid',
label: extended_field_mapping.extended_field_label
)
- Read upRead up
- Exclude checks
Checks for uses of if with a negated condition. Only ifs without else are considered. There are three different styles:
- both
- prefix
- postfix
Example: EnforcedStyle: both (default)
# enforces `unless` for `prefix` and `postfix` conditionals
# bad
if !foo
bar
end
# good
unless foo
bar
end
# bad
bar if !foo
# good
bar unless foo
Example: EnforcedStyle: prefix
# enforces `unless` for just `prefix` conditionals
# bad
if !foo
bar
end
# good
unless foo
bar
end
# good
bar if !foo
Example: EnforcedStyle: postfix
# enforces `unless` for just `postfix` conditionals
# bad
bar if !foo
# good
bar unless foo
# good
if !foo
bar
end
Avoid rescuing without specifying an error class. Open
rescue
- Read upRead up
- Exclude checks
This cop checks for rescuing StandardError
. There are two supported
styles implicit
and explicit
. This cop will not register an offense
if any error other than StandardError
is specified.
Example: EnforcedStyle: implicit
# `implicit` will enforce using `rescue` instead of
# `rescue StandardError`.
# bad
begin
foo
rescue StandardError
bar
end
# good
begin
foo
rescue
bar
end
# good
begin
foo
rescue OtherError
bar
end
# good
begin
foo
rescue StandardError, SecurityError
bar
end
Example: EnforcedStyle: explicit (default)
# `explicit` will enforce using `rescue StandardError`
# instead of `rescue`.
# bad
begin
foo
rescue
bar
end
# good
begin
foo
rescue StandardError
bar
end
# good
begin
foo
rescue OtherError
bar
end
# good
begin
foo
rescue StandardError, SecurityError
bar
end
Use the return of the conditional for variable assignment and comparison. Open
if new_v.kind_of?(Hash)
out_hash[new_k] = remove_xml_fix(new_v)
else
out_hash[new_k] = new_v
end
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression. Open
if !values_array.all? { |v| valid_choice_values.member?(v) }
- 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
Freeze mutable objects assigned to constants. Open
CLASSES_WITH_SUMMARIES = ['Topic', 'Document']
- Read upRead up
- Exclude checks
This cop checks whether some constant value isn't a mutable literal (e.g. array or hash).
Example:
# bad
CONST = [1, 2, 3]
# good
CONST = [1, 2, 3].freeze
Use form_fields.size.positive?
instead of form_fields.size > 0
. Open
if form_fields.size > 0
- Read upRead up
- Exclude checks
This cop checks for usage of comparison operators (==
,
>
, <
) to test numbers as zero, positive, or negative.
These can be replaced by their respective predicate methods.
The cop can also be configured to do the reverse.
The cop disregards #nonzero?
as it its value is truthy or falsey,
but not true
and false
, and thus not always interchangeable with
!= 0
.
The cop ignores comparisons to global variables, since they are often
populated with objects which can be compared with integers, but are
not themselves Interger
polymorphic.
Example: EnforcedStyle: predicate (default)
# bad
foo == 0
0 > foo
bar.baz > 0
# good
foo.zero?
foo.negative?
bar.baz.positive?
Example: EnforcedStyle: comparison
# bad
foo.zero?
foo.negative?
bar.baz.positive?
# good
foo == 0
0 > foo
bar.baz > 0
Pass &:extended_field
as an argument to map
instead of a block. Open
@all_fields ||= all_field_mappings.map { |mapping| mapping.extended_field }.flatten
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
Unused method argument - extended_field_mapping
. If it's necessary, use _
or _extended_field_mapping
as an argument name to indicate that it won't be used. You can also write as validate_extended_textarea_field_content(*)
if you want the method to accept any arguments but don't care about them. Open
def validate_extended_textarea_field_content(extended_field_mapping, value)
- Read upRead up
- Exclude checks
This cop checks for unused method arguments.
Example:
# bad
def some_method(used, unused, _unused_but_allowed)
puts used
end
Example:
# good
def some_method(used, _unused, _unused_but_allowed)
puts used
end
Use each_with_object
instead of inject
. Open
field.inject({}) do |multiple, value|
- Read upRead up
- Exclude checks
This cop looks for inject / reduce calls where the passed in object is returned at the end and so could be replaced by eachwithobject without the need to return the object at the end.
However, we can't replace with eachwithobject if the accumulator parameter is assigned to within the block.
Example:
# bad
[1, 2].inject({}) { |a, e| a[e] = e; a }
# good
[1, 2].each_with_object({}) { |e, a| a[e] = e }
Use a guard clause instead of wrapping the code inside a conditional expression. Open
unless value =~ /^((Y|y)es|(N|n)o)$/
- 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
Favor a normal unless-statement over a modifier clause in a multiline statement. Open
errors.add_to_base(I18n.t(
'extended_content_lib.validate_extended_content_single_value.cannot_be_blank',
label: extended_field_mapping.extended_field.label
)) unless \
extended_field_mapping.extended_field.ftype != 'year' && \
- Read upRead up
- Exclude checks
Checks for uses of if/unless modifiers with multiple-lines bodies.
Example:
# bad
{
result: 'this should not happen'
} unless cond
# good
{ result: 'ok' } if cond
Pass &:nil?
as an argument to reject
instead of a block. Open
value = array_of_values(value).reject { |questionable_value| questionable_value.nil? }
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
Pass &:id
as an argument to collect
instead of a block. Open
valid_topic_type_ids = parent_topic_type.full_set.collect { |topic_type| topic_type.id }
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
Use each_with_object
instead of inject
. Open
convert_xml_to_key_value_hash.inject({}) do |hash, field|
- Read upRead up
- Exclude checks
This cop looks for inject / reduce calls where the passed in object is returned at the end and so could be replaced by eachwithobject without the need to return the object at the end.
However, we can't replace with eachwithobject if the accumulator parameter is assigned to within the block.
Example:
# bad
[1, 2].inject({}) { |a, e| a[e] = e; a }
# good
[1, 2].each_with_object({}) { |e, a| a[e] = e }
Use a guard clause instead of wrapping the code inside a conditional expression. Open
unless additional_value.blank?
- 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 a guard clause instead of wrapping the code inside a conditional expression. Open
unless value =~ /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/
- 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
Unused block argument - k
. If it's necessary, use _
or _k
as an argument name to indicate that it won't be used. Open
values = value_pairs.map { |k, v| v }
- Read upRead up
- Exclude checks
This cop checks for unused block arguments.
Example:
# bad
do_something do |used, unused|
puts used
end
do_something do |bar|
puts :foo
end
define_method(:foo) do |bar|
puts :baz
end
Example:
#good
do_something do |used, _unused|
puts used
end
do_something do
puts :foo
end
define_method(:foo) do |_bar|
puts :baz
end
When using method_missing
, define respond_to_missing?
. Open
def method_missing(symbol, *args, &block)
# Construct some information we need
method_name = symbol.to_s
method_root = method_name.gsub(/[^\w]/, '')
- Read upRead up
- Exclude checks
This cop checks for the presence of method_missing
without also
defining respond_to_missing?
and falling back on super
.
Example:
#bad
def method_missing(name, *args)
# ...
end
#good
def respond_to_missing?(name, include_private)
# ...
end
def method_missing(name, *args)
# ...
super
end
Use next
to skip iteration. Open
if !matching_choice && %w(autocomplete choice).include?(field.ftype) && field.user_choice_addition?
- 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 %r
around regular expression. Open
xml_ish.gsub(/<(\/?)(\d+)>/, '<\1position_\2>')
- Read upRead up
- Exclude checks
This cop enforces using // or %r around regular expressions.
Example: EnforcedStyle: slashes (default)
# bad
snake_case = %r{^[\dA-Z_]+$}
# bad
regex = %r{
foo
(bar)
(baz)
}x
# good
snake_case = /^[\dA-Z_]+$/
# good
regex = /
foo
(bar)
(baz)
/x
Example: EnforcedStyle: percent_r
# bad
snake_case = /^[\dA-Z_]+$/
# bad
regex = /
foo
(bar)
(baz)
/x
# good
snake_case = %r{^[\dA-Z_]+$}
# good
regex = %r{
foo
(bar)
(baz)
}x
Example: EnforcedStyle: mixed
# bad
snake_case = %r{^[\dA-Z_]+$}
# bad
regex = /
foo
(bar)
(baz)
/x
# good
snake_case = /^[\dA-Z_]+$/
# good
regex = %r{
foo
(bar)
(baz)
}x
Example: AllowInnerSlashes: false (default)
# If `false`, the cop will always recommend using `%r` if one or more
# slashes are found in the regexp string.
# bad
x =~ /home\//
# good
x =~ %r{home/}
Example: AllowInnerSlashes: true
# good
x =~ /home\//
Pass &:blank?
as an argument to reject
instead of a block. Open
values.to_a.reject { |v| v.blank? }.collect do |v|
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
Unused block argument - v
. If it's necessary, use _
or _v
as an argument name to indicate that it won't be used. Open
value_pairs = extended_content_pairs.select { |k, v| k == field.label_for_params }
- Read upRead up
- Exclude checks
This cop checks for unused block arguments.
Example:
# bad
do_something do |used, unused|
puts used
end
do_something do |bar|
puts :foo
end
define_method(:foo) do |bar|
puts :baz
end
Example:
#good
do_something do |used, _unused|
puts used
end
do_something do
puts :foo
end
define_method(:foo) do |_bar|
puts :baz
end
Unused method argument - value
. If it's necessary, use _
or _value
as an argument name to indicate that it won't be used. You can also write as validate_extended_textarea_field_content(*)
if you want the method to accept any arguments but don't care about them. Open
def validate_extended_textarea_field_content(extended_field_mapping, value)
- Read upRead up
- Exclude checks
This cop checks for unused method arguments.
Example:
# bad
def some_method(used, unused, _unused_but_allowed)
puts used
end
Example:
# good
def some_method(used, _unused, _unused_but_allowed)
puts used
end
Use next
to skip iteration. Open
unless value.to_s.blank? || (value.is_a?(Hash) && value.values.to_s.blank?)
- 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
Prefer Object#is_a?
over Object#kind_of?
. Open
if new_v.kind_of?(Hash)
- Read upRead up
- Exclude checks
This cop enforces consistent use of Object#is_a?
or Object#kind_of?
.
Example: EnforcedStyle: is_a? (default)
# bad
var.kind_of?(Date)
var.kind_of?(Integer)
# good
var.is_a?(Date)
var.is_a?(Integer)
Example: EnforcedStyle: kind_of?
# bad
var.is_a?(Time)
var.is_a?(String)
# good
var.kind_of?(Time)
var.kind_of?(String)
Use each_with_object
instead of inject
. Open
hash.inject({}) do |result, field|
- Read upRead up
- Exclude checks
This cop looks for inject / reduce calls where the passed in object is returned at the end and so could be replaced by eachwithobject without the need to return the object at the end.
However, we can't replace with eachwithobject if the accumulator parameter is assigned to within the block.
Example:
# bad
[1, 2].inject({}) { |a, e| a[e] = e; a }
# good
[1, 2].each_with_object({}) { |e, a| a[e] = e }
Use each_with_object
instead of inject
. Open
field.inject({}) do |multiple, value|
- Read upRead up
- Exclude checks
This cop looks for inject / reduce calls where the passed in object is returned at the end and so could be replaced by eachwithobject without the need to return the object at the end.
However, we can't replace with eachwithobject if the accumulator parameter is assigned to within the block.
Example:
# bad
[1, 2].inject({}) { |a, e| a[e] = e; a }
# good
[1, 2].each_with_object({}) { |e, a| a[e] = e }
Use a guard clause instead of wrapping the code inside a conditional expression. Open
unless valid_topic_type_ids.include?(topic.topic_type_id)
- 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
Convert if
nested inside else
to elsif
. Open
if (extended_field.multiple && field.size > 0) || field.size > 1
- 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
Unused method argument - extended_field_mapping
. If it's necessary, use _
or _extended_field_mapping
as an argument name to indicate that it won't be used. You can also write as validate_extended_radio_field_content(*)
if you want the method to accept any arguments but don't care about them. Open
def validate_extended_radio_field_content(extended_field_mapping, value)
- Read upRead up
- Exclude checks
This cop checks for unused method arguments.
Example:
# bad
def some_method(used, unused, _unused_but_allowed)
puts used
end
Example:
# good
def some_method(used, _unused, _unused_but_allowed)
puts used
end
Favor a normal unless-statement over a modifier clause in a multiline statement. Open
errors.add_to_base(I18n.t(
'extended_content_lib.validate_extended_content_multiple_values.need_at_least_one',
label: extended_field_mapping.extended_field.label
)) unless \
xml_attributes_without_position[extended_field_mapping.extended_field.label_for_params + '_multiple'].nil? && \
- Read upRead up
- Exclude checks
Checks for uses of if/unless modifiers with multiple-lines bodies.
Example:
# bad
{
result: 'this should not happen'
} unless cond
# good
{ result: 'ok' } if cond
Favor a normal unless-statement over a modifier clause in a multiline statement. Open
return I18n.t(
'extended_content_lib.validate_extended_topic_type_field_content.no_such_topic',
label: extended_field_mapping.extended_field_label
) unless topic
- Read upRead up
- Exclude checks
Checks for uses of if/unless modifiers with multiple-lines bodies.
Example:
# bad
{
result: 'this should not happen'
} unless cond
# good
{ result: 'ok' } if cond
%w
-literals should be delimited by [
and ]
. Open
no_map_enabled = (%w(map map_address).member?(extended_field_mapping.extended_field.ftype) && (!value || value['no_map'] == '1'))
- 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)
Pass &:value
as an argument to collect
instead of a block. Open
valid_choice_values = extended_field_mapping.extended_field.choices.collect { |c| c.value }
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)