Showing 182 of 182 total issues
Class has too many lines. [105/100] Open
class Core
attr_reader :context_key, :context_value
def initialize(key_id:, msgpack: false, context_key: nil, context_value: nil)
@base_key_id = key_id
- Read upRead up
- Exclude checks
This cop checks if the length a class exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Assignment Branch Condition size for kms_attr is too high. [47.25/15] Open
def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, context_value: nil)
include InstanceMethods
real_field = "#{field}_enc"
enc = Core.new(key_id: key_id, msgpack: msgpack, context_key: context_key, context_value: context_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. [37/10] Open
def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, context_value: nil)
include InstanceMethods
real_field = "#{field}_enc"
enc = Core.new(key_id: key_id, msgpack: msgpack, context_key: context_key, context_value: context_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 kms_attr
has a Cognitive Complexity of 29 (exceeds 5 allowed). Consider refactoring. Open
def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, context_value: nil)
include InstanceMethods
real_field = "#{field}_enc"
enc = Core.new(key_id: key_id, msgpack: msgpack, context_key: context_key, context_value: context_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
Assignment Branch Condition size for kms_args is too high. [23.87/15] Open
def kms_args(field_numbers, key_id:, msgpack: false, context_key: nil, context_value: nil)
enc = Core.new(key_id: key_id, context_key: context_key, msgpack: msgpack, context_value: context_value)
define_method 'serialize_arguments' do |args|
args = args.dup
- 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 kms_attr is too high. [13/6] Open
def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, context_value: nil)
include InstanceMethods
real_field = "#{field}_enc"
enc = Core.new(key_id: key_id, msgpack: msgpack, context_key: context_key, context_value: context_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.
Method has too many lines. [17/10] Open
def kms_args(field_numbers, key_id:, msgpack: false, context_key: nil, context_value: nil)
enc = Core.new(key_id: key_id, context_key: context_key, msgpack: msgpack, context_value: context_value)
define_method 'serialize_arguments' do |args|
args = args.dup
- 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 kms_attr is too high. [14/7] Open
def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, context_value: nil)
include InstanceMethods
real_field = "#{field}_enc"
enc = Core.new(key_id: key_id, msgpack: msgpack, context_key: context_key, context_value: context_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
Method has too many lines. [12/10] Open
def apply_context(args, key, value)
if key && value
if key.is_a?(Proc)
key = key.call
end
- 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. [12/10] Open
def key_id
case @base_key_id
when Proc
@base_key_id.call
when 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.
Cyclomatic complexity for apply_context is too high. [7/6] Open
def apply_context(args, key, value)
if key && value
if key.is_a?(Proc)
key = key.call
end
- 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. [11/10] Open
define_method "#{field}" do
raise RuntimeError, "Field '#{field}' must not be a real column, '#{real_field}' is the real column" if self.class.column_names.include?(field.to_s)
raise RuntimeError, "Field '#{real_field}' must exist to retrieve decrypted data" unless self.class.column_names.include?(real_field)
hash = get_hash(field)
- 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. [11/10] Open
define_method "#{field}=" do |data|
raise RuntimeError, "Field '#{field}' must not be a real column, '#{real_field}' is the real column" if self.class.column_names.include?(field.to_s)
raise RuntimeError, "Field '#{real_field}' must exist to store encrypted data" unless self.class.column_names.include?(real_field)
if data.blank? # Just set to nil if nil
- Read upRead up
- Exclude checks
This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Method has too many lines. [11/10] Open
def encrypt(data)
return nil if data.nil?
data_key = aws_generate_data_key(key_id)
data = data.to_msgpack if @msgpack
- 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 kms_attr
has 37 lines of code (exceeds 25 allowed). Consider refactoring. Open
def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, context_value: nil)
include InstanceMethods
real_field = "#{field}_enc"
enc = Core.new(key_id: key_id, msgpack: msgpack, context_key: context_key, context_value: context_value)
Method kms_args
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
def kms_args(field_numbers, key_id:, msgpack: false, context_key: nil, context_value: nil)
enc = Core.new(key_id: key_id, context_key: context_key, msgpack: msgpack, context_value: context_value)
define_method 'serialize_arguments' do |args|
args = args.dup
- 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
KmsRails::ActiveJob::ClassMethods#kms_arg has boolean parameter 'msgpack' Open
def kms_arg(field_number, key_id:, msgpack: false, context_key: nil, context_value: nil)
- Read upRead up
- Exclude checks
Boolean Parameter
is a special case of Control Couple
, where a method parameter is defaulted to true or false. A Boolean Parameter effectively permits a method's caller to decide which execution path to take. This is a case of bad cohesion. You're creating a dependency between methods that is not really necessary, thus increasing coupling.
Example
Given
class Dummy
def hit_the_switch(switch = true)
if switch
puts 'Hitting the switch'
# do other things...
else
puts 'Not hitting the switch'
# do other things...
end
end
end
Reek would emit the following warning:
test.rb -- 3 warnings:
[1]:Dummy#hit_the_switch has boolean parameter 'switch' (BooleanParameter)
[2]:Dummy#hit_the_switch is controlled by argument switch (ControlParameter)
Note that both smells are reported, Boolean Parameter
and Control Parameter
.
Getting rid of the smell
This is highly dependent on your exact architecture, but looking at the example above what you could do is:
- Move everything in the
if
branch into a separate method - Move everything in the
else
branch into a separate method - Get rid of the
hit_the_switch
method alltogether - Make the decision what method to call in the initial caller of
hit_the_switch
KmsRails::Core#initialize has boolean parameter 'msgpack' Open
def initialize(key_id:, msgpack: false, context_key: nil, context_value: nil)
- Read upRead up
- Exclude checks
Boolean Parameter
is a special case of Control Couple
, where a method parameter is defaulted to true or false. A Boolean Parameter effectively permits a method's caller to decide which execution path to take. This is a case of bad cohesion. You're creating a dependency between methods that is not really necessary, thus increasing coupling.
Example
Given
class Dummy
def hit_the_switch(switch = true)
if switch
puts 'Hitting the switch'
# do other things...
else
puts 'Not hitting the switch'
# do other things...
end
end
end
Reek would emit the following warning:
test.rb -- 3 warnings:
[1]:Dummy#hit_the_switch has boolean parameter 'switch' (BooleanParameter)
[2]:Dummy#hit_the_switch is controlled by argument switch (ControlParameter)
Note that both smells are reported, Boolean Parameter
and Control Parameter
.
Getting rid of the smell
This is highly dependent on your exact architecture, but looking at the example above what you could do is:
- Move everything in the
if
branch into a separate method - Move everything in the
else
branch into a separate method - Get rid of the
hit_the_switch
method alltogether - Make the decision what method to call in the initial caller of
hit_the_switch
KmsRails::ActiveJob::ClassMethods#kms_args contains iterators nested 2 deep Open
field_numbers.each do |i|
# We skip encoding if nil or if already encrypted
unless args[i].nil? || (args[i].class == Hash && args[i].keys.to_set == ['key', 'iv', 'blob'].to_set)
args[i] = enc.encrypt64(args[i])
end
- Read upRead up
- Exclude checks
A Nested Iterator
occurs when a block contains another block.
Example
Given
class Duck
class << self
def duck_names
%i!tick trick track!.each do |surname|
%i!duck!.each do |last_name|
puts "full name is #{surname} #{last_name}"
end
end
end
end
end
Reek would report the following warning:
test.rb -- 1 warning:
[5]:Duck#duck_names contains iterators nested 2 deep (NestedIterators)
KmsRails::ActiveRecord::ClassMethods#kms_attr has approx 28 statements Open
def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, context_value: nil)
- Read upRead up
- Exclude checks
A method with Too Many Statements
is any method that has a large number of lines.
Too Many Statements
warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements
counts +1 for every simple statement in a method and +1 for every statement within a control structure (if
, else
, case
, when
, for
, while
, until
, begin
, rescue
) but it doesn't count the control structure itself.
So the following method would score +6 in Reek's statement-counting algorithm:
def parse(arg, argv, &error)
if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
return nil, block, nil # +1
end
opt = (val = parse_arg(val, &error))[1] # +2
val = conv_arg(*val) # +3
if opt and !arg
argv.shift # +4
else
val[0] = nil # +5
end
val # +6
end
(You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)