Method resolve
has a Cognitive Complexity of 20 (exceeds 11 allowed). Consider refactoring. Open
def self.resolve(rec, list = nil, event = nil)
# list is expected to be a list of policies, not profiles.
policies = list.nil? ? all : where(:name => list)
policies.collect do |p|
next if event && !p.events.include?(event)
- Read upRead up
- Create a ticketCreate a ticket
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method resolve_policy_conditions
has a Cognitive Complexity of 14 (exceeds 11 allowed). Consider refactoring. Open
def self.resolve_policy_conditions(policy, rec)
policy_result = 'allow'
conditions =
policy.conditions.collect do |c|
rec_model = rec.class.base_model.name
- Read upRead up
- Create a ticketCreate a ticket
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Avoid parameter lists longer than 5 parameters. [6/5] Open
def self.invoke_actions(target, mode, profiles, succeeded, failed, inputs)
- Read upRead up
- Create a ticketCreate a ticket
- 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
Use match?
instead of match
when MatchData
is not used. Open
towhat = "Vm" if towhat.downcase.match("template")
- Read upRead up
- Create a ticketCreate a ticket
- 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
Use filter_map
instead. Open
miq_policy_contents.collect(&:miq_event_definition).compact.uniq
- Create a ticketCreate a ticket
- Exclude checks
Use filter_map
instead. Open
policies.collect do |p|
next unless p.kind_of?(self) # skip built-in policies
{
:miq_policy => p,
:result => status.to_s,
- Create a ticketCreate a ticket
- Exclude checks
Use =~
in places where the MatchData
returned by #match
will not be used. Open
rec_model = "Vm" if rec_model.downcase.match("template")
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop identifies the use of Regexp#match
or String#match
, which
returns #<MatchData>
/nil
. The return value of =~
is an integral
index/nil
and is more performant.
Example:
# bad
do_something if str.match(/regex/)
while regex.match('str')
do_something
end
# good
method(str =~ /regex/)
return value unless regex =~ 'str'
Use filter_map
instead. Open
miq_policy_contents.where(:miq_event_definition => event).order(order).collect do |pe|
next unless pe.qualifier == on.to_s
pe.get_action(on)
end.compact
- Create a ticketCreate a ticket
- Exclude checks
Use filter_map
instead. Open
policy.conditions.collect do |c|
rec_model = rec.class.base_model.name
rec_model = "Vm" if rec_model.downcase.match("template")
next unless rec_model == c["towhat"]
- Create a ticketCreate a ticket
- Exclude checks
Use filter_map
instead. Open
miq_policy_contents.collect(&:miq_action).compact.uniq
- Create a ticketCreate a ticket
- Exclude checks
Use filter_map
instead. Open
policies.collect do |p|
next if event && !p.events.include?(event)
policy_hash = {"result" => "N/A", "conditions" => [], "actions" => []}
policy_hash["scope"] = MiqExpression.evaluate_atoms(p.expression, rec) unless p.expression.nil?
- Create a ticketCreate a ticket
- Exclude checks
Use =~
in places where the MatchData
returned by #match
will not be used. Open
rec_model = "Vm" if rec_model.downcase.match("template")
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop identifies the use of Regexp#match
or String#match
, which
returns #<MatchData>
/nil
. The return value of =~
is an integral
index/nil
and is more performant.
Example:
# bad
do_something if str.match(/regex/)
while regex.match('str')
do_something
end
# good
method(str =~ /regex/)
return value unless regex =~ 'str'
Use =~
in places where the MatchData
returned by #match
will not be used. Open
towhat = "Vm" if towhat.downcase.match("template")
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop identifies the use of Regexp#match
or String#match
, which
returns #<MatchData>
/nil
. The return value of =~
is an integral
index/nil
and is more performant.
Example:
# bad
do_something if str.match(/regex/)
while regex.match('str')
do_something
end
# good
method(str =~ /regex/)
return value unless regex =~ 'str'
Use filter_map
instead. Open
associations_to_get_policies.collect do |assoc|
next unless target.respond_to?(assoc)
obj = target.send(assoc)
next unless obj
- Create a ticketCreate a ticket
- Exclude checks
Use match?
instead of match
when MatchData
is not used. Open
rec_model = "Vm" if rec_model.downcase.match("template")
- Read upRead up
- Create a ticketCreate a ticket
- 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
Use match?
instead of match
when MatchData
is not used. Open
rec_model = "Vm" if rec_model.downcase.match("template")
- Read upRead up
- Create a ticketCreate a ticket
- 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