Showing 209 of 211 total issues
Class DocumentQuery
has 21 methods (exceeds 20 allowed). Consider refactoring. Open
class DocumentQuery
include Elasticsearch::DSL
HIGHLIGHT_OPTIONS = {
pre_tags: ["\ue000"],
Method has too many lines. [14/10] Open
def language_templates(json)
LANGUAGE_ANALYZER_LOCALES.each do |locale|
json.child! do
json.set!(locale) do
json.match("*_#{locale}")
- Read upRead up
- Exclude checks
Checks if the length of a method exceeds some maximum value. Comment lines can optionally be allowed. The maximum allowed length is configurable.
You can set constructs you want to fold with CountAsOne
.
Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
will be counted as one line regardless of its actual size.
NOTE: The ExcludedMethods
and IgnoredMethods
configuration is
deprecated and only kept for backwards compatibility.
Please use AllowedMethods
and AllowedPatterns
instead.
By default, there are no methods to allowed.
Example: CountAsOne: ['array', 'heredoc', 'method_call']
def m
array = [ # +1
1,
2
]
hash = { # +3
key: 'value'
}
<<~HEREDOC # +1
Heredoc
content.
HEREDOC
foo( # +1
1,
2
)
end # 6 points
Method has too many lines. [12/10] Open
def search
i14y_search_results = execute_client_search
if i14y_search_results.total.zero? && i14y_search_results.suggestion.present?
suggestion = i14y_search_results.suggestion
doc_query.query = suggestion['text']
- Read upRead up
- Exclude checks
Checks if the length of a method exceeds some maximum value. Comment lines can optionally be allowed. The maximum allowed length is configurable.
You can set constructs you want to fold with CountAsOne
.
Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
will be counted as one line regardless of its actual size.
NOTE: The ExcludedMethods
and IgnoredMethods
configuration is
deprecated and only kept for backwards compatibility.
Please use AllowedMethods
and AllowedPatterns
instead.
By default, there are no methods to allowed.
Example: CountAsOne: ['array', 'heredoc', 'method_call']
def m
array = [ # +1
1,
2
]
hash = { # +3
key: 'value'
}
<<~HEREDOC # +1
Heredoc
content.
HEREDOC
foo( # +1
1,
2
)
end # 6 points
Method has too many lines. [11/10] Open
def analyzer(json)
json.analyzer do
generic_analyzers(json)
french_analyzer(json)
japanese_analyzer(json)
- Read upRead up
- Exclude checks
Checks if the length of a method exceeds some maximum value. Comment lines can optionally be allowed. The maximum allowed length is configurable.
You can set constructs you want to fold with CountAsOne
.
Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
will be counted as one line regardless of its actual size.
NOTE: The ExcludedMethods
and IgnoredMethods
configuration is
deprecated and only kept for backwards compatibility.
Please use AllowedMethods
and AllowedPatterns
instead.
By default, there are no methods to allowed.
Example: CountAsOne: ['array', 'heredoc', 'method_call']
def m
array = [ # +1
1,
2
]
hash = { # +3
key: 'value'
}
<<~HEREDOC # +1
Heredoc
content.
HEREDOC
foo( # +1
1,
2
)
end # 6 points
Cyclomatic complexity for extract_hits is too high. [8/7] Open
def extract_hits(hits)
hits.map do |hit|
highlight = hit['highlight']
source = deserialized(hit)
if highlight.present?
- Read upRead up
- Exclude checks
Checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.
An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one. Blocks that are calls to builtin iteration methods (e.g. `ary.map{...}) also add one, others are ignored.
def each_child_node(*types) # count begins: 1
unless block_given? # unless: +1
return to_enum(__method__, *types)
children.each do |child| # each{}: +1
next unless child.is_a?(Node) # unless: +1
yield child if types.empty? || # if: +1, ||: +1
types.include?(child.type)
end
self
end # total: 6
Method has too many lines. [11/10] Open
def extract_site_filters
site_filters = { included_sites: [], excluded_sites: [] }
@stripped_query = @query.gsub(/\(?(-?site:\S+)\b\/?\)?/i) do
match = $1
if match.first == '-'
- Read upRead up
- Exclude checks
Checks if the length of a method exceeds some maximum value. Comment lines can optionally be allowed. The maximum allowed length is configurable.
You can set constructs you want to fold with CountAsOne
.
Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
will be counted as one line regardless of its actual size.
NOTE: The ExcludedMethods
and IgnoredMethods
configuration is
deprecated and only kept for backwards compatibility.
Please use AllowedMethods
and AllowedPatterns
instead.
By default, there are no methods to allowed.
Example: CountAsOne: ['array', 'heredoc', 'method_call']
def m
array = [ # +1
1,
2
]
hash = { # +3
key: 'value'
}
<<~HEREDOC # +1
Heredoc
content.
HEREDOC
foo( # +1
1,
2
)
end # 6 points
Method extract_hits
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
def extract_hits(hits)
hits.map do |hit|
highlight = hit['highlight']
source = deserialized(hit)
if highlight.present?
- 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 linguistic_filter
has 6 arguments (exceeds 4 allowed). Consider refactoring. Open
def linguistic_filter(json, locale, lines, name, field, type)
Method search
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
def search
i14y_search_results = execute_client_search
if i14y_search_results.total.zero? && i14y_search_results.suggestion.present?
suggestion = i14y_search_results.suggestion
doc_query.query = suggestion['text']
- 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. [27/25] Open
task :reindex, [:entity_name] => [:environment] do |_t, args|
entity_name = args.entity_name
persistence_model_klass = entity_name.singularize.camelize.constantize
klass = entity_name.camelize.constantize
template_generator = klass.new
- Read upRead up
- Exclude checks
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.
You can set constructs you want to fold with CountAsOne
.
Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct
will be counted as one line regardless of its actual size.
NOTE: The ExcludedMethods
configuration is deprecated and only kept
for backwards compatibility. Please use AllowedMethods
and AllowedPatterns
instead. By default, there are no methods to allowed.
Example: CountAsOne: ['array', 'heredoc', 'method_call']
something do
array = [ # +1
1,
2
]
hash = { # +3
key: 'value'
}
msg = <<~HEREDOC # +1
Heredoc
content.
HEREDOC
foo( # +1
1,
2
)
end # 6 points
NOTE: This cop does not apply for Struct
definitions.
Avoid parameter lists longer than 5 parameters. [6/5] Open
def linguistic_filter(json, locale, lines, name, field, type)
- Read upRead up
- Exclude checks
Checks for methods with too many parameters.
The maximum number of parameters is configurable. Keyword arguments can optionally be excluded from the total count, as they add less complexity than positional or optional parameters.
Any number of arguments for initialize
method inside a block of
Struct.new
and Data.define
like this is always allowed:
Struct.new(:one, :two, :three, :four, :five, keyword_init: true) do
def initialize(one:, two:, three:, four:, five:)
end
end
This is because checking the number of arguments of the initialize
method
does not make sense.
NOTE: Explicit block argument &block
is not counted to prevent
erroneous change that is avoided by making block argument implicit.
Example: Max: 3
# good
def foo(a, b, c = 1)
end
Example: Max: 2
# bad
def foo(a, b, c = 1)
end
Example: CountKeywordArgs: true (default)
# counts keyword args towards the maximum
# bad (assuming Max is 3)
def foo(a, b, c, d: 1)
end
# good (assuming Max is 3)
def foo(a, b, c: 1)
end
Example: CountKeywordArgs: false
# don't count keyword args towards the maximum
# good (assuming Max is 3)
def foo(a, b, c, d: 1)
end
This cop also checks for the maximum number of optional parameters.
This can be configured using the MaxOptionalParameters
config option.
Example: MaxOptionalParameters: 3 (default)
# good
def foo(a = 1, b = 2, c = 3)
end
Example: MaxOptionalParameters: 2
# bad
def foo(a = 1, b = 2, c = 3)
end
Space inside } missing. Open
NewRelic::Agent.notice_error(error, options: { custom_params: { indices: indices }})
- Read upRead up
- Exclude checks
Checks that braces used for hash literals have or don't have surrounding space depending on configuration.
Example: EnforcedStyle: space (default)
# The `space` style enforces that hash literals have
# surrounding space.
# bad
h = {a: 1, b: 2}
# good
h = { a: 1, b: 2 }
Example: EnforcedStyle: no_space
# The `no_space` style enforces that hash literals have
# no surrounding space.
# bad
h = { a: 1, b: 2 }
# good
h = {a: 1, b: 2}
Example: EnforcedStyle: compact
# The `compact` style normally requires a space inside
# hash braces, with the exception that successive left
# braces or right braces are collapsed together in nested hashes.
# bad
h = { a: { b: 2 } }
foo = { { a: 1 } => { b: { c: 2 } } }
# good
h = { a: { b: 2 }}
foo = {{ a: 1 } => { b: { c: 2 }}}
Example: EnforcedStyleForEmptyBraces: no_space (default)
# The `no_space` EnforcedStyleForEmptyBraces style enforces that
# empty hash braces do not contain spaces.
# bad
foo = { }
bar = { }
baz = {
}
# good
foo = {}
bar = {}
baz = {}
Example: EnforcedStyleForEmptyBraces: space
# The `space` EnforcedStyleForEmptyBraces style enforces that
# empty hash braces contain space.
# bad
foo = {}
# good
foo = { }
foo = { }
foo = {
}
Prefer single-quoted strings when you don't need string interpolation or special symbols. Open
NO_HITS = { "hits" => { "total" => 0, "hits" => [] }}
- Read upRead up
- Exclude checks
Checks if uses of quotes match the configured preference.
Example: EnforcedStyle: single_quotes (default)
# bad
"No special symbols"
"No string interpolation"
"Just text"
# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"
Example: EnforcedStyle: double_quotes
# bad
'Just some text'
'No special chars or interpolation'
# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"
Prefer Rails.root.join('path/to')
. Open
result = `#{Rails.root.join('vendor', 'stream2es')} es #{options.join(' ')}`
- Read upRead up
- Exclude checks
This cop is used to identify usages of file path joining process
to use Rails.root.join
clause. It is used to add uniformity when
joining paths.
Example: EnforcedStyle: arguments (default)
# bad
Rails.root.join('app/models/goober')
File.join(Rails.root, 'app/models/goober')
"#{Rails.root}/app/models/goober"
# good
Rails.root.join('app', 'models', 'goober')
Example: EnforcedStyle: slashes
# bad
Rails.root.join('app', 'models', 'goober')
File.join(Rails.root, 'app/models/goober')
"#{Rails.root}/app/models/goober"
# good
Rails.root.join('app/models/goober')
Use parentheses for method calls with arguments. Open
json.type "custom"
- Read upRead up
- Exclude checks
Enforces the presence (default) or absence of parentheses in method calls containing parameters.
In the default style (require_parentheses), macro methods are allowed.
Additional methods can be added to the AllowedMethods
or AllowedPatterns
list. These options are
valid only in the default style. Macros can be included by
either setting IgnoreMacros
to false or adding specific macros to
the IncludedMacros
list.
Precedence of options is all follows:
AllowedMethods
AllowedPatterns
IncludedMacros
eg. If a method is listed in both
IncludedMacros
and AllowedMethods
, then the latter takes
precedence (that is, the method is allowed).
In the alternative style (omit_parentheses), there are three additional options.
AllowParenthesesInChaining
isfalse
by default. Setting it totrue
allows the presence of parentheses in the last call during method chaining.AllowParenthesesInMultilineCall
isfalse
by default. Setting it totrue
allows the presence of parentheses in multi-line method calls.AllowParenthesesInCamelCaseMethod
isfalse
by default. This allows the presence of parentheses when calling a method whose name begins with a capital letter and which has no arguments. Setting it totrue
allows the presence of parentheses in such a method call even with arguments.
NOTE: Parentheses are still allowed in cases where omitting them results in ambiguous or syntactically incorrect code. For example, parentheses are required around a method with arguments when inside an endless method definition introduced in Ruby 3.0. Parentheses are also allowed when forwarding arguments with the triple-dot syntax introduced in Ruby 2.7 as omitting them starts an endless range. And Ruby 3.1's hash omission syntax has a case that requires parentheses because of the following issue: https://bugs.ruby-lang.org/issues/18396.
Example: EnforcedStyle: require_parentheses (default)
# bad
array.delete e
# good
array.delete(e)
# good
# Operators don't need parens
foo == bar
# good
# Setter methods don't need parens
foo.bar = baz
# okay with `puts` listed in `AllowedMethods`
puts 'test'
# okay with `^assert` listed in `AllowedPatterns`
assert_equal 'test', x
Example: EnforcedStyle: omit_parentheses
# bad
array.delete(e)
# good
array.delete e
# bad
foo.enforce(strict: true)
# good
foo.enforce strict: true
# good
# Allows parens for calls that won't produce valid Ruby or be ambiguous.
model.validate strict(true)
# good
# Allows parens for calls that won't produce valid Ruby or be ambiguous.
yield path, File.basename(path)
# good
# Operators methods calls with parens
array&.[](index)
# good
# Operators methods without parens, if you prefer
array.[] index
# good
# Operators methods calls with parens
array&.[](index)
# good
# Operators methods without parens, if you prefer
array.[] index
Example: IgnoreMacros: true (default)
# good
class Foo
bar :baz
end
Example: IgnoreMacros: false
# bad
class Foo
bar :baz
end
Example: AllowParenthesesInMultilineCall: false (default)
# bad
foo.enforce(
strict: true
)
# good
foo.enforce \
strict: true
Example: AllowParenthesesInMultilineCall: true
# good
foo.enforce(
strict: true
)
# good
foo.enforce \
strict: true
Example: AllowParenthesesInChaining: false (default)
# bad
foo().bar(1)
# good
foo().bar 1
Example: AllowParenthesesInChaining: true
# good
foo().bar(1)
# good
foo().bar 1
Example: AllowParenthesesInCamelCaseMethod: false (default)
# bad
Array(1)
# good
Array 1
Example: AllowParenthesesInCamelCaseMethod: true
# good
Array(1)
# good
Array 1
Example: AllowParenthesesInStringInterpolation: false (default)
# bad
"#{t('this.is.bad')}"
# good
"#{t 'this.is.better'}"
Example: AllowParenthesesInStringInterpolation: true
# good
"#{t('this.is.good')}"
# good
"#{t 'this.is.also.good'}"
Avoid multi-line chains of blocks. Open
end.each do |locale, file|
- Read upRead up
- Exclude checks
Checks for chaining of a block after another block that spans multiple lines.
Example:
# bad
Thread.list.select do |t|
t.alive?
end.map do |t|
t.object_id
end
# good
alive_threads = Thread.list.select do |t|
t.alive?
end
alive_threads.map do |t|
t.object_id
end
Prefer single-quoted strings when you don't need string interpolation or special symbols. Open
json.match_mapping_type "string"
- Read upRead up
- Exclude checks
Checks if uses of quotes match the configured preference.
Example: EnforcedStyle: single_quotes (default)
# bad
"No special symbols"
"No string interpolation"
"Just text"
# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"
Example: EnforcedStyle: double_quotes
# bad
'Just some text'
'No special chars or interpolation'
# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"
Prefer single-quoted strings when you don't need string interpolation or special symbols. Open
json.type "stemmer"
- Read upRead up
- Exclude checks
Checks if uses of quotes match the configured preference.
Example: EnforcedStyle: single_quotes (default)
# bad
"No special symbols"
"No string interpolation"
"Just text"
# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"
Example: EnforcedStyle: double_quotes
# bad
'Just some text'
'No special chars or interpolation'
# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"
Use each_value
instead of values.each
. Open
doc_query.full_text_fields.values.each do |field|
- Read upRead up
- Exclude checks
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.
Safety:
This cop is unsafe because it cannot be guaranteed that the receiver
is a Hash
. The AllowedReceivers
configuration can mitigate,
but not fully resolve, this safety issue.
Example:
# bad
hash.keys.each { |k| p k }
hash.values.each { |v| p v }
# good
hash.each_key { |k| p k }
hash.each_value { |v| p v }
Example: AllowedReceivers: ['execute']
# good
execute(sql).keys.each { |v| p v }
execute(sql).values.each { |v| p v }
Use match?
instead of ===
when MatchData
is not used. Open
elsif /description/ === field
- Read upRead up
- Exclude checks
In Ruby 2.4, String#match?
, Regexp#match?
and Symbol#match?
have been added. The methods are faster than match
.
Because the methods avoid creating a MatchData
object or saving
backref.
So, when MatchData
is not used, use match?
instead of match
.
Example:
# bad
def foo
if x =~ /re/
do_something
end
end
# bad
def foo
if x.match(/re/)
do_something
end
end
# bad
def foo
if /re/ === x
do_something
end
end
# good
def foo
if x.match?(/re/)
do_something
end
end
# good
def foo
if x =~ /re/
do_something(Regexp.last_match)
end
end
# good
def foo
if x.match(/re/)
do_something($~)
end
end
# good
def foo
if /re/ === x
do_something($~)
end
end